I'm unsure of why there is an error on F=@x

2 views (last 30 days)
function Xs= newtonM(fun,FunDer,Xest,err)
for i=1:100
Xs= Xest-Fun(Xest)/FunDer(Xest);
error =abs((Xs-Xest)/Xs)*100;
fprintf('%3i %11.6f %11.6\n', i, Xs, error)
if error < err
break;
end
Xest= Xs;
end
end
f=@(x)exp(-0.5*x)*(4-x)-2;
df=@(x)exp(-0.5*x)*(-3+0.5*x);
Xs = newtonM(f,df,5);

Accepted Answer

Star Strider
Star Strider on 1 Mar 2024
The statement order is reversed from what it should be —
f=@(x)exp(-0.5*x)*(4-x)-2;
df=@(x)exp(-0.5*x)*(-3+0.5*x);
Xs = newtonM(f,df,5,0.001)
1 -45.729976 2 -43.807300 3 -41.887610 4 -39.971139 5 -38.058150 6 -36.148939 7 -34.243841 8 -32.343235 9 -30.447556 10 -28.557302 11 -26.673052 12 -24.795477 13 -22.925367 14 -21.063656 15 -19.211459 16 -17.370128 17 -15.541316 18 -13.727084 19 -11.930062 20 -10.153724 21 -8.402890 22 -6.684771 23 -5.011257 24 -3.404173 25 -1.907058 26 -0.607889 27 0.340095 28 0.795644 29 0.882955 30 0.885706 31 0.885709
Xs = 0.8857
function Xs= newtonM(fun,FunDer,Xest,err)
for i=1:100
Xs= Xest-fun(Xest)/FunDer(Xest);
error =abs((Xs-Xest)/Xs)*100;
fprintf('%3i %11.6f %11.6\n', i, Xs, error)
if error < err
break;
end
Xest= Xs;
end
end
Also, ‘newtonM’ needed an ‘err’ argument.
.

More Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!