Several tangent vectors on curve

34 Ansichten (letzte 30 Tage)
Viktor Karlsson
Viktor Karlsson am 14 Mai 2021
Kommentiert: Viktor Karlsson am 17 Mai 2021
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.

Akzeptierte Antwort

the cyclist
the cyclist am 14 Mai 2021
Bearbeitet: the cyclist am 14 Mai 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 Kommentare
the cyclist
the cyclist am 14 Mai 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 am 17 Mai 2021
I understand, thank you helping!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by