Newton's Method for a polynomial equation

8 ビュー (過去 30 日間)
Dorothy  Carter
Dorothy Carter 2017 年 9 月 12 日
コメント済み: Dorothy Carter 2017 年 9 月 12 日
This is my code for the function:
function x=newton(x0,func,grad,xTOL)
%xo=initial guess
% func= function
% grad= derivative of function
% xTOL=tolerance
% solving f(x)=0 using the Newton's method with initial guess x0
%
xerror=xTOL*2;
while xerror>xTOL
x1 = x0 - feval(func,x0)/feval(grad,x0);
xerror = abs(x1-x0);
disp(x1);
x0=x1; % x1= x_k, x0=x_{k-1}
end
x= x1;
end
I called it here:
x0=1;
func=36*x^4-12*x^3+37*x^2-12*x+1;
grad=144*x^3-36*x^2+74*x-12;
xTOL=1*10^-6;
newton(x0,func,grad,xTOL)
But it keeps coming up as x is undefined. Can someone help me. Is there code I am missing from the file where I call the function?

採用された回答

James Tursa
James Tursa 2017 年 9 月 12 日
編集済み: James Tursa 2017 年 9 月 12 日
You need to create function handles for your functions. As written, MATLAB thinks you are trying to evaluate the functions at a currently defined value of x. But x is not defined, hence the error. To fix this, create function handles:
func = @(x) 36*x^4-12*x^3+37*x^2-12*x+1;
grad = @(x) 144*x^3-36*x^2+74*x-12;
  1 件のコメント
Dorothy  Carter
Dorothy Carter 2017 年 9 月 12 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by