Plotting the tangent line for newton raphson method
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Patrick Ofenloch
le 3 Déc 2021
Commenté : Patrick Ofenloch
le 3 Déc 2021
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.
0 commentaires
Réponse acceptée
Alan Stevens
le 3 Déc 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 ***************')
disp(' x y=f(x) ')
disp([ x y ])
format short
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Parallel Computing 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!
