problem1_drivenpendulum.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. % Enormous portions of this code were borrowed from code written while
  2. % following along in class.
  3. clc; clear all; close all;
  4. % Constants
  5. g = 9.81;
  6. L = 1;
  7. F0 = 10;
  8. c = 0.5;
  9. m = 1;
  10. initial_angle = deg2rad(70);
  11. initial_v = 0;
  12. initial_conditions = [initial_angle, initial_v];
  13. t_span = [0 100];
  14. % Driving frequencies for each test:
  15. omega1 = 1;
  16. omega2 = 1.9;
  17. % Setting up my ODE vectors manually again
  18. ode_system1 = @(t, theta) [
  19. theta(2); % dtheta/dt
  20. (F0*cos(omega1*t) - m*g*L*sin(theta(1)) - c*theta(2))/(m*L^2) % dtheta2/d2t
  21. ];
  22. ode_system2 = @(t, theta) [
  23. theta(2); % dtheta/dt
  24. (F0*cos(omega2*t) - m*g*L*sin(theta(1)) - c*theta(2))/(m*L^2) % dtheta2/d2t
  25. ];
  26. % Test 1, using omega1
  27. [t1, theta1] = ode45(ode_system1, t_span, initial_conditions);
  28. theta_sol1 = theta1(:, 1);
  29. dthetadt_sol1 = theta1(:, 2);
  30. f1 = figure;
  31. ax1 = axes(f1);
  32. plot(ax1, t1, theta_sol1);
  33. title(ax1, sprintf("Test 1, using initial angle %.0f deg and driving freq %.0f rad/s", initial_angle, omega1));
  34. xlabel(ax1, "Time (s)");
  35. ylabel(ax1, "Theta (rad)");
  36. % Test 2, using omega2
  37. [t2, theta2] = ode45(ode_system2, t_span, initial_conditions);
  38. theta_sol2 = theta2(:, 1);
  39. dthetadt_sol2 = theta2(:, 2);
  40. f2 = figure;
  41. ax2 = axes(f2);
  42. plot(ax2, t2, theta_sol2);
  43. title(ax2, sprintf("Test 2, using initial angle %.0f deg and driving freq %.0f rad/s", initial_angle, omega2));
  44. xlabel(ax2, "Time (s)");
  45. ylabel(ax2, "Theta (rad)");
  46. %-------------------------------------------------
  47. % Pendulum's base position
  48. x0 = 0; y0 = 0;
  49. % Animation loop
  50. f3 = figure;
  51. ax3 = axes(f3);
  52. axis equal;
  53. xlim(ax3, [-L, L]);
  54. ylim(ax3, [-L, 0.5]);
  55. xlabel(ax3, 'x (m)');
  56. ylabel(ax3, 'y (m)');
  57. % We're using test 2 results for the animation
  58. % Gotta render one frame first to get handles for the plots
  59. x = x0 + L*sin(theta_sol2(1));
  60. y = y0 - L*cos(theta_sol2(1));
  61. hold on;
  62. plot_string = plot(ax3, [x0,x], [y0,y], 'k-', 'LineWidth', 2);
  63. plot_ball = plot(ax3, x, y, 'ro', 'MarkerSize', 20);
  64. for i = 1:length(t2)
  65. % Update title
  66. title(ax3, sprintf("Pendulum Animation (Initial Angle = %.0f deg)\nTheta = %f\nTime = %f",...
  67. rad2deg(initial_angle),...
  68. theta_sol2(i),...
  69. t2(i)...
  70. ), 'FontSize',12);
  71. % Update pendulum position
  72. x = x0 + L*sin(theta_sol2(i));
  73. y = y0 - L*cos(theta_sol2(i));
  74. hold on;
  75. set(plot_string, 'XData', [x0,x], 'YData', [y0,y]);
  76. set(plot_ball, 'XData', x, 'YData', y);
  77. drawnow;
  78. pause(0.05);
  79. end