我们有一个任务要求我们exp(2)
使用 MacLaurin 级数进行估计,我已经进一步尝试开发和运行,您可以在其中输入一个方程、x
您希望估计的值以及您希望有多少个有效数字.
它适用于某些方程,即exp(x)
, sin(x)
,甚至exp(x) + 2x
,但第二次我投入更高的功率x
,说它x^2
返回0
。
例如,当我将函数调用为: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
请问我是否有不清楚的地方。