フィルターのクリア

Determine f(x)=0 with newtons method

8 ビュー (過去 30 日間)
Joakim Karlsson
Joakim Karlsson 2017 年 11 月 4 日
コメント済み: Matt J 2017 年 11 月 4 日
Hi everybody. I am going to be honest that this is something for school but I need some help and guidance with it. I am almost 100% done with the code but it somehows doesnt seem to work.
So the task is to determine f(x)=0 using newtons method and my function code looks like this
function x = min_newton(f,Df,x0,tol)
%Determine f(x)=0
%Code below
kmax=100;
x=x0
for k=1:kmax
h=-f(x)/Df(x);
x=x+h;
if abs(h)<tol, break, end
end
end
It seems alright to me. Then my code below is to determine the f(x)=0 to a given function.
f=@(x)0.5.*(x-2).^2-2.*cos(2*x)-1.5;
Df=@(x)x-2.*cos(2*x)-3.5;
plot(x,f(x)) %intersects approx at x=-0.5 x=1, x=2, x=4
x0=-0.5;
zero=min_newton(f,Df,x0,0.5e-8);
disp(zero)
The problem for me here is whatever I set my x0 to it only calculates it for the first zero which is approx at -0.5 and if I set my x0=1 it still calculates for -0.5. What is wrong here exactly?
I have tried different kmax etc.

採用された回答

Matt J
Matt J 2017 年 11 月 4 日
編集済み: Matt J 2017 年 11 月 4 日
Your Df(x) looks very wrong.
  2 件のコメント
Joakim Karlsson
Joakim Karlsson 2017 年 11 月 4 日
Yes of course, my bad.
Df=@(x)x+4.*sin(2*x)-2;
This seems more correct?
Matt J
Matt J 2017 年 11 月 4 日
Yes, it does.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by