MATLAB Answers

0

why the script show wrong output can you help me?

Asked by Khulood Alhashmi on 11 Oct 2019
Latest activity Edited by Matt J
on 13 Oct 2019
Recall from Physics that, given a position function of a curve in three-dimensional space, the first derivative represents the velocity and the second derivative represents the acceleration at any given time t.
Given the curve r( t ) = < 2*cos( t ), 2*sin( t ), 3*t >, find the position vector pos, velocity vector v, and acceleration vector a at t = 3*pi/2. Then plot the curve using the plot3command over the interval tplot=[0:0.01:2*pi]; and use the code given to view the position, velocity, and acceleration vectors.
% Define variables and functions
syms t;
f=@(t)2*cos(t);
g=@(t)2*sin(t);
h=@(t)3*t;
r=[f(t) g(t) h(t)];
% Find position, velocity, and acceleration vectors
pos=subs(r) % Evaluate at t=3*pi/2
dr=diff(r); % First derivative
v=subs(dr) % Evaluate at t=3*pi/2
d2r=diff(dr); % Second derivative
a=subs(d2r) % Evaluate at t=3*pi/2
%
% Plot curve
tplot=[0:0.01:2*pi];
x=pos;
y=v;
z=a;
p=plot3(x,y,z);
%
% LEAVE ALL REMAINING CODE AS IS
% Plot position from origin and vel/accel from point on curve
hold on
plot3([0 pos(1)],[0 pos(2)],[0 pos(3)],'-k','LineWidth',1); % Plots a line from the origin to the position point
plot3(pos(1),pos(2),pos(3),'*'); % Plots a symbol at the end of the line
plot3([pos(1) pos(1)+v(1)],[pos(2) pos(2)+v(2)],[pos(3) pos(3)+v(3)],'-r','LineWidth',1); % Plots the velocity line starting at the position point
plot3(pos(1)+v(1),pos(2)+v(2),pos(3)+v(3),'*');
plot3([pos(1) pos(1)+a(1)],[pos(2) pos(2)+a(2)],[pos(3) pos(3)+a(3)],'-b','LineWidth',1); % Plots the acceleration line starting at the position point
plot3(pos(1)+a(1),pos(2)+a(2),pos(3)+a(3),'*');
legend('curve', 'position', '', 'velocity', '', 'acceleration','');
OUTPUT
pos =
[ 2*cos(t), 2*sin(t), 3*t]
v =
[ -2*sin(t), 2*cos(t), 3]
a =
[ -2*cos(t), -2*sin(t), 0]
Error using plot3
Data must be numeric, datetime, duration or an array convertible to double.
Error in solution (line 20)
p=plot3(x,y,z);

  0 Comments

Sign in to comment.

1 Answer

Answer by Matt J
on 11 Oct 2019
Edited by Matt J
on 11 Oct 2019

tplot=[0:0.01:2*pi];
x=double(subs(pos,tplot));
y=double(subs(v,tplot));
z=double(subs(a,tplot));
p=plot3(x,y,z);

  4 Comments

Show 1 older comment
It's the same issue. pos(1), pos(2) etc... need to be converted to doubles.
Can you show me how can I convert it to double please?
See here.

Sign in to comment.