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

조회 수: 1 (최근 30일)
Patrick
Patrick 2024년 3월 1일
편집: Walter Roberson 2024년 3월 2일
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);

채택된 답변

Star Strider
Star Strider 2024년 3월 1일
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.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by