Newton Raphson to find intersection of graphs
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
David Hill
2021-3-22
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*');
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!