Tangent line of a parametric curve

How do I program MATLAB to determine the equation of a line tangent to a parametric curve?
I see plenty of videos/threads on plotting parametrics, but I havent found any on using MATLAB to perform the deriving of a tangent equation.

 Accepted Answer

The function you have and the result you want are not obvious.
Try this:
t = linspace(0, 2*pi); % Create Curve
y = sin(t); % Create Curve
Point = randi(numel(t)); % Random Point On Curve
dydt = gradient(y) ./ gradient(t); % Derivative
b = y(Point) - dydt(Point)*t(Point); % Intercept
idxv = max(1,Point-10):min(numel(t),Point+10); % Index Vector
Tan = dydt(Point)*t(idxv) + b;
figure
plot(t, y)
hold on
plot(t(idxv),Tan, '-r')
hold off
grid

2 Comments

That seems to work. However, I am looking for MATLAB to spit out the equation, not just plot the tangent.
x=sqrt(t) , y=t^2-2*t , where t=4
Revised:
t = linspace(0, 9); % Create Curve
x = @(t) sqrt(t); % Desired Function
y = @(t) t.^2-2*t; % Desired Function
tv = 4; % Desired Prameter Value
xv = x(tv);
yv = y(tv);
dfx = @(f,x) (f(x+1E-6)-f(x))/1E-6; % Derivative Function
m = dfx(y, tv) / dfx(x, tv); % Slope
b = y(tv) - m*x(tv); % Intercept
TanEqn = sprintf('y = %.4f x %+.4f', m, b)
Tan = m*x(tv+[-1 1]) + b;
figure
plot(x(t), y(t))
hold on
plot(x(tv+[-1 1]), Tan, '-r')
hold off
grid
and:
TanEqn =
'y = 24.0000 x -40.0000'
I changed it to include the functions (and a simple derivative calculation replacing the gradient calls). The plot demonstrates that it works and calculates the appropriate tangent line.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!