1. clear all; close all; clc;
tspan=linspace(0,2.100) % time span ranges from 0 to 15 sec
% Defined the Inintial conditions
x0=[6;-1;1;1] % x1(0), x2(0), x1prime(0), x2prime(0);
%ode code
[t,x]= ode45(@odefun,tspan,x0) %odefun-name of function
%make plots
plot(t,x(:,1)) % first degree
hold on % add second plot
plot(t,x(:,2))% plot second degree
legend ('x1-cart','x2-pendulum') % created the legends on
the graph
% step 4: creating the derivaticce function
function dx2dt= odefun(t,x) % argument are t, x
% we defiend the parameters values
m1 = 0.5; % mass of cart
m2 = 0.2; % mass of pendulum
L=0.3;
Izz =0.006;
g = 9.81;
f=1
p1 = ((m1+m2)*(Izz+(m2*(L^2))))-((m2^2)*(L^2));
% initializing the column vector for the output derivative
dx2dt= zeros(4,1);
dx2dt(1)=x(3);
dx2dt(2)=x(4);
dx2dt(3)= (f/(m1+m2))-((((m2*L)^2)*g)/p1)*x(2); % x double
derivative equation
dx2dt(4)=m2*g*L*x(2) -((m2*L*f)/(m1+m2))/p1 ; % Phi double
derivative equation
% dx2dt is the output to our ode function
end