2016-12-05 1 views
-2
function [est k ] = approximate_e(delta) 
%APPROXIMATE_E Summary of this function goes here 
% Detailed explanation goes here 
n =1; 
est = 0; 
while abs(exp(1)-est)>delta 
    if n ==1 
     est = 1; 
    end 
    if n == 2 
     est = 2; 
    end 
    if n >2 
     est = est+1/prod(1:(n-1)); 
    end 
    fprintf "e is %d and n is %d: \n",est,n 
    k = n; 
    n = n + 1; 
    if n >10000 
     fprinf "n is greater than 10000.\n" 
     break; 
    end 
end 

나는 오일러 수 계산과 관련된 질문에 답하기 위해 앞의 코드를 작성했습니다. 문제는 다음과 같습니다Matlab 솔루션 필요

쓰기 다음 공식 를 사용 approximate_e라는 함수가 전자를 계산하기 위해, 오일러의 수 :

enter image description here

대신 무한대로 가고는 기능은 정차 근사가 exp (1)과 다른 최소 k (즉,

값이 반환 됨 MATLAB의 bui lt-in 함수)보다 더 작은 숫자 인 양의 스칼라 인 delta 만 입력 인수입니다. 함수의 첫 번째 출력은 e의 근사치이며, 두 번째는 입니다. (주의 : 프로그램이나 그레이더가 오랜 시간이 걸릴 경우, 은 무한 루프를 생성하고 키보드 에서 Ctrl-C를 눌러야합니다.) 내장 함수 계승을 사용할 수 없습니다.

+4

그리고 질문은 어디에 있습니까? – obchardon

+0

그래서 당신의 코드는 예상치 못한 일을합니다 ('fprintf's 제외)? – beaker

답변

0
function [est] = approximate_e(delta) 
%APPROXIMATE_E Summary of this function goes here 
% Detailed explanation goes here 
n = 0; 
est = 0; 
TerminationCondition = 10000; 
while abs(exp(1)-est)>delta 
    est = 1/factorial(n)+est; 
    display(['The current value of est is ', num2str(est),char(10)]); 
    n = n + 1; 
    if n > TerminationCondition 
     break; 
    end 
end 
if n > TerminationCondition 
    display(['n is greater than ', num2str(TerminationCondition), ' terminating loop']); 
else 
    display(['This estimation took ', num2str(n), ' cycles to complete']); 
end 

당신은 (변수 (n) 인) 당신에게 k 값을 제공하는 기능을 조정해야합니다.

관련 문제