You are on page 1of 43

Institute of Technology of Cambodia

Faculty of Civil Engineering


Department of Civil Engineering

Subject: MATLAB

Lecturer: Keang Sè POUV, Ph.D., Eng.

Group 6: I3-GCI-A2
ENG Samrithvongkheang ID: e20200406
HAKKA Kimheng ID: e20200002
HAM Chetra ID: e20200546
HAY Liheng ID: e20200825
HEAL Na Bunnin ID: e20201203
HEAN Sopha ID: e20200085

Academic Year: 2022 − 2023


Contents

Introduction 1

TP1: Getting start with MATLAB 2


Exercise 1: Vectors, curves and formal calculations . . . . . . . . . . . . . . . . . . . . . . . 2
Exercise 2: Manipulation of matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exercise 3: Graphics functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 4: Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

TP2: Mini-project: Internal forces of a beam on simple supports 21


Algrithm to determine Iz , R1 , R2 , V, M and σx . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Applying the algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Compare and analyze the results of the 3 case . . . . . . . . . . . . . . . . . . . . . . . . . . 29

References 31

Appendix A Table of saved results in TP2 32

Appendix B TP instruction 34
Script Listings
1 TP1EX12.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 TP1EX13A.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 TP1EX14.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 TP1EX15i.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 TP1EX15ii.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6 TP1EX21.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7 TP1EX22.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
8 TP1EX24.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9 TP1EX31D.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
10 TP1EX31F.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11 TP1EX31G.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
12 TP1EX32.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
13 kmh2ms.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
14 ms2kmh.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
15 polaire.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
16 TP1EX43.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
17 TP2 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
18 TP2_Program.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
19 Results for case 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
20 Results for case 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
21 Results for case 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
22 TP2_Analyze.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

List of Figures
1 s1 in function of Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2 Addition of s2 and s3 to figure 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Addition of Formatting to figure 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Addition of point of t in figure 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5 Displaying s1 , s2 and s3 in 3 separate graphs . . . . . . . . . . . . . . . . . . . . . . . . 15
6 Addition of graph x(t) to figure 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7 3D surface of z1 and z2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
8 OA1 A2 in polar coordinate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
9 Beam subjected to a distributed load. . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
10 The divided segment for calculating shear force and bending moment. . . . . . . . . . . 21
11 Diagrams of V (x) and M(x) for case 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 27
12 Diagrams of V (x) and M(x) for case 2 . . . . . . . . . . . . . . . . . . . . . . . . . . 28
13 Diagrams of V (x) and M(x) for case 3 . . . . . . . . . . . . . . . . . . . . . . . . . . 29
14 Diagrams of V (x) and M(x) for all cases . . . . . . . . . . . . . . . . . . . . . . . . . 30

List of Tables
1 Extreme value of V (x), N(x) and σx for all cases . . . . . . . . . . . . . . . . . . . . . 30
2 xVM_table1.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3 xVM_table2.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4 xVM_table3.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Introduction
In this practical work (TP)1 , we focused on getting start with MATLAB. There are exercises indicated
to some needed and common commands and functions in civil engineering such as:

• Vectors, curves and formal calculations

• Manipulation of matrices

• Graphics functions

• Writing functions.

What is MATLAB?
Like other well-known programming languages including Java, C#, etc., MATLAB was initially known
as the matrix programming language created by MathWorks. MATLAB is an acronym for "Matrix
Laboratory", it provides an interactive environment with hundreds of built-in functions for technical
computing, graphics, and animations.

Operating modes
1. interactive mode: MATLAB executes the instructions as they are given (Conmmend Window).

2. executive mode: MATLAB executes line by line an ".m" file (MATLAB programming language)
(write in Script).

Advantages
• Easy to use, quick to learn

• Existence of useful toolboxes for the engineer

• Possibility of interfacing it with other languages (C, C++, Fortran), enables parallel computing.

Disadvantages
• Limitation in memory

• Paid to use.

1
Please refer to Appendix B for the instruction and exercises.

Prepared by I3-GCI-A2(6) 2022 − 23 Page 1 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

TP1: Getting start with MATLAB


Exercise 1: Vectors, curves and formal calculations
hπ π π i
1. Define variable x = , , and calculate y1 = sin(x) and y2 = cos(x).
6 4 3
Command Window:
>> x = [pi/6 pi/4 pi/3];
>> y1 = sin(x)

y1 =

0.5000 0.7071 0.8660

>> y2 = cos(x)

y2 =

0.8660 0.7071 0.5000

Then calculate y3 = 3 tan2 (x) and y4 = sin(2x) + cos(3x) using exclusively the previous vectors y1
and y2 .
We have  2
sin x y1
tan x = =⇒ y3 = 3
cos x y2
sin(2x) = 2 sin x cos x and cos(3x) = 4 cos3 x − 3 cos x
=⇒ y4 = (2y1 y2 ) + (4y23 − 3y2 )

Command Window:
>> y3 = 3.*(y1./y2).^2

y3 =

1.0000 3.0000 9.0000

>> y4 = (2.*y1.*y2)+(4.*y2.^3-3.*y2)

y4 =

0.8660 0.2929 -0.1340

Note: .*, ./ and .∧ are for term-to-term matrix operations.


√ √
3
2. We have y = 2e − x . Calculate
Z
I = y dx and d = y ′ (x)

Listing 1: TP1EX12.m
syms x ; % Making x as a variable by command syms

Prepared by I3-GCI-A2(6) 2022 − 23 Page 2 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

y = sqrt(2)*exp(-x^(1/3)); % Defining function y


% int for integral, and diff for differential
I = int(y);
d = diff(y);
% Making it easy to read by command pretty
pretty(y)
pretty(I)
pretty(d)

Command Window:
1/3
sqrt(2) exp(- x )

1/3 1/3 2/3


-sqrt(2) exp(- x ) (2 x + x + 2) 3

1/3
sqrt(2) exp(- x )
- -------------------
2/3
3 x

We get
√ √3
 √ √
3

I = −3 2e − x 2 3 x + x 2 + 2 + c
√ −√ 3
2e x
d =− √ 3
3 x2
3. We consider the following vectors:
     
0 −5 −1
u = −6 , v = 2  and w = 0 
3 1 1
(a) Calculate t = u + 3uv − uv w .
Listing 2: TP1EX13A.m
1 u = [0; -6; 3];
2 v = [-5; 2; 1];
3 w = [-1; 0; 1];
4 t = u + 3*u.*v - u.*v.*w

Command Window:
>> t =

0
-42
9

We get  
0
t = −42
9

Prepared by I3-GCI-A2(6) 2022 − 23 Page 3 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

(b) Calculate ∥u∥, ∥v ∥, ∥w ∥, ∥t∥.


Using command norm
Command Window:
>> norm(u)

ans =

6.7082

>> norm(v)

ans =

5.4772

>> norm(w)

ans =

1.4142

>> norm(t)

ans =

42.9535

We get
∥u∥ = 6.7082, ∥v ∥ = 5.4772, ∥w ∥ = 1.4142 and ∥t∥ = 42.9535

4. Define complex variable a = [i , −i , 1 + i , 1 − i , 2 + 2i , 2 − 2i , . . . , 100 + 100i , 100 − 100i ]. Then


calculate the complex module of each element of a.
Let n be the last term’s value of the vector. The complex sequence starts from 1 to n in pair value
with each i -s in deference signs (+, −). Using command for, the beginning two values (i and −i )
are outside the sequence; hence, they are put manually. Finally, (−1)2m and (−1)2m+1 indicate
positive and negative signs respectively.

