Plotting points of intersection using Newton Raphson

17 次查看(过去 30 天)
I am creating a raw code for the Newton raphson method using a while loop like so
x=1;
xold=0;
i=0;
format short
while abs(x-xold)>.01
f = sin(x)-cos(x);
f_1 =cos(x)+sin(x);
xnew = x - y/dy;
i = i+1;
xold = x;
end
plot([xnew], [0.719], '*r' )
plot([new], [-0.719], '*r' )
This is after plotting the 2 graphs before cos(x) and sin(x). Now i would like to plot the points of intersection that were found through the newton raphson's method.
The last 2 lines of the code are incorrect as when i tried some different values i was getting 2 points.

采纳的回答

Thiago Henrique Gomes Lobato
Your last line use fixed values, so it will only work for one case. Additionally there was an error in the order of the variable assignment on the loop. Following code should work:
x=0;
xold=inf;
i=0;
format short
while abs(x-xold)>.01
xold = x; % This should be done before the calculation, otherwise you have only 1 iteration
f = sin(x)-cos(x);
f_1 =cos(x)+sin(x);
x = x - f/f_1; % Note f and f_1 instead of y and dy
i = i+1;
end
figure
xplot = linspace(-8,8,100);
plot(xplot,cos(xplot)),hold on
plot(xplot,sin(xplot))
plot([x], cos(x), '*r' )
plot([x], sin(x), '*r' )

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by