how to plot a tangent line of specific point (x,y) of function y=x^2+2?

조회 수: 43 (최근 30일)
Tsvi Weiss
Tsvi Weiss 2016년 12월 10일
댓글: Sam Chak 2025년 4월 19일
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.

채택된 답변

Image Analyst
Image Analyst 2016년 12월 10일
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);
  댓글 수: 2
Sam Chak
Sam Chak 2025년 4월 19일
@Hadgh, This looks like a differential equation and it is unstable because the rate of change is growing exponentially for x.
In fact, such exponential function is also infinitely differentiable. You can take the derivative of the exponential function an infinite number of times, and the result will always be another exponential function.
Wanna try it in MATLAB?
syms f(x)
f = exp(x)
f = 
df = diff(f)
df = 
d2f = diff(df)
d2f = 
d3f = diff(d2f)
d3f = 

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by