# Newton Raphson to find intersection of graphs

6 views (last 30 days)
Jane Smith on 22 Mar 2021
Answered: David Hill on 22 Mar 2021
So after i plotted the functions f=cos(x)+1 and g=sin(x) and tried to mark where the graphs intersect using this code
x=0.5;
x_old=0;
i=0;
format short
while abs(x-x_old)>.01
i=i+1;
x_old=x;
x=x-((cos(x)+1)-sin(x));
end
display(i,'iterations')
display(x,'x_which satisfies_the equation')
plot([x], [0], '*r' )
plot([x], [1], '*r' )
The points started coming on top and bottom of the image of the graph not on the points themselves but if i remove
plot([x], [1], '*r' )
Then not all points appear.

David Hill on 22 Mar 2021
Why not just use fzero()
f=@(x)1+cos(x);
g=@(x)sin(x);
h=@(x)1+cos(x)-sin(x);
interval=fzero(h,1)-fzero(h,-5);
p=sort([fzero(h,-5):interval:8,fzero(h,-3):interval:8]);
x=-8:.01:8;
plot(x,f(x),x,g(x),p,f(p),'r*');

### Categories

Find more on Newton-Raphson Method in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by