2016-09-15 1 views
0

MacLaurinシリーズを使用して、exp(2)と見積もりを依頼しました。私はそれをさらに進化させ、 、あなたが見積もりしたいと思っている数値とそれを持っている有効数字の数を入力してください。MacLaurinシリーズのdiff(f、x、n)に関する問題

それは、特定の方程式に適しています

、すなわちexp(x)sin(x)、でもexp(x) + 2xが、私はxに高い電力で投げる二つ目は、それが0を返しx^2言います。

たとえば、関数を:MacLaurin(x^2,2,1)と呼び出すと、適切な答えではない0が返されます。ここで

はコードです:

% Created by: DarkRiot43 
% Student #: 
% Date: Sept 12, 2016 
% Course: Numerical Analysis, MTH 510 

function [ ] = MacLaurin(func , valueOfx, sigfigs) 
%MACLAURIN Summary of this function goes here 
% Detailed explanation goes here 
%  inputs: func: a function 
%    sigfigs: the number of significant figures you would like 
%    to have the answer evaluated to. 
clc; 
syms f(x) x ; 
f(x) = func; 
presentApprox = 0; 
previousApprox = 0; 
n = 0; 
% Find the criterion for stopping the iteration as an double not a percent. 
Es = (0.5*10^(2-round(sigfigs)))/100; 

Ea =Es+1; % Ensures Ea is larger than Es to begin 

while Ea >= Es 
    %differentiate the n'th derivative of the equation f(x) w.r.t. x 
    beenDiff = diff(f(x),n); 
    presentApprox = previousApprox + (vpa(subs(beenDiff,x,0))* (((valueOfx)^n)/factorial(n))); %MacLaurin series structure used to evaluate. 
    n = n+1; %Counter incrementation 
    % Call to function approxError to determine approximate relative error. 
    Ea = approxError(presentApprox,previousApprox); 
    previousApprox = presentApprox; 
    f(x) = beenDiff;  
    % can be used to ensure proper iterating counting of the program 
    disp(n); 

    disp(f(x)); %shows the n'th derivative function 

    %check to see if in the approxError function there would have been a 
    %by zero error. if so I edit the value (999 was all i could think of? 
    % was thinking of using something else but didn't know what to use 
    if Ea == 999 
     break; 
    end 
end 
else 
    fprintf('\nStopping criterion used:\t'); 
    disp(Es*100) 


    fprintf('The estimate generated using MacLaurin Series of '); 
    disp(func); 
    fprintf('using the value x = %.3f is: \n\n',valueOfx); 
    fprintf('%7f',presentApprox); 
    fprintf('\nThis was done using %d iterations', n); 

    if Ea == 0 
     fprintf('\nCould not reach the requested stopping criterion.'); 

    end 


end 

function [Ea] = approxError(presentApprox,previousApprox) 
%Approximate relative error Function 
% inputs: 
% presentApprox: type double 
% previousApprox: type double 
% returns: 
% Ea: type double 
%  
% Uses two values to determine the approximate relative error w.r.t 
%  eachother 

    if presentApprox ~= 0 

     Ea = (presentApprox - previousApprox)/(presentApprox); 

    elseif presentApprox == 0 

     disp('Cannot determine the value with more precision as it would involve dividing by zero!'); 
     Ea = 999; 

    end 
end 

私は不明であった場合はお問い合わせください。

答えて

0

コードにはいくつか問題があります(私が編集したwhileループの最後にある構文エラー以外)。最初の行はf(x)= beenDiffの行です。 beenDiffはn番目の微分であるはずですので、この代入を行うと、diffは実際に(n!)番目の微分になります。行f(x)= beenDiffを削除すると、この問題がソートされます。

2番目の問題は、停止基準が良くないことです。もし微分関数のいずれかが0を生成した場合、さらなる反復が値を変更するかどうかにかかわらず、コードは単に差がゼロであるので停止します。コサインのようなものは、これが問題です。 (関数x = 0で関数自体がゼロを生成すると、コードは実行を拒否するため、sin(x)は単に機能しません)。

停止基準を取得することは、大きなジャンプを取得しないように、関数の後の導関数の大きさを知る必要があるため、それ自体にとって非常に難題です。 (関数は収束するかもしれませんが、反復ごとに単調に収束する必要はありません)。

+0

ありがとうDave、 これらの問題を調べます。私が思ったように、私は頭の中にいるようです。それが言われて、それは私が現在どこにいるのかはいいチャレンジでした。 – DarkRiot43

関連する問題