Sample M File

Document Sample
Sample M File
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



MATLAB M-files (script files)



MATLAB Mat-files (binary files)



Importing Data into /Exporting Data from MATLAB



Displaying output on the command window



Input and Output

MATLAB M-files



Semi-interactive mode – commands can be saved,

refined and reused in MATLAB editor as needed.

These files end in “.m” in all platforms.

Any word processor can also be used to edit

and save files as text.

Good for many problem types

(ranging from small simple

to large and complex models).

Open built-in editor, select from menu:

File > New > M-file

Sample M-files - I



Try this example, In MATLAB command window, type





This single line defines two matrices (a and b) and computes their product (c).

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



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





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



These files are convenient to store information that needs to be

reused.

These binary files end in “.mat”.

Use the “load” and “save” commands at the MATLAB command line.

They are platform independent.

Sample MAT-files



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



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)



Method 1 – in MATLAB command window, use load command

>> load road_elevation_centerline.txt



An array name road_elevation_centerline which has dimensions

5x2 was just being loaded.

We know that first and second column corresponding to station

# and elevation at centerline respectively.

One can produce two new array variables

station no = road_elevation_centerline (:,1) ;

center_elev = road_elevation_centerline (:,2) ;

Importing Data (II)



Method 2 – To import data go to the Editor Window

Select Import from the File pull-down menu

Importing Data (III)



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



Save data into a file in any specific format.

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



Use function ‘disp’ to display to output screen.

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)



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)



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)



From Ax =b

Multiply both sides by A-1

A-1 Ax = A-1 b

I*x = A-1 b

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

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


Share This Document


Related docs
Other docs by nicknameD
Julian Date Calander
Views: 672  |  Downloads: 7
Daily Medicine Log
Views: 2641  |  Downloads: 34
Interest Calculations Formula
Views: 1710  |  Downloads: 14
Creating A Timeline
Views: 95  |  Downloads: 6
Personal Invoice Template
Views: 1033  |  Downloads: 6
Microsoft Excel Project
Views: 152  |  Downloads: 4
Home Inventory Xls
Views: 317  |  Downloads: 5
Mortgage Payment Calculation
Views: 9  |  Downloads: 0
Julian Date Calender
Views: 1949  |  Downloads: 4
Own Calendar Template
Views: 25  |  Downloads: 0
by registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!