Plotting points of intersection using Newton Raphson

조회 수: 5 (최근 30일)
Jane Smith
Jane Smith 2021년 4월 18일
댓글: Jane Smith 2021년 4월 18일
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
Thiago Henrique Gomes Lobato 2021년 4월 18일
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개)

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by