# Plotting points of intersection using Newton Raphson

10 vues (au cours des 30 derniers jours)
Jane Smith le 18 Avr 2021
Commenté : Jane Smith le 18 Avr 2021
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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

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' )
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Jane Smith le 18 Avr 2021
Worked perfectly thank you!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Newton-Raphson Method dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by