Several tangent vectors on curve

34 visualizaciones (últimos 30 días)
Viktor Karlsson
Viktor Karlsson el 14 de Mayo de 2021
Comentada: Viktor Karlsson el 17 de Mayo de 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.

Respuesta aceptada

the cyclist
the cyclist el 14 de Mayo de 2021
Editada: the cyclist el 14 de Mayo de 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 comentarios
the cyclist
the cyclist el 14 de Mayo de 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 el 17 de Mayo de 2021
I understand, thank you helping!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by