Listing 3: TP1EX14.m
1 n=100;
2 a=[i, -i];
3 for m=1:n
4 a=[a, m+m*i, m-m*i];
5 end
6 a

Command Window:
>> a

a =

Prepared by I3-GCI-A2(6) 2022 − 23 Page 4 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

1.0e+02 *

Columns 1 through 4

0.0000 + 0.0100i 0.0000 - 0.0100i 0.0100 + 0.0100i 0.0100 - 0.0100i

Columns 5 through 8

0.0200 + 0.0200i 0.0200 - 0.0200i 0.0300 + 0.0300i 0.0300 - 0.0300i


:
:
:
Columns 197 through 200

0.9800 + 0.9800i 0.9800 - 0.9800i 0.9900 + 0.9900i 0.9900 - 0.9900i

Columns 201 through 202

1.0000 + 1.0000i 1.0000 - 1.0000i

Using command abs for the module of complex numbers:


Command Window:
>> format bank
>> abs(a)

ans =

Columns 1 through 5

1.00 1.00 1.41 1.41 2.83

Columns 6 through 10

2.83 4.24 4.24 5.66 5.66


:
:
:
Columns 196 through 200

137.18 138.59 138.59 140.01 140.01

Columns 201 through 202

141.42 141.42

5. Calculate the factorial (i.e., 100!) by two methods possible.

i. Command prod
• Let n be the factorial number
• Defining a vector contain 1, 2, 3, . . . , n

Prepared by I3-GCI-A2(6) 2022 − 23 Page 5 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

• Using command prod to multiply all the elements.

Listing 4: TP1EX15i.m
1 n = 100;
2 v = 1:n;
3 f = prod(v)

Command Window:
>> f =

9.3326e+157

ii. Command for


Let n be the factorial number, and f be the value of the factorial.
Example: 3!
Command Window:
>> n=3;
>> f=1;
>> for m=1:n
f=f*m
end

f =

f =

f =

When m = 1 then f = f ∗ m = 1 × 1, because we defined f = 1 before for. When


m = 2 =⇒ f = 1 × 2, and m = 3 =⇒ f = 2 × 3. The value of f is taken continuously
from the previous result.

Listing 5: TP1EX15ii.m
1 fprintf('Factorial Calculation \n')
2 n = input('Please insert a number: ');
3 f = 1;
4 for m = 1:n
5 f = f*m;
6 end
7 formatSpec='The result is: %g! = %g \n';
8 fprintf(formatSpec,n,f)

Prepared by I3-GCI-A2(6) 2022 − 23 Page 6 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Note: fprintf to display the text, input to input the wanted value of n, formatSpec to
formats data, %g for compact of number with no trailing zeros.
Command Window:
Factorial Calculation
Please insert a number: 100
The result is: 100! = 9.33262e+157

Exercise 2: Manipulation of matrices


1. Define vector U = [−1 − 2 . . . − 14 − 15]. What is the size of this vector? Define vector V
containing the first five and the last five elements of U. Then define vector W = [1/2 1 . . . 7 15/2]
and vector X = [1 1/22 . . . 1/142 1/152 ], all from vector U. Calculate P = 2W 2 X knowing that
P has the same number of elements as U.

• Create vector U where start = −1, end = −15, and step = −1


• Use command size where m is rows and n is columns
2
• Extract vectors V, W = − 12 U, and X = (U −1 )
• Calculate P .

Listing 6: TP1EX21.m
1 U = [-1:-1:-15];
2 [m,n] = size(U)
3 V = [U(:,[1:5]), U(:,[end-4:end])]
4 W = -1/2.*U
5 X = 1./U.^2
6 P = 2*W.^2.*X

Command Window:
m =

n =

15

V =

-1 -2 -3 -4 -5 -11 -12 -13 -14 -15

W =

Columns 1 through 8

0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000

Prepared by I3-GCI-A2(6) 2022 − 23 Page 7 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Columns 9 through 15

4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000

X =

Columns 1 through 8

1.0000 0.2500 0.1111 0.0625 0.0400 0.0278 0.0204 0.0156

Columns 9 through 15

0.0123 0.0100 0.0083 0.0069 0.0059 0.0051 0.0044

P =

Columns 1 through 8

0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000

Columns 9 through 15

0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000

 
1 2 3 4 5 6 7 8 9 10
2. Define matrix M = 11 12 13 14 15 16 17 18 19 20. What are its dimensions m ×
21 22 23 24 25 26 27 28 29 30
 
1 2  
3 7
n? Extract from this matrix, the matrix N = 11 12 , and the matrix P =
  . Extract
23 27
21 22
from matrix M, the matrix Q obtained by taking in matrix M one column out of 2.
Listing 7: TP1EX22.m
1 M=[[1:1:10]; [11:1:20]; [21:1:30]];
2 [m,n]=size(M)
3 N=M(:,[1,2])
4 P=M([1,3],[3,7])
5 Q=M(:,[1:2:end])

Command Window:
m =

n =

10

Prepared by I3-GCI-A2(6) 2022 − 23 Page 8 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

N =

1 2
11 12
21 22

P =

3 7
23 27

Q =

1 3 5 7 9
11 13 15 17 19
21 23 25 27 29

3. Define a matrix A dimensions 4 × 4 using command magic.


Command Window:
>> A = magic(4)

A =

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

Determine the matrix B = A/15.


Command Window:
>> B=A ./ 15

B =

1.0667 0.1333 0.2000 0.8667


0.3333 0.7333 0.6667 0.5333
0.6000 0.4667 0.4000 0.8000
0.2667 0.9333 1.0000 0.0667

How many numbers in this matrix B are between 0.4 and 0.8? Where are they located?
Command Window:
>> Locate = find(B>0.4 & B<0.8)

Locate =

Prepared by I3-GCI-A2(6) 2022 − 23 Page 9 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

3
6
7
10
14

>> Count = length(Locate)

Count =

• There are 5 numbers between 0.4 and 0.8 in matrix B.


• Command find shows the location of numbers which are counted continuously by columns:
{3 (row 3, col 1), 6 (row 2, col 2), 7 (row 3, col 2), 10 (row 2, col 3), 14 (row 2, col 4)}.
Construct matrix C obtained from matrix B by replacing all the numbers of B less than 0.8 by 0,
and those greater than or equal to 0.8 by 1.
Command Window:
>> C = B ;
>> C(C < 0.8) = 0 ;
>> C(C >= 0.8) = 1

C =

1 0 0 1
0 0 0 0
0 0 0 1
0 1 1 0

4. Solve the linear equation system below using two possible approaches:

x + y = 14 (1)
y + z = 10 (2)
x −y =4 (3)
z −t =1 (4)

i. Manually
Taking

(1) + (3) =⇒ 2x = 18 =⇒ x = 9
(1) =⇒ y = 14 − x = 5
(2) =⇒ z = 10 − y = 5
(4) =⇒ t = z − 1 = 4

ii. Using command solve


• Clear Workspace (optional)
• Define variable
• Define all of the equations

Prepared by I3-GCI-A2(6) 2022 − 23 Page 10 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

• Put the equations and the variables together (optional, we can also write all of them in
the command solve)
• Solve
• Show the result.

Listing 8: TP1EX24.m
1 clear x y z t
2 syms x y z t
3 Eq1 = x+y==14;
4 Eq2 = y+z==10;
5 Eq3 = x-y==4;
6 Eq4 = z-t==1;
7 Eqs = [Eq1, Eq2, Eq3, Eq4];
8 Vars = [x y z t];
9 Sol = solve(Eqs, Vars);
10 x=Sol.x
11 y=Sol.y
12 z=Sol.z
13 t=Sol.t

