Help storing my iterations into my variables
1 次查看(过去 30 天)
显示 更早的评论
I am having trouble storying my iterations into my variables X,EE, F that are inside my while loop
%Function and derivatives
function [X,EE,F]=my_newton(x0)
f = @ (x) x^2; %function
fp = @ (x) 2*x; % Derivative
X=[];
EE=[];
F=[];
%%%% Newton's Method search theory (c) part (b)%%%%
x0 = .5; % Starting location
y0 = f(x0);
es = 10^(-4); % Want error less than 0.5%
ea = 1.0; % Initialize approx. error
k = 0;
xr = x0; % initialize root guess
yr = y0;
yrp = fp(xr);
while (ea > es) && (k <= 2)
k = k +1;
xr_old = xr; % Updating previous root location
yr_old = yr; % function evaluation
yrp_old = yrp; % and derivative evaluation at xr_old
xr = xr_old - yr_old/yrp_old; % new root location guess
yr = f(xr); % Calculating function eval at new guess
yrp = fp(xr); % Calc. derivative eval at new guess
ea = abs((xr - xr_old)/xr); % updating relative approx. error
X=[xr]
EE=[ea];
F=[yrp];
if k>20
disp('To many iterations')
return
end
end
fprintf('Approximate root after %d operations is: %12.15f with approximate error %12.15f',k,xr,ea);
end
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!