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!