When I am doing Newton's Method with a tolerance, how do I only get the values up until it reaches the tolerance?

Using Newton's method, I was wondering if I was using the while loop correctly because it keeps running and doesn't stop even though I have a tolerance put into the code.
function [R,E] = myNewton(f, df, x0, tol)
R=myNewton(f,df,x0-f(x0)/df(x0),tol);
E=myNewton(f,df,(x0-R)*df(x0),tol);
while abs(f®)>tol
R=R+(x0-f(x0)/df(x0));
E=E+((x0-R)*df(x0));
end
end

1 件のコメント

Carlos
Carlos 2014 年 3 月 18 日
Sorry but what does abs(f®)mean? I cannot distinguish the symbols inside abs. If you edit your code, perhaps we could see more clearly.
Regards

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

回答 (2 件)

Christopher, I am not quite sure I understand your code.
  • What's the reason for executing myNewton twice, once for R once for E?
  • Why are you calling myNewton from whithin myNewton?
The code below shows a simple example on how to implement Newton's method.
function myMain()
tol = 1e-4;
f = @(x) x^2 - 2;
x0 = 1;
xsol = myNewton(f, x0, tol);
end
function x = myNewton(f, x0, tol)
dx = 1e-8;
df = @(x,dx) (f(x + dx/2) - f(x - dx/2))/dx;
x = x0;
while abs(f(x))>tol
x = x - f(x)/df(x,dx);
end
end

1 件のコメント

R(i) is the Newton estimation of the root of f for the i-th iteration.
E(i) is the value of abs(f(R(i))) for the i-th iteration of the Newton method.
I understand how to get get a value for Newton's Method, but I am uncertain how to iterate the code until I obtain a value that is less than the tolerance.

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

Jammi
Jammi 2022 年 9 月 23 日
function myMain()
tol = 1e-4;
f = @(x) x^2 - 2;
x0 = 1;
xsol = myNewton(f, x0, tol);
end
function x = myNewton(f, x0, tol)
dx = 1e-8;
df = @(x,dx) (f(x + dx/2) - f(x - dx/2))/dx;
x = x0;
while abs(f(x))>tol
x = x - f(x)/df(x,dx);
end
end

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

質問済み:

2014 年 3 月 18 日

回答済み:

2022 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by