Command Window:
x =

y =

z =

t =

Therefore, we get x = 9, y = 5, z = 5 and t = 4.

Exercise 3: Graphics functions


1. 2D graphics

(a) Create a time vector t (t in seconds) ranging from 0 to 10 with 11 points.


Command Window:
>> t=linspace(0,10,11)

Prepared by I3-GCI-A2(6) 2022 − 23 Page 11 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

t =

0 1 2 3 4 5 6 7 8 9 10

(b) Create a signal s1 (t) = 1 − e −t then display it as a function of time.


Command Window:
>> s1=1-exp(-t);
>> plot(t,s1)

Figure 1: s1 in function of Time

(c) Create two other signals defined by s2 (t) = 1 − e −2t and s3 (t) = 1 − e −4t , then display them
in the same figure.
Using command hold on so that we can display maltiple graph in the same figure.
Command Window:
>> hold on
>> s2=1-exp(-2*t);
>> plot(t,s2)
>> s3=1-exp(-4*t);
>> plot(t,s3)

Figure 2: Addition of s2 and s3 to figure 1

Prepared by I3-GCI-A2(6) 2022 − 23 Page 12 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

(d) Make changes to display these 3 signals in blue, red and green respectively. In addition to the
colors, show asterisks (*), crosses and dots at different values making up the 3 signals. Add
a legend to the curves and the titles corresponding to the abscissa axis and the ordinate axis.

Listing 9: TP1EX31D.m
1 t=linspace(0,10,11);
2 s1=1-exp(-t);
3 plot(t,s1,'b*-')
4 hold on
5 s2=1-exp(-2*t);
6 plot(t,s2,'rx:')
7 s3=1-exp(-4*t);
8 plot(t,s3,'g.-.')
9 title('Signal in function of time s(t)')
10 ylabel('Signal s_1, s_2, s_3')
11 xlabel('t (Time in second)')
12 legend('s_1','s_2','s_3')

Figure 3: Addition of Formatting to figure 2

(e) We now want to change the time variable and define it with 10 times more points.
Making the only change [t=linspace(1,10,11*10)], then just run the script (listings 9)
again.

Prepared by I3-GCI-A2(6) 2022 − 23 Page 13 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 4: Addition of point of t in figure 3

(f) Display these 3 signals in the same figure, but this time in 3 separate charts on two lines,
including 2 charts at the top and 1 chart at the bottom.
Graphics 1 Graphics 2
Graphics 3
Using subplot(m,n,p) divides the figure into m × n grid and grid position specified by p.

Listing 10: TP1EX31F.m


1 t=linspace(0,10,11*10);
2 figure
3 % Plot s1
4 subplot(2,2,1)
5 s1=1-exp(-t);
6 plot(t,s1,'b*-')
7 title('Signal s_1 in function of time')
8 ylabel('Signal')
9 xlabel('t (Time in second)')
10 % Plot s2
11 subplot(2,2,2)
12 s2=1-exp(-2*t);
13 plot(t,s2,'rx:')
14 title('Signal s_2 in function of time')
15 ylabel('Signal')
16 xlabel('t (Time in second)')
17 % Plot s3
18 subplot(2,1,2)
19 s3=1-exp(-4*t);
20 plot(s3,t,'g.-.')
21 title('Signal s_3 in function of time')
22 ylabel('Signal')
23 xlabel('t (Time in second)')

Prepared by I3-GCI-A2(6) 2022 − 23 Page 14 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 5: Displaying s1 , s2 and s3 in 3 separate graphs

(g) Now add in the third graph the harmonic curve of form x(t) = A cos(ωt +φ) with ω = 2, A = 3
and φ = 0.5. Display the properties of this curve like title, color, etc.
In the script, using command hold on after plotting s3 (t), then add the codes for displaying
x(t) and makeing some changes to the properties.
Script: continue from listings 10

Listing 11: TP1EX31G.m


...
% Plot s3
subplot(2,1,2)
plot(t,s3,'g.-.')
hold on
% Plot x(t)
subplot(2,1,2)
x=3*cos(2*t+0.5);
plot(t,x,'mo--')
xlabel('t (Time in second)')
ylabel('s_3 & x')
title('Plot of s_3 and x(t)=Acos(\omega t + \phi)')

Prepared by I3-GCI-A2(6) 2022 − 23 Page 15 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 6: Addition of graph x(t) to figure 5

2. 3D graphics
Illustrate in the same graph, the surfaces of the following two functions:

• z1 = 100 sin(r )/r , where r = x 2 + y 2 (mexican hat)


p

• z2 = x − y + 50

First, we defined the vectors in the functions (x and y ) to create some points for plotting by
-10:0.25:10. Then, using meshgrid to produce full grids from vectors (Otherwise 3D ploting
wouldn’t work). Third, we defined our function (r, z1 , z2 ). Next, displaying z1 in 3D by command
surf; then, using command hold on so that we can display z2 in the same figure. Finally, we
added the properties.

Listing 12: TP1EX32.m


1 x = -10:0.25:10;
2 y = x;
3 [x,y] = meshgrid(x,y);
4 r = sqrt(x.^2+y.^2) ;
5 z1 = 100*sin(r)./r;
6 z2 = x-y+50;
7 surf(x,y,z1)
8 hold on
9 surf(x,y,z2)
10 title('Surface of z_1 = 100sin(r)/r and z_2 = x - y + 50')
11 legend('z_1','z_2')
12 xlabel('x')
13 ylabel('y')
14 zlabel('z')

Prepared by I3-GCI-A2(6) 2022 − 23 Page 16 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 7: 3D surface of z1 and z2

Exercise 4: Writing functions


1. Create a function to convert the speed unit from km/h to m/s.

• The formats of the function in Matlab:


1 function [outputArg1,outputArg2] = name(inputArg1,inputArg2)
2 outputArg1 = inputArg1;
3 outputArg2 = inputArg2;
4 end

• name: Name of the function, and it must be the same as the file’s name.
• inputArg1 & 2: The variables that we want to compute.
• outputArg1 & 2: The variables that we want to return.
• Since 1km/h = 1000/3600m/s, we can write our function as:

Listing 13: kmh2ms.m


1 function [ms] = kmh2ms(kmh)
2 ms=kmh*1000./3600;
3 end

• Then, save and run it. However; it returned as error. In order to compute, we must define
the variables and call this function (by name) in the Command Window.
Command Window:
>> kmh=120;
>> [ms]=kmh2ms(kmh)

ms =

33.3333

Prepared by I3-GCI-A2(6) 2022 − 23 Page 17 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

• Thus, we get 120km/h = 33.33m/s

2. Create a function to convert the speed unit from m/s to km/h.

1m/s = 10−3 × 3600km/h

Listing 14: ms2kmh.m


1 function [kmh] = ms2kmh(ms)
2 kmh = ms*3600./1000;
3 end

Command Window
>> ms2kmh
Not enough input arguments.

Error in ms2kmh (line 2)


kmh = ms*3600./1000;

>> kmh=ms2kmh(3)

kmh =

10.8000

Therefore, 3m/s = 10.8km/h.

3. Write a function named polai r e, taking the Cartesian coordinates (x, y ) as input arguments of a
point and returning the polar coordinates (r, θ) as output from this point, where θ is in degrees.

Listing 15: polaire.m


