Intersection of two functions
Afficher commentaires plus anciens
Hi, I've been trying to solve a problem using the newton-raphson method for the intersection of two functions. I have a matlab code to find the roots of simpler functions which I have been trying to adapt to the problem. this is my code:
function root = newton(x,f,df,imax,tol)
% Input: x initial guess at the root
% f function to be evaluated
% df derivative of f with respect to x
% imax iteration maximum
% tol convergence criterion
% Output: root position of the root
%intialise parameters
xold=x;
fold=feval(f,xold);
count=0;
for i=0:imax
count=count+1;
fold=feval(f,xold);
dfold=feval(df,xold);
% Newton's algorithm
xnew = xold -(fold/dfold);
% CHECK: verify that derivative is not zero.
while norm(fold)<tol
if df == 0
error('Problem 0 gradient')
end
% calculate equation 4
% CHECK: Excessive iterations
% test for convergence
if norm(fold)<tol
root=xnew;
return
end
end
xold=xnew;
end
error('max iterations reached')
end
I've been running this code where function f is
function z=trial(x)
z=(100.*exp(-x))-(5.*(sin((pi/2).*x)));
end
and function df is
function w=trial2(x)
w=(-100.*exp(-x))-((5/2).*cos((pi/2).*x));
end
My code is running however its reaching my max number of iterations and I'm not sure how to resolve this problem.
Thanks in advance
Réponse acceptée
Plus de réponses (1)
GG
le 16 Mar 2015
0 votes
Catégories
En savoir plus sur Newton-Raphson Method dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!