Newton's Method implementing in Matlab
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone,
This weeks task in our course "numerical methods" we have to implement a Newton's Method following the given instruction:
% function x=newton(x0,F,DF,nMax,TOL)
%starting point and Value of F in the starting point.
x = x0;
Fx_new = F;
%Newton iteration
for ii = 1:nMax
%value of F and DF in the old iteration
Fx = Fx_new;
DFx = DF;
%condition, DF not zero
if abs(DFx)<1e2*eps
fprintf('DFx ist zu klein\n')
assert(false);
end
%Newton update
delta_x = fx/DFx;
%new iterate
x = x-delta_x;
%value of F in the new iterated
Fx_new = Fx;
%if update too small -> stop and exit
if abs(DFx\Fx_new) < TOL;
break
end
end
However, it doesnt work. I'm not quite sure how to handle F at position x0 and how to the new iterated for F. It would be most kind if someone would help me.
Kind regards
Jean-Marc
0 Kommentare
Antworten (1)
Geoff Hayes
am 18 Apr. 2015
Jean-March - I suspect that the inputs F and DF are function handles to the function and its first derivative. For example,
f = @(x)x^2 + 3*x;
df = @(x)2*x + 3;
where df is the first derivative of f. As these are function handles, we can evaluate them for any value of x. For example, if x is 42, then we can write
f(42)
df(42)
to evaluate f and df are 42. For Newton's Method, you would use these two functions as
xnl = xn - f(xn)/df(xn);
where xn is the value calculated on the previous iteration (or x0 if first the first iteration).
Please be careful when naming your variables - you attempt to use an fx but it is never assigned. Should this be Fx instead? Also, is delta_x an appropriately named variable?
Try implementing the above into your code and see what happens!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Function Creation 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!