1 function [r,theta] = polaire(x,y)
2 % x, y are the Cartesian coord. (inputs), and r, theta are the polar coord. (
outputs).
3 r = sqrt(x^2+y^2);
4 % Find r by formular
5 theta = atan(abs(y/x))*180/pi; % Compute in 1st quadrant only.
6 % abs for absolute number, and convert to degree.
7 % Conditioning for theta by quadrant counterclockwise
8 if (x<0) && (y>=0) % 2nd quadrant
9 theta = 180-theta;
10 elseif (x<0) && (y<0) %3rd quadrant
11 theta = 180+theta;
12 elseif (x>=0) && (y<0)
13 theta = 360-theta; % 4th quadrant
14 else
15 theta = theta; % 1st quadrant
16 end
17 end

Using this created polai r e function, then write a new script (file extension .m) to:

(a) Convert the Cartesian coordinates of A1 (43.3, 25) and A2 (65.78, 23.94) to polar coordinates.

Prepared by I3-GCI-A2(6) 2022 − 23 Page 18 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

(b) Illustrate graphically the triangle OA1 A2 in polar coordinates, where O(0, 0) is the coordinate
system of the polar axes.
−−→ −−→
(c) Determine the angle α formed by the two vectors OA1 and OA2 .

Listing 16: TP1EX43.m


1 % a. Convert A1 and A2 to polar coord.
2 % A1(43.3,25)
3 % A2(65.78,23.94)
4 %[r,theta]=polaire(x,y)
5 [r1, theta1] = polaire(43.3,25)
6 [r2, theta2] = polaire(65.78,23.94)
7
8 % b. Illustrate OA1A2 in polar coordinates
9 theta = [0 theta1 theta2 0].*pi./180;
10 % Since our polaire function, theta is in degree, we need to convert it back to
radiant.
11 r = [0, r1 r2 0];
12 figure
13 polarplot(theta,r,'gx-','LineWidth',3,'MarkerSize',10,'MarkerEdgeColor','blue')
14 title('Polar graph of OA_1A_2')
15 % Modifying theta axis
16 ax = gca; % current axes format in polar
17 ax.ThetaLim = [0 90]; % Limiting the graph, display only by 90 degree
18 ax.ThetaTick = 0:10:90 ; % Angle grids on theta axis
19 % Modifying r-axis
20 ax.RLim = [0 75]; % Min and Max of radius axis
21 ax.RTick = 0:10:75 ; % Number in radius axis
22 % Puting text on points
23 text(pi/6,r1+3,'A_1')
24 text(pi/10,r2,'A_2')
25
26 % c. Determine alpha formed by OA1 and OA2
27 Alpha = abs(theta2-theta1)
28 text(pi/6.5,25,"\alpha = " + Alpha)

Command Window
r1 =

49.9989

theta1 =

30.0007

r2 =

70.0009

Prepared by I3-GCI-A2(6) 2022 − 23 Page 19 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

theta2 =

19.9985

Alpha =

10.0022

Figure 8: OA1 A2 in polar coordinate

Prepared by I3-GCI-A2(6) 2022 − 23 Page 20 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

TP2: Mini-project: Internal forces of a beam on simple supports


We consider a beam of length L and section a × b and having a Young’s modulus E and a resistance σa
(figure 9). The beam is placed on two simple supports and subjected to a distributed load w applied at
a distance A from the left support.

Figure 9: Beam subjected to a distributed load.

Write under Matlab, an algorithm allowing to obtain:

• moment inertia of vertical axis Iz


1 3 1 3
We have Iz = bh = ab
12 12
• reactions of the two supports (R1 and R2 )
By equations of equilibrium:
X
(↑ +) fy = 0 ⇐⇒ R1,y + R2,y − B × w = 0
=⇒ R1,y = −R2,y + Bw (1)
X 1
(⟲ +) M1 = 0 ⇐⇒ −Bw × (A + B) + LR2,y = 0
2
B(A + 12 B)w
=⇒ R2,y = (2)
L
By (1) & (2)

B(A + 21 B)w
=⇒ R1,y = − + Bw
L
−(A + 21 B)Bw + LBw
=
L
Bw (−A − 21 B + L)
=
L
Bw (L − A − 12 B) (A + 21 B)Bw
Therefore, R1 = and R2 = .
L L

(a) Segment 1 (b) Segment 2 (c) Segment 3

Figure 10: The divided segment for calculating shear force and bending moment.

• shear force (V )

Prepared by I3-GCI-A2(6) 2022 − 23 Page 21 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

+ Segment 1 (0 ≤ x < A):


By equations of equilibrium
X
(↑ +) fy = 0 ⇐⇒ R1 − V = 0 =⇒ V = R1

+ Segment 2 (A < x < A + B):


X
(↑ +) fy = 0 ⇐⇒ R1 − w (x − A) − V = 0 =⇒ V = R1 − w (x − A)

+ Segment 3 (A + B < x < L):


X
(↑ +) fy = 0 ⇐⇒ R1 − Bw − V = 0 =⇒ V = R1 − Bw

• bending moment (M)

+ Segment 1 (0 ≤ x < A):


By equations of equilibrium
X
(⟲ +) M1 = 0 ⇐⇒ M − V × x = 0

=⇒ M = V x = R1 x

+ Segment 2 (A < x < A + B):


 
X x −A
(⟲ +) M1 = 0 ⇐⇒ M − w (x − A) A + −V ·x =0
2
w
⇐⇒ M − w A(x − A) − (x − A)2 − x(R1 − w (x − A)) =0
2
w
⇐⇒ M − R1 x − w A(x − A) − (x − A)2 + w x(x − A) =0
2
w 2
=⇒ M − R1 x − (x − A) + w (x − A)(x − A) =0
2
w
=⇒ M − R1 x + (x − A)2 =0
2
w
=⇒ M = R1 x − (x − A)2
2
+ Segment 3 (A + B < x < L):
X
(⟲ +) M1 = 0 ⇐⇒ M − Bw (A + B/2) − V · x =0
⇐⇒ M − Bw (A + B/2) − x(R1 − Bw ) =0
1
⇐⇒ M − R1 x − ABw − B 2 w + Bw x =0
2
=⇒ M − R1 x + w B(x − A − B/2) =0

=⇒ M = R1 x − w B(x − A − B/2)

• bending stress (σx ) at the position of the maximum moment


We have
Mmax · y
σx =
Iz
Where in our beam y = b/2.

Prepared by I3-GCI-A2(6) 2022 − 23 Page 22 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Listing 17: TP2 Algorithm


1 %moment inertia
2 Iz=1/12*a*b^3;
3
4 %reaction
5 R1=(B*w*(L-A-0.5*B))/L;
6 R2=(B*w*(A+0.5*B))/L;
7
8 %shear and moment
9 %segment 1
10 x1 = 0:0.1:A ;
11 V1 = R1;
12 M1 = R1 * x1;
13 %segment 2
14 x2 = A:.1:A+B ;
15 V2 = R1 - w * (x2 - A);
16 M2 = R1 * x2 - 0.5 * w * (x2 - A) .^ 2;
17 %segment 3
18 x3 = A+B:.1:L ;
19 V3 = R1 - B * w;
20 M3 = R1 * x3 - w*B*(x3 - A - 0.5*B);
21
22 % max & min
23 Vmax = max([V1 V2 V3]);
24 Vmin = min([V1 V2 V3]);
25 Mmax = max([M1 M2 M3]);
26
27 %Max stress
28 Sx = Mmax*(b/2)/Iz;

Apply the program for the following cases:

