| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- clc; clear all; close all;
- % Constants
- g = 9.81;
- L = 2;
- omega = 1.5;
- A = 0.4;
- initial_angle = deg2rad(20);
- initial_v = 0;
- initial_conditions = [initial_angle; initial_v];
- t_span = [0 10];
- length_time = @(l0, t, A, omega) l0 + A*cos(omega*t);
- dlength_dtime = @(t, A, omega) -A*omega*sin(omega*t);
- length_span = length_time(L, t_span, A, omega);
- ode_system = @(t, theta) [
- theta(2);
- -(g/length_time(L, t, A, omega))*sin(theta(1)) - (2*dlength_dtime(t, A, omega)/length_time(L, t, A, omega))*theta(2)
- ];
- [t, theta] = ode45(ode_system, t_span, initial_conditions);
- theta_sol = theta(:, 1);
- dthetadt_sol = theta(:, 2);
- figure;
- plot(t, theta_sol);
- %-------------------------------------------------
- % Pendulum's base position
- x0 = 0; y0 = 0;
- % Animation loop
- figure;
- axis equal;
- xlim([-L, L]); % Set x-axis limits
- ylim([-L, 0.5]); % Set y-axis limits
- xlabel('x (m)');
- ylabel('y (m)');
- title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
- for i = 1:length(t)
- % Update pendulum position
- x = x0 + length_span(i)*sin(theta_sol(i));
- y = y0 - length_span(i)*cos(theta_sol(i));
- hold on;
- % string
- plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
- % ball
- plot(x, y, 'ro', 'MarkerSize', 20);
- drawnow;
- pause(0.1);
- hold off;
- cla;
- end
|