Newton Raphson Getting Wrong Root

I'm trying use the Newton Raphson method to produce a root of -3.4256, according to fzero(f,-3.5). I think I have the equations right but for some reason it gets -0.9428. Please help correct mistakes. Thanks!
f=@(x) x*tan(x)-1;
df = @(x) tan(x) + x*(tan(x)^2 - 1);
x1 =-5;
x2=-1.5;
x=x1;
for i=1:1:10
x3=x-(f(x)/df(x));
x=x3;
end
fprintf('Approx. Root is %.15f\n',x);

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 4 月 8 日

0 投票

This is a general characteristic of Newton Raphson: because of the way it does the projections, you can easily end up projecting into a different "basin of attraction" than the one you are currently in.
Try this:
function x = do_nr(x1, f, df)
x=x1;
for i=1:1:10
x3 = x - (f(x)/df(x));
x=x3;
end
end
together with
f=@(x) x*tan(x)-1;
df = @(x) tan(x) + x*(tan(x)^2 - 1);
x1 = linspace(-5, -2.5, 500);
root_found = arrayfun( @(X1) do_nr(X1, f, df), x1);
plot(x1, root_found)
The values on the y axis will be the root located. Notice the scale -- and notice how bumpy the graph is.
The closer you get to a root, the further you can end up projecting, because the angle can be very steep.

4 件のコメント

Farrah Rinehart
Farrah Rinehart 2018 年 4 月 8 日
編集済み: Farrah Rinehart 2018 年 4 月 8 日
The plot doesn't show for some reason. I typed it in as:
x1=linspace(-5,-1.5,500);
root_found=arrayfun(@(X1) do_nr(X1,f,df),x1);
plot(x1,root_found)
function x = do_nr(x1,f,df)
x=x1;
for i=1:1:10
x3=x-(f(x)/df(x));
x=x3;
end
end
Walter Roberson
Walter Roberson 2018 年 4 月 9 日
If you are using a release before R2016b you will need to use separate files for the two pieces.
Farrah Rinehart
Farrah Rinehart 2018 年 4 月 9 日
編集済み: Farrah Rinehart 2018 年 4 月 9 日
I'm using R2017b.
Edit Found the plot, it just wasn't showing up like earlier. However it's not bumpy at all, although I do notice the scale.
Walter Roberson
Walter Roberson 2018 年 4 月 9 日
On the scale of 10^10 the bumps are not showing up. If you were to change your range in the linspace() then you would change the size of that peak. You can also use the zoom tool to look at that range between -5 and -2

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

質問済み:

2018 年 4 月 8 日

コメント済み:

2018 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by