1. a = 20cm, b = 40cm, L = 6m, A = 0m, B = 6m, w = 20kN/m and σa = 28MP a.

2. a = 20cm, b = 40cm, L = 6m, A = 1m, B = 4m, w = 20kN/m and σa = 28MP a.

3. a = 20cm, b = 40cm, L = 6m, A = 2m, B = 2m, w = 20kN/m and σa = 28MP a.

For each case, we want to:

− determine the maximum values of V, M and σx , also verify σx vs σa .

− plot V (x) and M(x) diagrams, then indicate the Vmin , Vmax and Mmax values in the positions corre-
sponding with curves.

− save the results of each case in a file in “.txt” by column x, V and M respectively.

Listing 18: TP2_Program.m


1 clear;clc;close all
2
3 c = input('Please insert number of case (1,2 or 3): '); %Not necessary
4 disp('Please insert value of a, b, L, A, B, w and sigma_a')
5 a = input('a (m) = ');

Prepared by I3-GCI-A2(6) 2022 − 23 Page 23 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

6 b = input('b (m) = ');


7 L = input('L (m) = ');
8 A = input('A (m) = ');
9 B = input('B (m) = ');
10 w = input('w (kN/m) = ');
11 Sa = input('Sigma_a (MPa) = ');
12
13 %Not necessary
14 fprintf('\nResults:\nCase #%g\n',c)
15 fprintf('a = %gm, b = %gm, L = %gm, A = %gm, B = %gm, w = %gkN/m, Sigma_a = %gMPa\n'
, a,b,L,A,B,w,Sa)
16
17 %moment inertia
18 Iz=1/12*a*b^3;
19 fprintf('Moment inertia: Iz = %g m4\n',Iz)
20
21 %reaction
22 R1=(B*w*(L-A-0.5*B))/L;
23 R2=(B*w*(A+0.5*B))/L;
24 disp('Reactions of the two supports:')
25 fprintf('R1 = %g kN and R2 = %g kN\n',R1,R2)
26
27 %shear and moment
28 %segment 1
29 x1 = 0:0.1:A ;
30 V1 = R1;
31 M1 = R1 * x1;
32 %segment 2
33 x2 = A+.1:.1:A+B ;
34 V2 = R1 - w * (x2 - A);
35 M2 = R1 * x2 - 0.5 * w * (x2 - A) .^ 2;
36 %segment 3
37 x3 = A+B+.1:.1:L ;
38 V3 = R1 - B * w;
39 M3 = R1 * x3 - w*B*(x3 - A - 0.5*B);
40
41 % max and min of shear and moment
42 Vmax = max([V1 V2 V3]);
43 Vmin = min([V1 V2 V3]);
44 Mmax = max([M1 M2 M3]);
45 fprintf('Maximum shear stress: V = %g kN\n',Vmax)
46 fprintf('Maximum bending moment: M = %g kN.m\n',Mmax)
47
48 %Max stress
49 Sx = Mmax*(b/2)/Iz*10^(-3); %in kN/m2 or kPa ==> convert to MPa
50 fprintf('Maximum bending stress: Sigma_x = %g MPa\n',Sx)
51 %stress condition
52 if Sx <= Sa
53 disp('maximum stress <= allowable stress, VERIFIED!')
54 else
55 disp('maximum stress > allowable stress, NOT VERIFIED!')

Prepared by I3-GCI-A2(6) 2022 − 23 Page 24 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

56 end
57
58 %plot
59 %putting litle to the figure
60 sgtitle("Diagram of Shear Force and Bending Moment Case #"+c, ...
61 'FontName','Times New Roman', ...
62 'FontWeight','bold','FontAngle','italic','FontSize',16)
63
64 subplot(2,1,1)
65 x = [x1 x2 x3];
66 %Because V1 and V3 are single value, we need to make it into vector corresponding
with x, so V and x contain the same size.
67 V = [ones(size(x1))*V1 V2 ones(size(x3))*V3];
68 plot(x,V,'b','LineWidth',2)
69 %Keeping extra space on the graph
70 xlim([-1 x(end)+1])
71 ylim([Vmin-30 Vmax+30])
72 %Showing grid
73 grid on
74 xticks(0:x(end))
75 yticks(Vmin:20:Vmax)
76 %Adding title and label
77 title('Shear Force','FontSize',14,'FontName','Times New Roman')
78 xlabel('x (in m)','FontSize',13,'FontName','Times New Roman')
79 ylabel('V(x) (in kN)','FontSize',13,'FontName','Times New Roman')
80 %Putting text
81 nVmax = find(V==Vmax); %Locate the position of the value
82 nVmin = find(V==Vmin);
83 text(x(nVmax(end))-.25,Vmax+10,"V_{max} = "+Vmax,'FontName','Times New Roman')
84 text(x(nVmin(1))-.25,Vmin-10,"V_{min} = "+Vmin,'FontName','Times New Roman')
85
86 subplot(2,1,2)
87 M = [M1 M2 M3];
88 plot(x,M,'r','LineWidth',2)
89 xlim([-1 x(end)+1])
90 ylim([0 Mmax+30])
91 grid on
92 xticks(0:x(end))
93 yticks(0:20:100)
94 title('Bending Moment','FontSize',14,'FontName','Times New Roman')
95 xlabel('x (in m)','FontSize',13,'FontName','Times New Roman')
96 ylabel('M(x) (in kN.m)','FontSize',13,'FontName','Times New Roman')
97 nMmax = find(M==Mmax);
98 text(x(nMmax)-.5, Mmax+10,"M_{max} = "+Mmax,'FontName','Times New Roman')
99
100 %create table and write file
101 table = [x; V; M]; % We have data 3 row defining x, V and M respectively
102 fid = fopen("xVM_table"+c+".txt",'w'); %Just for the name to be loaded automatically
103 fprintf(fid,'%4.1f %4.3f %4.3f\n',table);
104 % the data is written by column 3 times then enter to make it a vertical table
105 fclose(fid);

Prepared by I3-GCI-A2(6) 2022 − 23 Page 25 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Note:

+ Lines 33 and 37, for the value of x, adding the first value of x2 and x3 (A+0.1 and A+B+0.1) for
it not to repeat the same value when we combine x1, x2 and x3, because the former sigments (x1
and x2) end in A and A + B respectively already.

+ Lines 60, 85, 86, 100 and 104, Double quotes ("") and (+) are used for string operation.2

+ Line 67, in order to plot, we need V and x to be vectors that contain the same numbers. Since,
V 1 and V 2 are single value (lines 30 and 38), in order to make it corresponding to x automatically:

– by using ones(size(x)), we get a vector the same size as x containing number 1.


– multiply it with V , we have a vector containing all V .

+ Lines 81-84, we want the text of Vmax and Vmin to position automatically. Thus we need V to be
the same term as x:

– locate the position of Vmax or Vmin by command Find


– in this case, we have more than one value (since we make vector at line 67)
– in the command text, we call only one value (1 or end).

+ Lines 103-108, fprintf(FID, FORMAT, A, ...) applies the FORMAT to all elements of array A
and any additional array arguments in column order, and writes the data to a text file.3

• Case 1:
Command Window:

Listing 19: Results for case 1


Please insert number of case (1,2 or 3): 1
Please insert value of a, b, L, A, B, w and sigma_a
a (m) = 0.2
b (m) = 0.4
L (m) = 6
A (m) = 0
B (m) = 6
w (kN/m) = 20
Sigma_a (MPa) = 28

