0% found this document useful (0 votes)
9 views31 pages

CS Lab1 2022 MC 12

Uploaded by

Hashir Toheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views31 pages

CS Lab1 2022 MC 12

Uploaded by

Hashir Toheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CS_LAB_1

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");

%state variables give the following function


function dx = func1(t,x)
M=10;B=5;K=5;F=5;
dx(1)=x(2)%for x dot
dx(2)=(F-B*x(2)-K*x(1))/M %for x dot dot
dx = dx';
end
Graphs

Simulink and Graph


Simscape
Transfer Function Obtained
Example 2

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')

function dy= fun(t,y)


f=5;
m1=1;
m2=4;
k1=3;
k2=2;
k3=1;
fv1=0.03;
fv2=0.02;
fv3=0.01;
dy(1)=y(2);
dy(3)=y(4);
dy(2)=1/m1*(f-(k1+k2)*y(1)-(fv1+fv3)*y(2)+fv3*y(4)+k2*y(3));
dy(4)=1/m2*(-(fv2+fv3)*y(4)-(k2+k3)*y(3)+k2*y(1)+fv3*y(2));
dy=dy';
end
Simulink
Simscape
Transfer Function Obtained
Example5

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);

plot(t, x2, 'b', t, v2, 'r', t, a2, 'g', 'LineWidth', 1.5);


xlabel('Time (s)');
ylabel('Response');
legend('Displacement', 'Velocity', 'Acceleration');
title('');
grid on;
end

function dXdt = system_odes(~, X)


x1 = X(1);
v1 = X(2);
x2 = X(3);
v2 = X(4);
m = 3;
a1 = (1/m) * (5 - 0.02 * v1 - 2 * x1 + 2 * x2);
a2 = (1/m) * (-0.04 * v2 - 4 * x2 + 2 * x1);
dXdt = [v1; a1; v2; a2];
end

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

You might also like