CS Lab1 2022 MC 12
CS Lab1 2022 MC 12
Example1
Mfile code
X0=[0;0];%initial condiitons are zero
TR=[0 10];%time response RANGE
%t=0:0.1:50;
[t,x]=ode45(@func1,TR,X0);
Displacement=x(:,1)
Velocity=x(:,2)
plot(t, Displacement)
hold on
plot(t,Velocity)
acceleration = diff(Velocity);
hold on
plot(t,[0;acceleration])
ylabel('x,v,a')
xlabel('time')
legend("displacement","velocity","acceleration");
Mfile Code
clc;
TR = [0 10];
X0 = [0;6;-1;3];
[t,y]=ode45(@fun,TR,X0);
x1=y(:,1);
v1=y(:,2);
x2=y(:,3);
v2=y(:,4);
a1=gradient(v1);
a2=gradient(v2);
subplot(2,3,1)
plot(t,x1)
xlabel('time')
ylabel('Displacement-1')
subplot(2,3,2)
plot(t,v1)
xlabel('time')
ylabel('Velocity-1')
subplot(2,3,3)
plot(t,a1)
xlabel('time')
ylabel('Acceleration-1')
subplot(2,3,4)
plot(t,x2)
xlabel('time')
ylabel('Displacement-2')
subplot(2,3,5)
plot(t,v2)
xlabel('time')
ylabel('Velocity-2')
subplot(2,3,6)
plot(t,a2)
xlabel('time')
ylabel('Acceleration-2')
Simscape
Simulink
Transfer Function Obtainned
Example6
Mfile
clc;
TR=0:0.01:10;
x0=[0;0;0;0;0];
[t,x]=ode45(@Task2Fun,TR,x0);
x1=x(:,1);
x1_dot=x(:,2);
x1_ddot=gradient(x1_dot)./gradient(t);
x2=x(:,3);
x2_dot=x(:,4);
x2_ddot=gradient(x2_dot)./gradient(t);
subplot(2,3,1);
plot(t,x1);xlabel('time');ylabel('x1');
subplot(2,3,2);
plot(t,x1_dot);xlabel('time');ylabel('x1 dot');
subplot(2,3,3);
plot(t,x1_ddot);xlabel('time');ylabel('x1 double dot')
subplot(2,3,4);
plot(t,x2);xlabel('time');ylabel('x2');
subplot(2,3,5);
plot(t,x2_dot);xlabel('time');ylabel('x2 dot');
subplot(2,3,6);
plot(t,x2_ddot);xlabel('time');ylabel('x2 double dot')
%%%%%%%%%%%%%%%%%%%%
function dy=Task2Fun(t,y)
f=1;
dy(1)=y(2);
dy(2)=1/4*(f - 8*y(2) - 5*y(1) + 8*y(4) + 5*y(5));
dy(3)=y(4);
dy(5)=1/4*(5*y(1) + 4*y(4) - 5*y(5));
dy(4)=1/4*(8*y(2) + 4*dy(5) - 16*y(4));
dy=dy';
end
Simulink
Simscape
Transfer Function Obtained
Self Example
Mfile
function solve_ode_system
tspan = [0 10];
x0 = [0; 0; 0; 0];
[t, X] = ode45(@system_odes, tspan, x0);
x2 = X(:, 3);
v2 = X(:, 4);
a2 = acceleration(X);
function a2 = acceleration(X)
m = 3;
a2 = (1/m) * (-0.04 * X(:, 4) - 4 * X(:, 3) + 2 * X(:, 1));
end
Simulink
Simscape
Transfer Function Obtained