Using Secant Method...issue with arctanx

5 ビュー (過去 30 日間)
Drake
Drake 2013 年 10 月 6 日
編集済み: Matt J 2013 年 10 月 6 日
I'm using secant method, I've done previous problems with the same setup of x2 = ..., but when I'm using arctan(x) as the function it tells me there is an error with the x2 = ... line (inside for loop). I have no idea why.
f = @(x) arctan(x);
format long
maxN = 10;
x0 = 1;
x1 = -0.5;
r = 0; %know r=0 in advance
Err = .0000001; %max error
for i = 1:maxN
x2 = x1 - f(x1)*(x1-x0)/(f(x1)-f(x0))
en1 = r - x2; %e_n+1
en = r - x1; %e_n
enm1 = r - x0; %e_n-1
ratio = en1/(en*enm1)
if abs((x2-x1)/x1) < Err
r2 = x2; %root
f(r2)
break
end
x1 = x0
x0 = x2
end
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 10 月 6 日
What is the error message?

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

採用された回答

Matt J
Matt J 2013 年 10 月 6 日
編集済み: Matt J 2013 年 10 月 6 日
MATLAB has no function called "arctan". You really want
f=@atan;
Also, instead of
x1 = x0
x0 = x2
I think you want
[x1,x0]=deal(x2,x1);

その他の回答 (0 件)

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by