Effacer les filtres
Effacer les filtres

Newton's Method for a polynomial equation

1 vue (au cours des 30 derniers jours)
Dorothy  Carter
Dorothy Carter le 12 Sep 2017
Commenté : Dorothy Carter le 12 Sep 2017
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?

Réponse acceptée

James Tursa
James Tursa le 12 Sep 2017
Modifié(e) : James Tursa le 12 Sep 2017
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;

Plus de réponses (0)

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by