Results:
Case #1
a = 0.2m, b = 0.4m, L = 6m, A = 0m, B = 6m, w = 20kN/m, Sigma_a = 28MPa
Moment inertia: Iz = 0.00106667 m4
Reactions of the two supports:
R1 = 60 kN and R2 = 60 kN
Maximum shear stress: V = 60 kN
Maximum bending moment: M = 90 kN.m
Maximum bending stress: Sigma_x = 16.875 MPa
maximum stress <= allowable stress, VERIFIED!
2
For more operators: [» help string]
3
Its form taken from an example of: [» help fprintf]

Prepared by I3-GCI-A2(6) 2022 − 23 Page 26 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 11: Diagrams of V (x) and M(x) for case 1

• Case 2:
Command Window:

Listing 20: Results for case 2


Please insert number of case (1,2 or 3): 2
Please insert value of a, b, L, A, B, w and sigma_a
a (m) = 0.2
b (m) = 0.4
L (m) = 6
A (m) = 1
B (m) = 4
w (kN/m) = 20
Sigma_a (MPa) = 28

Results:
Case #2
a = 0.2m, b = 0.4m, L = 6m, A = 1m, B = 4m, w = 20kN/m, Sigma_a = 28MPa
Moment inertia: Iz = 0.00106667 m4
Reactions of the two supports:
R1 = 40 kN and R2 = 40 kN
Maximum shear stress: V = 40 kN
Maximum bending moment: M = 80 kN.m
Maximum bending stress: Sigma_x = 15 MPa
maximum stress <= allowable stress, VERIFIED!

Prepared by I3-GCI-A2(6) 2022 − 23 Page 27 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 12: Diagrams of V (x) and M(x) for case 2

• Case 3:
Command Window:

Listing 21: Results for case 3


Please insert number of case (1,2 or 3): 3
Please insert value of a, b, L, A, B, w and sigma_a
a (m) = 0.2
b (m) = 0.4
L (m) = 6
A (m) = 2
B (m) = 2
w (kN/m) = 20
Sigma_a (MPa) = 28

Results:
Case #3
a = 0.2m, b = 0.4m, L = 6m, A = 2m, B = 2m, w = 20kN/m, Sigma_a = 28MPa
Moment inertia: Iz = 0.00106667 m4
Reactions of the two supports:
R1 = 20 kN and R2 = 20 kN
Maximum shear stress: V = 20 kN
Maximum bending moment: M = 50 kN.m
Maximum bending stress: Sigma_x = 9.375 MPa
maximum stress <= allowable stress, VERIFIED!

Prepared by I3-GCI-A2(6) 2022 − 23 Page 28 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Figure 13: Diagrams of V (x) and M(x) for case 3

Finally, we want to compare and analyze the results of the 3 case studies:

− draw the V(x) and M(x) diagrams for the 3 cases using only the previously saved .txt files.

− make a summary table of the extreme values of V, M and of σx between the 3 cases to compare.

Listing 22: TP2_Analyze.m


1 fid1 = fopen("xVM_table1.txt",'r');
2 fid2 = fopen("xVM_table2.txt",'r');
3 fid3 = fopen("xVM_table3.txt",'r');
4 % The data comes with 1 row vector combine, so we need to specified it into 3 row
defining x, V and M
5 data1 = fscanf(fid1,'%f',[3,inf]);
6 data2 = fscanf(fid2,'%f',[3,inf]);
7 data3 = fscanf(fid3,'%f',[3,inf]);
8
9 fclose all;
10
11 % Plot
12 % Putting title to the figure
13 sgtitle('Diagram of Shear Force and Bending Moment of the 3 Cases', ...
14 'FontName','Times New Roman', ...
15 'FontWeight','bold','FontAngle','italic','FontSize',16)
16
17 subplot(2,1,1)
18 plot(data1(1,:),data1(2,:),'g-','LineWidth',1.5)
19 hold on
20 plot(data2(1,:),data2(2,:),'m--','LineWidth',1.5)
21 plot(data3(1,:),data3(2,:),'b-.','LineWidth',1.5)
22 hold off
23 title('Shear Force','FontSize',14,'FontName','Times New Roman')
24 xlabel('x (in m)','FontSize',13,'FontName','Times New Roman')

Prepared by I3-GCI-A2(6) 2022 − 23 Page 29 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

25 ylabel('V(x) (in kN)','FontSize',13,'FontName','Times New Roman')


26 grid on
27 legend('Case #1','Case #2','Case #3','FontSize',10,'FontName','Times New Roman')
28
29 subplot(2,1,2)
30 plot(data1(1,:),data1(3,:),'g-','LineWidth',1.5)
31 hold on
32 plot(data2(1,:),data2(3,:),'m--','LineWidth',1.5)
33 plot(data3(1,:),data3(3,:),'b-.','LineWidth',1.5)
34 hold off
35 title('Bending Moment','FontSize',14,'FontName','Times New Roman')
36 xlabel('x (in m)','FontSize',13,'FontName','Times New Roman')
37 ylabel('M(x) (in kN.m)','FontSize',13,'FontName','Times New Roman')
38 grid on
39 legend('Case #1','Case #2','Case #3','FontSize',10,'FontName','Times New Roman')

Note:
+ Lines 5-7, our .txt file contains three columns of integers, and [A,COUNT] = fscanf(FID,FORMAT)
reads and converts data from a text file into array A in column order.4

+ So, we specified to read as integers (%f), and create a 3 rows matrix ([3,inf]) for each row
containing x, V and M respectively.

Figure 14: Diagrams of V (x) and M(x) for all cases

Table 1: Extreme value of V (x), N(x) and σx for all cases

Case #1 Case #2 Case #3


V (x) 60kN 40kN 20kN
N(x) 90kN.m 80kN.m 50kN.m
σx 16.875MP a 15MP a 9.375MP a

4
its form is taken directly from an example of: [» help fscanf].

Prepared by I3-GCI-A2(6) 2022 − 23 Page 30 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

References
[1] K.S. POUV, 2015, MATLAB course book, Institute of Technology of Cambodia, Phnom Penh.

[2] Matlab introduction, javaTpoint, www.javatpoint.com, accessed 14 June 2023,


<https://www.javatpoint.com/matlab-introduction>.

[3] D. Houcque, 2005, INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS, V. 1.2,


Northwestern University.

[4] MATLAB Documentation, MathWorks® Help Center, www.mathworks.com, accessed 14 June 2023,
<https://www.mathworks.com/help/matlab/index.html?s_tid=hc_panel>.

Prepared by I3-GCI-A2(6) 2022 − 23 Page 31 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Appendix

A Table of saved results in TP2

Table 2: xVM_table1.txt

