MATLAB: Function Intersection using Newton’s Method

intersectionsnewton's methodoutput issues

Hi everyone, I'm trying to write a function that finds a point at
which two functions f(x) and g(x) intersect, i.e., f(x) = g(x). I'm using
Newton's Method and making and h(x)=f(x)-g(x) and h'(x) as well, but I'm not getting the right x-value. Please help me debug my code!
% function x = fgIntersect(f, df, g, dg, x0, tol, maxIter)
h=f(x0)-g(x0)
dh=df(x0)-dg(x0)
k=1;
while k<=maxIter
x=x0-h/dh;
if abs(x-x0)<tol*abs(x)
return
end
x0=x;
k=k+1;
end
end

Best Answer

You're not updating h and dh within your loop.