You are on page 1of 30

Advanced Mathematical Methods

for Civil Engineering Applications

Wonsiri Punurai, PhD

Department of Civil Engineering


Room 6391, EG Building 3
Faculty of Engineering, Mahidol University
Personal Web: www.egmu.net/~civil/wonsiri
MATLAB 103

z MATLAB M-files (script files)

z MATLAB Mat-files (binary files)

z Importing Data into /Exporting Data from MATLAB

z Displaying output on the command window

z Input and Output


MATLAB M-files

z Semi-interactive mode – commands can be saved,


refined and reused in MATLAB editor as needed.
z These files end in “.m” in all platforms.
z Any word processor can also be used to edit
and save files as text.
z Good for many problem types
(ranging from small simple
to large and complex models).
z Open built-in editor, select from menu:
File > New > M-file
Sample M-files - I

z Try this example, In MATLAB command window, type

This single line defines two matrices (a and b) and computes their product (c).
z This can be done in the same fashion in m-file format.
– Open new m-file using editor.
– Now type the following
% A sample file to compute a product of two matrices

- Save the file as sample.m


- Go back to Command Window and type sample to execute file.
Sample M-files - II

z Try another example, In MATLAB command window,


type
Plot of simple trigonometric functions

Sample M-files - III 1.5


sin(t)
cos(t)
sin(t)*cos(t)
1

0.5

z Simple trigonometry plot


0

-0.5

-1

-1.5
0 1 2 3 4 5 6
θ (radius)
Function m-files
Example function m-files
MATLAB Mat-files

z These files are convenient to store information that needs to be


reused.
z These binary files end in “.mat”.
z Use the “load” and “save” commands at the MATLAB command line.
z They are platform independent.
Sample MAT-files

z From previous example, we have created and executed m-file called


sample.m
% A sample file to compute a product of two matrices

Type whos in the command window, ones will see a, b, c matrices.


Let say we want to save these matrices (or variables) for later.
Type save sample.mat
Now try typing clear and then type load sample.mat
Type whos again and you will see matrices a,b and c in workspace.
Importing Data into MATLAB

z Suppose that we have a data file (called


road_elevation_centerline.txt) containing road elevation
data at center line. The information provided includes:
1) column 1 = station no. (ranging from 1 to 5)
2) column 2 = elevation at each station

1 12.49
2 35.02
3 5.45
4 28.29
5 9
Importing Data (I)

z Method 1 – in MATLAB command window, use load command


>> load road_elevation_centerline.txt

z An array name road_elevation_centerline which has dimensions


5x2 was just being loaded.
z We know that first and second column corresponding to station
# and elevation at centerline respectively.
z One can produce two new array variables
station no = road_elevation_centerline (:,1) ;
center_elev = road_elevation_centerline (:,2) ;
Importing Data (II)

z Method 2 – To import data go to the Editor Window


z Select Import from the File pull-down menu
Importing Data (III)

z Method 3 – use a function called dlmread


>> road_elevation_centerline = dlmread(‘road_elevation_centerline.txt’)
>> station no = road_elevation_centerline(:,1);
>> center_elev = road_elevation_centerline(:,2);
Exporting data from MATLAB

z Save data into a file in any specific format.


z Use a function called dlmwrite to specify any
delimiters needed.
>> load road_elevation_centerline.txt
>> dlmwrite(‘road_elevation_centerline1.txt’,ans)
>> save road_elevation_centerline2.txt
Displaying output

z Use function ‘disp’ to display to output screen.


z Typically used in conjunction with ‘num2str’ to
convert numerical to string variables
Input and Output
Text Output
System of linear equations

BASIC
System of linear equations (3 unknowns)

z In mathematics, a system of linear equations is a


collection of linear equations involving the same set
of variables.
4 x1 + 3 x2 + 4 x3 = 35
4 x1 + 6 x2 + 8 x3 = 22
3 x1 + 6 x2 + 6 x3 = 40

is a system of three equations in a three variables


x1, x2, x3.
Solution of linear equations (by hand-I)

z A solution to a linear system is an assignment of numbers to the


