Intersection of two functions

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

 Accepted Answer

You missed the pi constant when taking the derivative of sin((pi/2).*x). You only pulled out the 1/2 constant instead of the entire (pi/2) constant. Try
w=(-100.*exp(-x))-(pi*(5/2).*cos((pi/2).*x));

More Answers (1)

GG
GG on 16 Mar 2015
Ah, that actually plots it now. Thank you!

Asked:

GG
on 16 Mar 2015

Answered:

GG
on 16 Mar 2015

Community Treasure Hunt

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

Start Hunting!