Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- close all;
- clear all;
- %% Symbolic way. It works. But it's not really what Matlab was designed for. Use Maple or Mathematica, etc.
- syms time;
- syms position;
- position = time.^3 - 3*time.^2 - 15*time + 40;
- velocity = diff(position, time);
- acceleration = diff(velocity, time);
- jerk = diff(acceleration, time);
- % All in differen sub plots.
- figure(1);
- tic
- subplot(221);
- ezplot(position,[0 10]);
- title('Position vs Time');
- xlabel('Time (s)');
- ylabel('Position (m)');
- subplot(222);
- ezplot(velocity, [0 10]);
- title('Velocity vs Time');
- xlabel('Time (s)');
- ylabel('Position (m/s)');
- subplot(223);
- ezplot(acceleration, [0 10]);
- title('Acceleration vs Time');
- xlabel('Time (s)');
- ylabel('Position (m/s^2)');
- subplot(224);
- ezplot(jerk, [0 10]);
- title('Jerk vs Time');
- xlabel('Time (s)');
- ylabel('Position (m/s^3)');
- toc
- figure(2);
- % All on one plot
- ezplot(velocity, [0 10]);hold all
- ezplot(acceleration, [0 10]);hold all
- ezplot(jerk, [0 10]);hold all
- ezplot(position,[0 10]); hold all % Put last because EZ plot screws with axes
- legend('Position (m)','Velocity (m/s)','Acceleration (m/s^2)','Jerk (m/s^3)');
- xlabel('Time (s)');
- %% Closer to the a good MatLab way.
- figure(3);
- tic
- time=0:0.01:10;
- position1=subs(position);
- velocity1=subs(velocity);
- acceleration1=subs(acceleration);
- jerk1=subs(jerk);
- plot(time,position1,time,velocity1,time,acceleration1,time,jerk1);
- legend('Position (m)','Velocity (m/s)','Acceleration (m/s^2)','Jerk (m/s^3)');
- xlabel('Time (s)');
- toc
- %% IHO the best.
- figure(4);
- clear position time;
- tic
- dt=1e-3;
- time=0:dt:10;
- position = time.^3 - 3*time.^2 - 15*time + 40;
- velocity = diff(position)/(dt)^1;
- acceleration=diff(position,2)/(dt)^2;
- jerk=diff(position,3)/(dt)^3;
- plot(time,position,time(2:end),velocity,time(3:end),acceleration,time(4:end),jerk);
- toc
Add Comment
Please, Sign In to add comment