Help storing my iterations into my variables

1 Ansicht (letzte 30 Tage)
daniel choudhry
daniel choudhry am 2 Nov. 2020
Beantwortet: VBBV am 2 Nov. 2020
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

Akzeptierte Antwort

VBBV
VBBV am 2 Nov. 2020
Inside the while loop use
%if true
% code
% end
X(k) = xr;
EE(k) = ea;
F(k) = yrp;

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by