PROBLEM 1.1 STATEMENT y(x) = { sin(pix) 0 ≤ x cos(πx

College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
PROBLEM 1.1 STATEMENT
y(x) = {
sin(pix)
0 ≤ x < 250
cos(πx) 250 ≤ x ≤ 1000
A. Plot y(x) for x=0 to 1000?
B. Find the values of y(x) at x =200 and x = 750?
clear all
clc
x=0:1000;
for i=1:length(x)
z(i)=x(i);
if x(i)<250
y(i)=sin(pi*x(i)/180);
else
y(i)=cos(pi*x(i)/180);
end
end
plot(x,y)
xlabel('x');ylabel('y')
title('Y vs X')
%:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
y (200)= -0.3256 and y(750)= 0.8746
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
PROBLEM 1.2 STATEMENT
If A=[1,2,3,4,…….,100], using for loop to A. find
y(A) = A3 + 2A2 + A − 25
B. Plot y(x) of various values of A?
clear all
clc
A=[1:100];
for i=1:length(A)
y(i)=A(i).^3+2*A(i).^2+A(i)-25;
end
plot(A,y)
xlabel('A');ylabel('y')
title ('Y vs A')
grid on
%::::::::::::::::::::::::::::::::::::::::
Problem1.3. Statement
Plot all of those mathematical functions on one figure window for x various from zero to 1000.
1
1
A. y1 (x) = 1 − ex + cos(πx) + 1−x
B. y2 (x) = 1 −
clear all
clc
1
2
ex
1
+ sin(πx) + x−1
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
x=0:1000;
y1=1-1./exp(x)+cos(pi.*x/180)+1./(1-x);
y2=1-1./exp(x.^2)+sin(pi.*x/180)+1./(x-1);
subplot(2,1,1)
plot(x,y1)
xlabel('x');ylabel('y1');title('Y1 vs x')
grid on
subplot(2,1,2)
plot(x,y2)
xlabel('x');ylabel('y2');title('Y2 vs x')
grid on
%::::::::::::::::::::::
PROBLEM 1.4 STATEMENT
The Coefficient of friction (μ), can be determined in an experimental by measuring the Force (F)
required to move a mass (m). When F is measured and m is known, the coefficient of friction can
𝐅
F
𝐦
be calculated by: 𝛍 = 𝐦𝐠 (𝐠 = 𝟗. 𝟖𝟏 𝐬𝟐 )
mg
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Test #
1
2
3
4
5
Mass (m) kg
2
4
5
10
20
Force
12.5
23.5
30
61
117
6
50
294
Plot the friction coefficient vs mass of the block?
clear all
clc
g=9.81;
m=[2 4 5 10 20 50];
F=[12.5 23.5 30 61 117 294];
mu=F./(m*g);
plot(m,mu,'--o')
xlabel('m [kg]');ylabel('Friction Coefficient-mu')
grid on
PROBLEM 1.5 STATEMENT
The piston-connection rod-crank mechanism is used in many engineering applications. In the
mechanism shown in the following figure, the crank is rotating at a constant speed of 500 rpm.
L1=120 mm (piston arm) and L2= 250 mm (Connecting rod).
Ѳ
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Calculate and plot the position, velocity, and acceleration of the piston for one revolution of the
crank. Make the three plots on the same page. Set Ѳ=0 when t=0 sec, r =L1, and c = 0.25.
The displacement of the piston:
1
x = rcos(θ) + (c 2 − r 2 sin2(θ))2
r 2 θ̇ sin(2θ)
ẋ = −rθ̇ sin(θ) −
2(c 2
−
1
r 2 sin2(θ))2
(The velcoity of the piston)
The acceleration of the piston is:
ẍ =
dẋ
dt
θ = θ̇t, and θ̇ =
2πn
60
clear all
clc
c=0.25;
r=0.12;
t=0:1:120;
n=500;
thetadot=(2*pi*n)/60;
theta=thetadot*t;
x=r*cos(theta)+(c^2-r^2*(sin(theta)).^2).^(1/2);
subplot(3,1,1)
plot(t,x)
title('Displacment, Velcoity, and Acceleration vs time')
grid on
xlabel('time [s]');ylabel('Displacment in [m]')
subplot(3,1,2)
xdot=-r*thetadot*sin(theta)-(r^2*thetadot*sin(2*theta))./(2*(c^2r^2.*(sin(theta)).^2)).^(1/2);
plot(t,xdot)
grid on
xlabel('time [s]');ylabel('Velocity in [m/sec]')
subplot(3,1,3)
A=4*r^2*cos(2*theta).*(c^2-r^2.*(sin(theta)).^2)+(r^2.*sin(2*theta)).^2;
B=4*(c^2-r^2*(sin(theta)).^2).^(3/2);
xddot=-r*thetadot.^2.*cos(theta)-A./B;
plot(t,xddot)
grid on
xlabel('time [s]');ylabel('Acceleration in [m/s^2]')
hold off
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
PROBLEM 1.6 STATEMENT
Use AX=B to find the displacement of the springs as shown in Fig. 1.3. If W1=W1=W3 = 50 N
and the spring stiffness for all springs is 5 N/mm.
Fig.1.3 Spring Systems
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
clear all
clc
k1=5;
k2=k1;k3=k1;k4=k1;k5=k1;
W1=50;W2=W1,W3=W1;
K=[k1+k2+k3+k5
-k3
-k5;-k3 k3+k4 -k4;-k5
W=[W1;W2;W3];
X=inv(K)*W
-k4
k4+k5];
X =
15
25
25
PROBLEM 1.7 STATEMENT
Use Ku=P to find the displacement u at the joints of the truss as shown in Fig. 1.4.
Fig.1.4 Truss
clear all
clc
K=[27.58 7.004 -7.004 0
0;7.004 29.57 -5.253 0 -24.32;
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
-7.004 -5.253 29.57 0 0;0 0 0 27.58 -7.004;
0 -24.32 0 -7.004 29.57]*1000;
P=[0 0 0 0 -45]';
u=inv(K)*P
u =
0.0014
-0.0065
-0.0008
-0.0019
-0.0073
PROBLEM 1.8 STATEMENT
Solve the set of equations using the decomposition method and AX=B:
5x1 + 3x2 + 4x3 = 12
6x1 + 3x2 + 4x3 = 15
7x1 + 9x2 + 2x3 = 10
Solution:
A=[5 3 4;6 3
B=[12;15;10];
X=inv(A)*B
4;7
9 2];
X=
3.0000
-1.2667
0.2000
PROBLEM 1.9 STATEMENT
1 4
If A = [2 3
6 7
5
3
4] and B = [2
9
6
4 5
6 8]
2 4
Find
1.
2.
3.
4.
5.
6.
7.
The Rank of Matrix A and B?
C=A/B If it is possible?
C=A×B if it is possible?
C=A-B?
C=A+B?
Transpose of A and B?
Inverse of A and B, Is it same as the part (6)?
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
8. C=A\B if it is possible?
Solution:
clear all
clc
A= [1 4 5;2 3 4;6 7 9];
B=[3 4 5; 2 6 8;6 2 4];
% Rank of A and B
Z1=rank(A)
Z2=rank(B)
% Mathematical Operations
C1=A/B
C2=A*B
C3=A-B
C4=A+B
% Transpose
T1=A'
T2=B'
% Inverse of A and B
I1=inv(A)
I2=inv(B)
% Backward Division
C5=A\B
Z1 =
3
Z2 =
3
C1 =
0.3333
0.5000 -0.1667
0.3333
0.2500
1.6667
0
C2 =
41
38
57
36
34
50
86
84 122
C3 =
-2
0
0
0.0833
0.1667
College of Engineering
Fall Session- 2016
0 -3
-4
0
5
5
4
8
10
4
9
12
12
9
13
1
2
6
4
3
7
5
4
9
3
2
6
4
6
2
5
8
4
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
C4 =
T1 =
T2 =
I1 =
-0.3333 -0.3333
0.3333
2.0000 -7.0000
2.0000
-1.3333
5.6667 -1.6667
I2 =
0.3333 -0.2500
0.0833
1.6667 -0.7500 -0.5833
-1.3333
0.7500
0.4167
C5 =
0.3333 -2.6667 -3.0000
4.0000 -30.0000 -38.0000
-2.6667 25.3333 32.0000
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Problem 1.10 Statement
Using subplot command to plot the following equations:
y1 = x 3 + cos(x) + ex and y2 = x 2 + sin(x) + ex
(Note: Label and title each curves).
B. (15%) Using contour command to plot the following equation:
Z = f(x, y) = xy ∗ cos(x) where x = 0: 20 and y = 0: 20
(Note: Label and title each curves).
Solution:
Aclear all
clc
x=0:20;
y1=x.^3+cos(x)+exp(x);
y2=x.^2+sin(x)+exp(x);
subplot(2,1,1)
plot(x,y1)
xlabel('x');ylabel('y1')
title('y1 vs x')
grid on
subplot(2,1,2)
plot(x,y2)
xlabel('x');ylabel('y2')
title('y2 vs x')
grid on
Fig.1.5. Subplot of two Functions
clear all
clc
x=0:20;y=0:20;
for i=1:length(x)
for j=1:length(y)
z(i,j)=x(i).*y(j)*cos(x(i));
end
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
end
contourf(z',20)
xlabel('x')
ylabel('y')
title('z vs x&y')
z vs x&y
20
18
16
14
y
12
10
8
6
4
2
2
4
6
8
10
12
14
16
18
20
x
Fig.1.6. Contour Plot
Problem 1.11 Statement
Using Integration and differentiation commands to integrate and differentiate the following
equations:
𝐀. y1 = ln(x 2 + x + 1) + sin(x)
2
𝐁. y2 = ex + ln(x)
Solution:
clear all
clc
syms x y1 y2
y1=log(x^2+x+1)+sin(x);
y2=exp(x^2)+log(x);
I1=int(y1,x)
D1=diff(y1,x)
I2=int(y2,x)
D2=diff(y2,x)
Problem
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Problem 1.12 Statement
Using Finite Difference Method to plot the differential equation y vs x:
dy
+ 3y = ex where y(x = 0) = 1, and y(x = 1) = 0
dx
(Given: Niter=10000, Number of nodes/points/grids in x-direction is 100)
Solution:
The given boundary conditions are:
At i = 1,
y(1) = 1, at i = Nx y(Nx) = 0
For central: 2 ≤ 𝑖 ≥ 𝑁𝑥 − 1
y(i) − y(i − 1)
+ 3y(i) = exp(x(i))
∆x
[exp(x(i)∆x + y(i − 1)]
y(i) =
(3∆x + 1)
L
1
∆x =
=
Nx − 1 100 − 1
clear all
clc
Nx=100;
Niter=10000;
L=1;
dx=L/(Nx-1);
x=0:dx:L;
for k=1:Niter
y(1)=1;
y(Nx)=0;
for i=2:Nx-1
y(i)=1/(3*dx+1)*(exp(x(i))*dx+y(i-1));
end
end
plot(x,y,'--')
xlabel('x')
ylabel('y')
title('y vs x')
grid on
legend('1D-FDM ')
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Fig.1.7. FDM solution of Differential Equation
EXAM1’S PROBLEMS:
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Solution:
clear
clc
A=[1 4 0;2 3 0;6 7 0];
B=[3 4 5;2 6 8;6 2 4];
R1=rank(A);
R2=rank(B);
C1=A/B;
C2=A*B;
C3=A-B;
C4=A+B
C5=A';
C6=B';
C7=inv(A);
C8=inv(B);
C9=A\B;
Solution:
clear
clc
A=[1 2 3;3 3 4;2 3 3];
B=[1;1;2];
x=inv(A)*B
X=-0.5000
y=1.5000
Z=-0.5000
Solution:
College of Engineering
Fall Session- 2016
Programming and Computer Applications for ME -ME201
Review Lectures-ME201
Main file:
clear all
yo=0;
a=0;
b=5;
[t,y]=ode45(@f,[a,b],yo);
plot(t,y)
xlabel('t')
ylabel('y')
title('y vs t')
grid on
Subroutine File:
function z=f(t,y)
z=2*t;
end
Fig.1.8. Solving Ordinary Differential Equation using ODE45