Guest User

Untitled

a guest
May 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.80 KB | None | 0 0
  1. close all;
  2. clear all;
  3.  
  4.  
  5. %% Symbolic way. It works. But it's not really what Matlab was designed for. Use Maple or Mathematica, etc.
  6. syms time;
  7. syms position;
  8. position = time.^3 - 3*time.^2 - 15*time + 40;
  9. velocity = diff(position, time);
  10. acceleration = diff(velocity, time);
  11. jerk = diff(acceleration, time);
  12. % All in differen sub plots.
  13. figure(1);
  14. tic
  15. subplot(221);
  16. ezplot(position,[0 10]);
  17. title('Position vs Time');
  18. xlabel('Time (s)');
  19. ylabel('Position (m)');
  20. subplot(222);
  21. ezplot(velocity, [0 10]);
  22. title('Velocity vs Time');
  23. xlabel('Time (s)');
  24. ylabel('Position (m/s)');
  25. subplot(223);
  26. ezplot(acceleration, [0 10]);
  27. title('Acceleration vs Time');
  28. xlabel('Time (s)');
  29. ylabel('Position (m/s^2)');
  30. subplot(224);
  31. ezplot(jerk, [0 10]);
  32. title('Jerk vs Time');
  33. xlabel('Time (s)');
  34. ylabel('Position (m/s^3)');
  35. toc
  36. figure(2);
  37.  
  38. % All on one plot
  39. ezplot(velocity, [0 10]);hold all
  40. ezplot(acceleration, [0 10]);hold all
  41. ezplot(jerk, [0 10]);hold all
  42. ezplot(position,[0 10]); hold all % Put last because EZ plot screws with axes
  43. legend('Position (m)','Velocity (m/s)','Acceleration (m/s^2)','Jerk (m/s^3)');
  44. xlabel('Time (s)');
  45.  
  46. %% Closer to the a good MatLab way.
  47. figure(3);
  48. tic
  49. time=0:0.01:10;
  50. position1=subs(position);
  51. velocity1=subs(velocity);
  52. acceleration1=subs(acceleration);
  53. jerk1=subs(jerk);
  54. plot(time,position1,time,velocity1,time,acceleration1,time,jerk1);
  55. legend('Position (m)','Velocity (m/s)','Acceleration (m/s^2)','Jerk (m/s^3)');
  56. xlabel('Time (s)');
  57. toc
  58.  
  59.  
  60. %% IHO the best.
  61. figure(4);
  62. clear position time;
  63. tic
  64. dt=1e-3;
  65. time=0:dt:10;
  66. position = time.^3 - 3*time.^2 - 15*time + 40;
  67. velocity = diff(position)/(dt)^1;
  68. acceleration=diff(position,2)/(dt)^2;
  69. jerk=diff(position,3)/(dt)^3;
  70. plot(time,position,time(2:end),velocity,time(3:end),acceleration,time(4:end),jerk);
  71. toc
Add Comment
Please, Sign In to add comment