problem1_drivenpendulum.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. clc; clear all; close all;
  2. % Constants
  3. g = 9.81;
  4. L = 1;
  5. F0 = 10;
  6. c = 0.5;
  7. m = 1;
  8. initial_angle = deg2rad(70);
  9. initial_v = 0;
  10. initial_conditions = [initial_angle; initial_v];
  11. t_span = [0 100];
  12. % Setting up my ODE vector manually again
  13. % driving frequency for this test:
  14. omega = 1;
  15. ode_system = @(t, theta) [
  16. theta(1); % theta/t
  17. theta(2); % dtheta/dt
  18. (F0*cos(omega*t) - m*g*L*sin(theta) - c*theta(2))/(m*L^2) % dtheta2/d2t
  19. ];
  20. [t, theta] = ode45(ode_system, t_span, initial_conditions);
  21. theta_sol = theta(:, 1);
  22. dthetadt_sol = theta(:, 2);
  23. figure;
  24. plot(t, theta_sol);
  25. %-------------------------------------------------
  26. % Pendulum's base position
  27. x0 = 0; y0 = 0;
  28. % Animation loop
  29. figure;
  30. axis equal;
  31. xlim([-L, L]); % Set x-axis limits
  32. ylim([-L, 0.5]); % Set y-axis limits
  33. xlabel('x (m)');
  34. ylabel('y (m)');
  35. title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
  36. for i = 1:length(t)
  37. % Update pendulum position
  38. x = x0 + L*sin(theta_sol(i));
  39. y = y0 - L*cos(theta_sol(i));
  40. hold on;
  41. % string
  42. plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
  43. % ball
  44. plot(x, y, 'ro', 'MarkerSize', 20);
  45. drawnow;
  46. pause(0.1);
  47. hold off;
  48. cla;
  49. end