% Sample problem: 
%
% Consider the curve with parametrization r = (t, t^2, t^3) for t in [-1,1].
% Find the speed as expression of t.
% Plot the curve using tubeplot3, together with the vectors T,N for t=0.5.

% IMPORTANT: Use semicolon so that you do not print out arrays like tv below !!

syms t real             % define t as real symbolic variable

r = [t, t^2, t^3]                   

v = diff(r,t)           % velocity: differentiate r with respect to t
a = diff(v,t)           % acceleration: differentiate v with respect to t
speed = sqrt(dot(v,v))  % need to use sqrt(dot(v,v)) for norm of symbolic vector

t0 = 0.5;               % find r,v,a for t0=0.5
r0 = subs(r,t,t0);
v0 = subs(v,t,t0);
a0 = subs(a,t,t0);      % decompose a = apar + aperp with vectors apar,aperp parallel and perpendicular to v
apar = dot(a0,v0)/dot(v0,v0)*v0; 
aperp = a0 - apar; 

T0 = v0/norm(v0);       % T0 = v0/||v0||
N0 = aperp/norm(aperp); % N0 = aperp/||aperp||

tv = linspace(-1,1,100);   % use 100 equidistant t-values between -1 and 1 for plotting
tubeplot3(tv,tv.^2,tv.^3); % must use .*  ./  .^  instead of * / ^
hold on
arrow3(r0,T0,'b')       % plot blue vector T 
arrow3(r0,N0,'r')       % plot red vector N
hold off
 
nice3d                  % you can rotate plot with mouse
