| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- clc; clear all; close all;
- c = 0.5; m = 1; g = 9.81; L = 1; initial_angle = deg2rad(30);
- omega = 0.6*sqrt(g/L); f0 = 10;
- ode_system = @(t, theta) [
- theta(2);
- ((-m*g*L*sin(theta(1))) - (c*theta(2)) + (f0*cos(omega*t))) / (m*L^2)
- ];
- t_span = [0, 150]
- initial_conditions1 = [initial_angle, 0];
- [t1, theta1] = ode45(ode_system, t_span, initial_conditions1);
- theta_sol1 = theta1(:, 1);
- dthetadt_sol1 = theta1(:, 2);
- initial_conditions2 = [initial_angle+0.4, 0];
- [t2, theta2] = ode45(ode_system, t_span, initial_conditions2);
- theta_sol2 = theta2(:, 1);
- dthetadt_sol2 = theta2(:, 2);
- figure;
- hold on;
- xlim(t_span);
- plot(t1, theta_sol1, "Color", "red");
- plot(t2, theta_sol2, "Color", "blue");
- legend show;
- %-------------------------------------------------
- % Pendulum's base position
- x0 = 0; y0 = 0;
- % Animation loop
- figure;
- axis equal;
- xlim([-L, L]); % Set x-axis limits
- ylim([-L, L]); % 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(t1)
- % Update pendulum position
- x1 = x0 + L*sin(theta_sol1(i));
- y1 = y0 - L*cos(theta_sol1(i));
- x2 = x0 + L*sin(theta_sol2(i));
- y2 = y0 - L*cos(theta_sol2(i));
- hold on;
- % string
- plot([x0,x1], [y0,y1], 'k-', 'LineWidth', 2);
- plot([x0,x2], [y0,y2], 'k-', 'LineWidth', 2);
- % ball
- plot(x1, y1, 'ro', 'MarkerSize', 20);
- plot(x2, y2, 'bo', 'MarkerSize', 20);
- drawnow;
- pause(0.01);
- hold off;
- cla;
- end
|