Plotting the tangent line for newton raphson method
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!