Several tangent vectors on curve

34 views (last 30 days)
Hello,
I have the following curve plotted:
Now, I want 10 tangent vectors along this curve and I can't seem to figure out how to write the code for this to work. I wrote the following:
t = linspace(-2.5,2.5,100);
x = t.^3 - 4*t;
y = t.^2;
plot(x,y,'LineWidth',2)
axis equal
hold on
i=1:10:100;
ts=t(i);
xs=x(i);
ys=y(i);
tx = ts.^3 -4*ts;
ty = ts.^2;
quiver(tx,ty,xs,ys,'LineWidth',2,'Color','r')
hold off
and the result is.. not quite it:
Any help or advice is appreciated.

Accepted Answer

the cyclist
the cyclist on 14 May 2021
Edited: the cyclist on 14 May 2021
Well, you never actually calculated the tangent, so it is not that surprising you didn't get the correct result.
t = linspace(-2.5,2.5,100);
x = t.^3 - 4*t;
y = t.^2;
dydt = 2*t;
dxdt = 3*t.^2 - 4;
dydx = dydt./dxdt;
i=1:10:100;
ts=t(i);
xs=ones(size(i));
ys=dydx(i);
tx = ts.^3 -4*ts;
ty = ts.^2;
figure
hold on
plot(x,y,'LineWidth',2)
quiver(tx,ty,xs,ys,'LineWidth',2,'Color','r')
axis equal
Apologies for rearranging your code to my liking. See this page for the math of calculating the tangent of a parametric equation. (You'll need to be careful if dx/dt is ever zero, because you divide by that.)
Also, I was lazy and just set the length of the x-component of the tangent vector to (positive) 1, then calculated the y-component. You probably want something less lazy.
  3 Comments
the cyclist
the cyclist on 14 May 2021
As I mentioned, I always defined the x-component of the tangent vector as +1; therefore, it will always be toward the right.
The parametric equation has no inherent "direction", and the tangent line extends in both directions from any given point.
Viktor Karlsson
Viktor Karlsson on 17 May 2021
I understand, thank you helping!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!