![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175955/image.png)
how to plot a tangent line of specific point (x,y) of function y=x^2+2?
82 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tsvi Weiss
le 10 Déc 2016
Réponse apportée : Image Analyst
le 10 Déc 2016
I trying to obtain the tangent equation and draw the line from specific points (x,y) of the function y=x^2+2 and show them on a figure.
0 commentaires
Réponse acceptée
Image Analyst
le 10 Déc 2016
Try this:
x = linspace(-2, 2, 500);
y = x .^ 2 + 2;
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
fontSize = 20;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
ylim([0, 7]);
% Pick a point at -1 (or wherever) and plot the tangent
xTangent = -1;
% Equation of the tangent is y = slope*x + offset
% From the equation y=x^2 we know the slope is 2*x.
% Find the slope at x = xTangent.
slope = 2 * xTangent;
% We want to plot the tangent line where it just touches the curve,
% so we need to know the y value at xTangent.
yTangent = xTangent .^ 2 + 2; % Y value of curve at x = xTangent.
hold on;
plot(xTangent, yTangent, 'r*', 'LineWidth', 2, 'MarkerSize', 10);
% Use point slope formula of a line to get equation for y
% y-y0 = slope*(x-x0).
% y = slope * (x - xTangent) + yOffset
yTangentLine = slope * (x - xTangent) + yTangent;
plot(x, yTangentLine, 'b-', 'LineWidth', 2);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175955/image.png)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interpolation 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!