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
채택된 답변
추가 답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!