%%%use numerical integration methods like the Runge-Kutta method.
Below is the MATLAB code to
accomplish this%%%
% All Defined parameters according to the question
m1 = 1; % kg
r1 = 1; % m
I1 = 1; % kg-m^2
l1 = 2; % m
m2 = 0.5; % kg
g = 9.81; % m/s^2
% Define control inputs
tau1 = @(t) 10 * (cos(t)).^2 + 4 * sin(t); % N-m
F2 = @(t, d2) -10 * d2; % N
% Define equations of motion
equations_of_motion = @(t, x) [
x(2); % dθ1/dt = theta1_dot
x(4); % dd2/dt = d2_dot
(tau1(t) - m1 * g * r1 * sin(x(1)) - m2 * g * l1 * sin(x(1))) / I1; % d(theta1_dot)/dt = theta1_ddot
(F2(t, x(2)) + m2 * g * cos(x(1))) / m2; % d(d2_dot)/dt = d2_ddot
];
% Set time span
tspan = 0:0.1:20; % seconds
% Set initial conditions
initial_conditions = [0; 0; 0; 0]; % [theta1(0); d2(0); theta1_dot(0); d2_dot(0)]
% Solve the equations of motion numerically
[t, X] = ode45(equations_of_motion, tspan, initial_conditions);
% Extract variables
theta1 = X(:, 1);
d2 = X(:, 2);
theta1_dot = X(:, 3);
d2_dot = X(:, 4);
% Plot results
figure;
subplot(4, 1, 1);
plot(t, theta1);
xlabel('Time (s)');
ylabel('\theta_1 (rad)');
title('\theta_1 vs. Time');
subplot(4, 1, 2);
plot(t, d2);
xlabel('Time (s)');
ylabel('d2 (m)');
title('d2 vs. Time');
subplot(4, 1, 3);
plot(t, theta1_dot);
xlabel('Time (s)');
ylabel('\theta_1_dot (rad/s)');
title('\theta_1_dot vs. Time');
subplot(4, 1, 4);
plot(t, d2_dot);
xlabel('Time (s)');
ylabel('d2_dot (m/s)');
title('d2_dot vs. Time');