0.0 60.000 0.000 1.3 34.000 61.100 2.6 8.000 88.400 3.9 -18.000 81.900 5.2 -44.000 41.600
0.1 58.000 5.900 1.4 32.000 64.400 2.7 6.000 89.100 4.0 -20.000 80.000 5.3 -46.000 37.100
0.2 56.000 11.600 1.5 30.000 67.500 2.8 4.000 89.600 4.1 -22.000 77.900 5.4 -48.000 32.400
0.3 54.000 17.100 1.6 28.000 70.400 2.9 2.000 89.900 4.2 -24.000 75.600 5.5 -50.000 27.500
0.4 52.000 22.400 1.7 26.000 73.100 3.0 -0.000 90.000 4.3 -26.000 73.100 5.6 -52.000 22.400
0.5 50.000 27.500 1.8 24.000 75.600 3.1 -2.000 89.900 4.4 -28.000 70.400 5.7 -54.000 17.100
0.6 48.000 32.400 1.9 22.000 77.900 3.2 -4.000 89.600 4.5 -30.000 67.500 5.8 -56.000 11.600
0.7 46.000 37.100 2.0 20.000 80.000 3.3 -6.000 89.100 4.6 -32.000 64.400 5.9 -58.000 5.900
0.8 44.000 41.600 2.1 18.000 81.900 3.4 -8.000 88.400 4.7 -34.000 61.100 6.0 -60.000 0.000
0.9 42.000 45.900 2.2 16.000 83.600 3.5 -10.000 87.500 4.8 -36.000 57.600
1.0 40.000 50.000 2.3 14.000 85.100 3.6 -12.000 86.400 4.9 -38.000 53.900
1.1 38.000 53.900 2.4 12.000 86.400 3.7 -14.000 85.100 5.0 -40.000 50.000
1.2 36.000 57.600 2.5 10.000 87.500 3.8 -16.000 83.600 5.1 -42.000 45.900

Table 3: xVM_table2.txt

0.0 40.000 0.000 1.3 34.000 51.100 2.6 8.000 78.400 3.9 -18.000 71.900 5.2 -40.000 32.000
0.1 40.000 4.000 1.4 32.000 54.400 2.7 6.000 79.100 4.0 -20.000 70.000 5.3 -40.000 28.000
0.2 40.000 8.000 1.5 30.000 57.500 2.8 4.000 79.600 4.1 -22.000 67.900 5.4 -40.000 24.000
0.3 40.000 12.000 1.6 28.000 60.400 2.9 2.000 79.900 4.2 -24.000 65.600 5.5 -40.000 20.000
0.4 40.000 16.000 1.7 26.000 63.100 3.0 0.000 80.000 4.3 -26.000 63.100 5.6 -40.000 16.000
0.5 40.000 20.000 1.8 24.000 65.600 3.1 -2.000 79.900 4.4 -28.000 60.400 5.7 -40.000 12.000
0.6 40.000 24.000 1.9 22.000 67.900 3.2 -4.000 79.600 4.5 -30.000 57.500 5.8 -40.000 8.000
0.7 40.000 28.000 2.0 20.000 70.000 3.3 -6.000 79.100 4.6 -32.000 54.400 5.9 -40.000 4.000
0.8 40.000 32.000 2.1 18.000 71.900 3.4 -8.000 78.400 4.7 -34.000 51.100 6.0 -40.000 0.000
0.9 40.000 36.000 2.2 16.000 73.600 3.5 -10.000 77.500 4.8 -36.000 47.600
1.0 40.000 40.000 2.3 14.000 75.100 3.6 -12.000 76.400 4.9 -38.000 43.900
1.1 38.000 43.900 2.4 12.000 76.400 3.7 -14.000 75.100 5.0 -40.000 40.000
1.2 36.000 47.600 2.5 10.000 77.500 3.8 -16.000 73.600 5.1 -40.000 36.000

Prepared by I3-GCI-A2(6) 2022 − 23 Page 32 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

Table 4: xVM_table3.txt

0.0 40.000 0.000 1.3 34.000 51.100 2.6 8.000 78.400 3.9 -18.000 71.900 5.2 -40.000 32.000
0.1 40.000 4.000 1.4 32.000 54.400 2.7 6.000 79.100 4.0 -20.000 70.000 5.3 -40.000 28.000
0.2 40.000 8.000 1.5 30.000 57.500 2.8 4.000 79.600 4.1 -22.000 67.900 5.4 -40.000 24.000
0.3 40.000 12.000 1.6 28.000 60.400 2.9 2.000 79.900 4.2 -24.000 65.600 5.5 -40.000 20.000
0.4 40.000 16.000 1.7 26.000 63.100 3.0 0.000 80.000 4.3 -26.000 63.100 5.6 -40.000 16.000
0.5 40.000 20.000 1.8 24.000 65.600 3.1 -2.000 79.900 4.4 -28.000 60.400 5.7 -40.000 12.000
0.6 40.000 24.000 1.9 22.000 67.900 3.2 -4.000 79.600 4.5 -30.000 57.500 5.8 -40.000 8.000
0.7 40.000 28.000 2.0 20.000 70.000 3.3 -6.000 79.100 4.6 -32.000 54.400 5.9 -40.000 4.000
0.8 40.000 32.000 2.1 18.000 71.900 3.4 -8.000 78.400 4.7 -34.000 51.100 6.0 -40.000 0.000
0.9 40.000 36.000 2.2 16.000 73.600 3.5 -10.000 77.500 4.8 -36.000 47.600
1.0 40.000 40.000 2.3 14.000 75.100 3.6 -12.000 76.400 4.9 -38.000 43.900
1.1 38.000 43.900 2.4 12.000 76.400 3.7 -14.000 75.100 5.0 -40.000 40.000
1.2 36.000 47.600 2.5 10.000 77.500 3.8 -16.000 73.600 5.1 -40.000 36.000

Prepared by I3-GCI-A2(6) 2022 − 23 Page 33 of 40


Institute of Technology of Cambodia MATLAB (TP) Dr. Keang Sè POUV

B TP instruction

Prepared by I3-GCI-A2(6) 2022 − 23 Page 34 of 40


Institut de Technologie du Cambodge
Département de Génie Civil

TRAVAUX PRATIQUES
D’INITIATION À MATLAB III
Dr.–Ing. K.S. POUV

Pour
I3 GCI et I3 OAC

Phnom Penh, l’automne 2017


NOTA :
Le groupe TP sera divisé en quatre nômes. Chaque quatre nôme doit rédiger un compte-
rendu et rendre à l’enseignant dans les délais prévus comme suite :
- TP1 à rendre lors de la 4ème séance
- TP2 à rendre lors de la 8ème séance
Des comptes-rendus rendus après les créneaux indiqués ci-dessus seront refusés. Au moment où
les étudiants rendent leurs comptes-rendus, les questions orales seront leurs demandées.

CONSEILS :
Chaque compte-rendu doit contenir les scripts et les résultats clés de compilation ainsi que
les commentaires. Les comptes-rendus peuvent également être rédigés pendant les séances
de TP.

Dr.–Ing. K.S.POUV 2
TP 1
Prise en main de Matlab
Exercice 1 : Vecteurs, courbes et calculs formels
  
1. Définir la variable x =  , ,  et calculer y1=sin(x) et y2=cos(x). Calculer ensuite
6 4 3
2
y3=3tan (x) et y4=sin(2x)+cos(3x) en utilisant exclusivement les vecteurs y1 et y2
précédents.
2. On a . Calculer :

3. On considère les vecteurs suivants :

, et

a. Calculer t=u+3uv-uvw.
b. Calculer u , v , w ,||t||.
4. Définir la variable complexe a=[i, -i, 1+i, 1-i, 2+2i, 2-2i, …., 100+100i, 100-1ooi]. Puis calculer
le module complexe de chaque élément de a.
5. Calculer la factorielle de 100 (i.e., 100 !) par deux méthodes possibles. On peut essayer
d'utiliser par exemple : (1) la commande « cumprod » et (2) la commande « for ».

Exercice 2 : Manipulation de matrices


