Plotting the tangent line for newton raphson method

4 Ansichten (letzte 30 Tage)
How can I plot the tangent lines for the newton raphson method?
I tried it this way... (have a closer look at line 33 - 38)
% Nullstellen von
%
% y(x) = x^2 - 2
%
clear, clc
fx = @(x) x.^2 -2
dfdx = @(x) 2*x
n_step = 7 ; % Anzahl der durchzuführenden Schritte für Newtonverfahren
x0 = 5 ; % Startwert
%%%% newton-verfahren %%%%%%%%%%%%
x = zeros(n_step+1,1) ;
x(1) = x0 ;
for i=1:n_step
x(i+1) = - fx(x(i)) / dfdx(x(i)) + x(i) ;
end
y = fx(x) ; % zugehörige y-werte / Daten zum Plotten
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xx = [1 : .01 : 5] ; % Daten zum plotten blaue Linie
yy = fx(xx) ; % Daten zum plotten blaue Linie
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:3
yt(j+1) = dfdx(x(j)) * (x(j)-x(j))+fx(x(j))
xt(j+1) = -fx(x(j))/dfdx(x(j)) +x(j)
end
figure(1), clf
subplot(2,1,1), grid on, hold on
plot(xx,yy)
plot(x,y,'r--*')
title('Newton-Raphson-Verfahren')
legend('y=x^2-2','Newton-Verfahren','location','northwest')
subplot(2,1,2), grid on, hold on
plot(xx,yy)
plot(x,y,'r*')
plot(xt,yt,'r.-')
title('Newton-Raphson-Verfahren')
legend('y=x^2-2','Newton-Verfahren','Tangenten','location','northwest')
%%%%% bildschirmausgabe *****************
format long
disp('************ Newton-Verfahren ***************')
disp(' x y=f(x) ')
disp([ x y ])
format short
The tangents should be generated in the secon for slope. (line 33 - 38)
After that, i want to plot it in the second subplot.
exception:
Thanks for helping me.

Akzeptierte Antwort

Alan Stevens
Alan Stevens am 3 Dez. 2021
Like this?
fx = @(x) x.^2 -2;
dfdx = @(x) 2*x;
n_step = 7 ; % Anzahl der durchzuführenden Schritte für Newtonverfahren
x0 = 5 ; % Startwert
%%%% newton-verfahren %%%%%%%%%%%%
x = zeros(n_step+1,1) ;
x(1) = x0;
for i=1:n_step
x(i+1) = - fx(x(i)) / dfdx(x(i)) + x(i) ;
end
y = fx(x) ; % zugehörige y-werte / Daten zum Plotten
% Tangent line calcuations
xt = zeros(2*n_step+1,1);
yt = zeros(2*n_step+1,1);
xt(1) = x(1); yt(1) = y(1);
for i = 1:n_step
xt(2*i) = x(i+1);
xt(2*i+1) = x(i+1);
yt(2*i) = 0;
yt(2*i+1) = fx(x(i+1));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xx = 1 : .01 : 5 ; % Daten zum plotten blaue Linie
yy = fx(xx) ; % Daten zum plotten blaue Linie
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1), clf
subplot(2,1,1), grid on, hold on
plot(xx,yy)
plot(x,y,'r--*')
title('Newton-Raphson-Verfahren')
legend('y=x^2-2','Newton-Verfahren','location','northwest')
subplot(2,1,2), grid on, hold on
plot(xx,yy)
plot(x,y,'r*')
plot(xt,yt,'r.-')
title('Newton-Raphson-Verfahren')
legend('y=x^2-2','Newton-Verfahren','Tangenten','location','northwest')
%%%%% bildschirmausgabe *****************
format long
disp('************ Newton-Verfahren ***************')
************ Newton-Verfahren ***************
disp(' x y=f(x) ')
x y=f(x)
disp([ x y ])
5.000000000000000 23.000000000000000 2.700000000000000 5.290000000000001 1.720370370370370 0.959674211248285 1.441455368177650 0.077793578448165 1.414470981367771 0.000728157131505 1.414213585796884 0.000000066252480 1.414213562373095 0.000000000000000 1.414213562373095 -0.000000000000000
format short

Weitere Antworten (0)

Kategorien

Mehr zu Parallel Computing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by