variables such that all the equation are simultaneously satisfied.
Given

4 x1 + 3 x2 + 4 x3 = 35
In the matrix form, ⎡4 3 4⎤ ⎡ x1 ⎤ ⎡35⎤
4 x1 + 6 x2 + 8 x3 = 22 ⎢4 6 8 ⎥ ⎢ x ⎥ = ⎢22⎥
we have Ax =b ⎢ ⎥⎢ 2 ⎥ ⎢ ⎥
3 x1 + 6 x2 + 6 x3 = 40
⎢⎣3 6 6⎥⎦ ⎢⎣ x3 ⎥⎦ ⎢⎣40⎥⎦

To obtain [x], one must compute det(A) and A-1 0.5 -0.25 0
-72-192-72
⎡ 4 3 4⎤ 4 3 0 -0.5 0.667
det(A) = ⎢4 6 8 ⎥ 4 6 = 24
⎢ ⎥ -0.25 0.625 -0.5
⎢⎣3 6 6⎥⎦ 3 6 144+72+96
Solution of linear equations (by hand-II)

z From Ax =b
z Multiply both sides by A-1
A-1 Ax = A-1 b
I*x = A-1 b
z A product of A-1b gives x

⎡ x1 ⎤ ⎡ 0.5 − 0.25 0 ⎤ ⎡35⎤ ⎡ x1 ⎤ ⎡ 12 ⎤


⎢x ⎥ = ⎢ 0 − 0 . 5 0 . 667 ⎥ ⎢22⎥ ⎢ x ⎥ = ⎢15.667 ⎥
⎢ ⎥ ⎢
2 ⎥⎢ ⎥ ⎢ 2⎥ ⎢ ⎥
⎢⎣ x3 ⎥⎦ ⎢⎣− 0.25 0.625 − 0.5 ⎥⎦ ⎢⎣40⎥⎦ ⎢⎣ x3 ⎥⎦ ⎢⎣ − 15 ⎥⎦
Solution of linear equations (by MATLAB – I)

x = inv(a)*b;
x=

12.0000
15.6667
-15.0000
Solution of linear equations (by MATLAB – II)
Problems in relation to CE work

Example: The design of a steel truss


z Structural analysis in which the equations of
equilibrium are applied at each node and written in
terms of the unknown element internal forces [f] and
known externally applied load [P]. Both terms are
related by

[B][f] = [P]

where [B], in this case, represents statics matrix.


System of linear equations (n unknowns)
Example: Member forces in truss

1. Write down matrices [B], [f], and [P] at each node.


2. Solve for [f] matrix for each node..
Solution: Member forces in truss (I)

F3 cos 60 = F2
F3 sin 60 = 750

⎡− 1 cos 60⎤ ⎡ F2 ⎤ ⎡ 0 ⎤
=
⎢0
⎣ 1 ⎥⎦ ⎢⎣ F3 ⎥⎦ ⎢⎣750⎥⎦

⎡cos 30 − cos 60⎤ ⎡ F1 ⎤ ⎡ 0 ⎤


⎢ sin 30 sin 60 ⎥ ⎢ F ⎥ = ⎢1000⎥
⎣ ⎦⎣ 3 ⎦ ⎣ ⎦
⎡ F1 ⎤ ⎡ 500.000 ⎤
⎢ F ⎥ = ⎢433.0127 ⎥
⎢ 2⎥ ⎢ ⎥
⎢⎣ F3 ⎥⎦ ⎢⎣866.0254 ⎥⎦

⎡− cos 30 1⎤ ⎡ F1 ⎤ ⎡ 0 ⎤ ⎡cos 60 − 1⎤ ⎡ F3 ⎤ ⎡ 0 ⎤
⎢ sin 30 0⎥ ⎢ F ⎥ = ⎢250⎥ ⎢ sin 60 0 ⎥ ⎢ F ⎥ = ⎢750⎥
⎣ ⎦⎣ 2 ⎦ ⎣ ⎦ ⎣ ⎦⎣ 2 ⎦ ⎣ ⎦
What we just did is so called “Matrix Formulation”
Requirements for a solution

Later

You might also like