1. Définir le vecteur U = [-1 -2 ... -14 -15]. Quelle est la taille de ce vecteur ? Définir le vecteur
V contenant les cinq premiers et les cinq derniers éléments de U. Définir ensuite le
vecteur W = [1/2 1 ... 7 15/2] et le vecteur X = [1 1/22 … 1/142 1/152], tous à partir du vecteur
U. Calculer P=2W2X sachant que P a le même nombre d'éléments que U.
1 2 3 4 5 6 7 8 9 10 
2. Définir la matrice M = 11 12 13 14 15 16 17 18 19 20 . Quels sont ses dimensions
21 22 23 24 25 26 27 28 29 30
1 2
3 7
m x n ? Extraire de cette matrice, la matrice N = 11 12  et la matrice P =  . Extraire
 23 27
21 22
de la matrice M, la matrice Q obtenue en prenant dans la matrice M une colonne sur 2.
3. Définir une matrice A de dimensions 4 x 4 à l’aide de la commande magic. Déterminer la
matrice B = A/15. Combien de nombres dans cette matrice B sont compris entre 0.4 et 0.8
? Ou sont-ils situés ? (commande find) Construire alors la matrice C obtenue à partir de la
matrice B en remplaçant tous les nombres de B inférieurs à 0.8 par 0, et ceux supérieurs
ou égaux à 0.8 par 1.
4. Résoudre le système d’équation linéaire ci-dessous par deux approches possibles :
x + y = 14
y + z = 10
x-y=4
z-t=1
Vérifier vos résultats pour chaque approche.

Dr.–Ing. K.S.POUV 3
Exercice 3 : Fonctions graphiques
1. Graphiques 2D :
a. Créer un vecteur temps t (t en secondes) allant de 0 à 10 avec 11 points.
b. Créer un signal s1(t) = 1 – e-t puis l’afficher en fonction du temps.
c. Créer deux autres signaux définis par s2(t) = 1 – e-2t et s3(t) = 1 – e-4t, puis les afficher
dans la même figure.
d. Faire les modifications pour afficher ces 3 signaux respectivement en bleu, en rouge
et en vert. Faire apparaître en plus des couleurs des astérisques (*), des croix et des
points aux différentes valeurs constituant les 3 signaux. Ajouter une légende à vos
courbes et les titres correspondant à l’axe d’abscisse et l’axe d’ordonnée.
e. On souhaite maintenant changer la variable temporelle et la définir avec 10 fois plus
de points. Faire les modifications nécessaires.
f. Afficher ces trois signaux dans une même figure, mais cette fois-ci dans 3 graphiques
séparés sur deux lignes, dont 2 graphiques en haut et 1 graphique en bas (figure 0).
Les propriétés des courbes sont les mêmes que celles du premier cas.
Graphique 1 Graphique 2
Graphique 3
Figure 0. Format de l’illustration pour la question f.
g. Ajouter maintenant dans le troisième graphique la courbe harmonique de forme
x(t ) = A cos(t +  ) avec ω = 2, A = 3 et φ=0.5. Faire apparaître les propriétés de cette
courbe comme le titre, le couleur, etc.
2. Graphiques 3D :
Illustrer, dans un même graphique, les surfaces des deux fonctions suivantes :
- z1 = 100sin(r)/r, where r = x 2 + y 2 (chapeau Mexicain)
- z2 = x – y + 50
Mettre également les titres pour les trois axes.

Exercice 4 : Ecriture des fonctions


1. Créer une fonction pour convertir l’unité de vitesse de km/h en m/s. En utilisant cette
fonction, convertir alors la vitesse de 120km/h en m/s.
2. Créer une fonction pour convertir l’unité de vitesse de m/s en km/h. En utilisant cette
fonction, convertir alors la vitesse de 3m/s en km/h.
3. Ecrire une fonction nommée polaire, prenant comme arguments d’entrée les
coordonnées cartésiennes (x,y) d’un point et renvoyant en sortie les coordonnées
polaires (r,θ) de ce point, où θ est en degré. Penser à commenter le code de manière
qu’un utilisateur puisse utiliser l’aide en ligne pour s’informer sur cette nouvelle fonction.
À l'aide de cette fonction polaire créée, écrire ensuite un nouveau script (fichier
d'extension .m) pour :
a. Convertir en coordonnées polaires les coordonnées cartésiennes de A1(43,3 ; 25) et
A2(65,78 ; 23,94).
b. Illustrer graphiquement le triangle OA1A2 en coordonnées polaires, où O(0 ; 0) est le
repère des axes polaires.
c. Déterminer l’angle α formé par les deux vecteurs OA1 et OA2 .

Dr.–Ing. K.S.POUV 4
TP 2
Mini-projet : Efforts internes d’une poutre sur appuis
simples

On considère une poutre de longueur L et de section a x b et ayant un module de Young E et


une résistance σa (figure 1). La poutre est posée sur deux appuis simples et soumise à une
charge répartie w appliquée à une distance A de l’appui gauche.

Figure 1. Poutre soumise à une charge répartie.

Ecrivez sous Matlab, un algorithme permettant d’obtenir :


- le moment d’inertie d’axe vertical Iz
- les réactions des deux appuis (R1 et R2)
- l’effort tranchant (V)
- le moment fléchissant (M)
- la contrainte en flexion (σx) en position du moment maximal
Maintenant, appliquez votre programme pour les cas suivants :
1. a = 20 cm, b = 40 cm, L = 6 m, A = 0 m, B = 6m, w = 20 kN/m et σa= 28 MPa.
2. a = 20 cm, b = 40 cm, L = 6 m, A = 1 m, B = 4m, w = 20 kN/m et σa= 28 MPa.
3. a = 20 cm, b = 40 cm, L = 6 m, A = 2 m, B = 2m, w = 20 kN/m et σa= 28 MPa.
Ensuite, on souhaite, pour chaque cas :
- déterminer les valeurs maximales de V, de M et de σx. Aussi, vérifiez si la contrainte
maximale σx,max est inférieure ou égale à la contrainte admissible σa. S’il est le cas,
affichez le texte suivant « contrainte maximale <= contrainte admissible, VERIFIE ! ». S’il
n’est pas le cas, affichez « contrainte maximale > contrainte admissible, NON VERIFIE ! ».
- tracer les diagrammes V(x) et M(x) dans deux graphiques séparées, mais alignés
verticalement sur la même figure. Indiquez également les valeurs Vmin, Vmax et Mmax en
bonnes positions sur les courbes correspondant.
- enregistrer les résultats de chaque cas dans un fichier en format « .txt » de façon
suivante : valeurs de x en 1ère colonne, valeurs de V en 2ème colonne et valeurs de M en
dernière colonne.
Enfin, on souhaite comparer et analyser les résultats des 3 cas d’études :
- tracez les diagrammes V(x) et M(x) pour les 3 cas en utilisant uniquement les fichiers
.txt enregistrés auparavant (Conseils : mettez les 3 courbes de même paramètre sur le
même graphique, puis alignez verticalement les 2 graphiques sur la même figure.)
- dans votre rapport, faire un tableau récapitulatif des valeurs extrêmes de V, de M et
de σx entre les 3 cas pour comparer.
Conseils : Pour toutes les figures, mettez les échelles des axes d’abscisse et d’ordonnée de
manière qu’elles sont plus grandes que les valeurs extrêmes des paramètres correspondant

Dr.–Ing. K.S.POUV 5
de la figure. En bref, illustrez vos figures de telle manière qu’elles sont bien agréables à lire :
couleur de la courbe, épaisseur de la courbe, noms des deux axes, légendes, titre, etc. (voir
les exemples sur la figure 2).

(a)

(b)

Figure 2. Exemple des illustrations agréables à lire : (a) une courbe, et (b) plusieurs courbes.

Dr.–Ing. K.S.POUV 6

You might also like