Newton Method using Matlab Code

33 ビュー (過去 30 日間)
Rohit Sil
Rohit Sil 2019 年 1 月 30 日
コメント済み: Oguz ODABAS 2020 年 9 月 14 日
Hey guys so i attempted to program the newton iteration
f is the function, f_prime the derivative, x_0 is the start value, epsilon is the stop criteria
I tried this with newton(sin(i),cos(i),x_0,10.^(-6)) but got the error message:
Index exceeds matrix dimensions.
Error in newton (line 4)
while abs(f(nullstelle(i))<epsilon);
Matlab Code:
function [x] = newton(f,f_prime,x_0,epsilon)
x(1) = x_0;
i = 1;
while abs(f(x(i))<epsilon);
x(i+1) = x(i) - f(x(i))/f_prime(x(i))
i = i + 1;
end
2 Questions - firstly does the code make sense. Secondly if i want to run the function, does my command make sense?

採用された回答

Matt J
Matt J 2019 年 1 月 30 日
編集済み: Matt J 2019 年 1 月 30 日
You need to pass function handles to the objective and its derivative,
newton(@sin,@cos,x_0,10.^(-6))
and the while loop continuation criterion should look like
while abs(f(x(i)) > epsilon
end
  3 件のコメント
Rohit Sil
Rohit Sil 2019 年 1 月 31 日
@Matt, one more question
if i want to enter a function like the one below.
f(x) = x.^5 − 14x.^4 + x.^2 − 3x + 5
how would that be done? also through function handles?
I tried it as it is and got a message saying that x is undefined...
Jan
Jan 2019 年 1 月 31 日
@Rohit: Exactly, you need function handles again:
f = @(x) x.^5 - 14 * x.^4 + x.^2 - 3 * x + 5;
and equivalently for the derivative.

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

その他の回答 (1 件)

Oguz ODABAS
Oguz ODABAS 2020 年 9 月 14 日
f = inline (x^4 + 2*x^3-23*x^2+12*x+36);
fd = inline (4*x^3 + 6*x^2 - 46*x + 12);
x = -10:0.01:10;
y = f(x);
x0 = -10;
while abs (f(x0))> 1.0e-6;
x1 = x0 - (f(x0)/fd(x0));
x0 = x1;
end
  1 件のコメント
Oguz ODABAS
Oguz ODABAS 2020 年 9 月 14 日
error: for x^y, only square matrix arguments are permitted and one argument must be scalar. Use .^ for elementwise p
ower.
error: called from
Octave13 at line 1 column 3

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by