Sunteți pe pagina 1din 20

CE 205-MATLAB for Civil Engineers

Irfan Turk
Fatih University, 2013-14

What is MATLAB?
MATLAB stands for MATrix LABoratory.
MATLAB is a high-performance language for technical

computing.

Math , computation, and numerical calculations


Algorithm development
Data acquisition
Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development, including graphical user interface
building
Many toolboxes for different applications

MATLAB ENVIRONMENT
Command Window
Type commands

Current Directory
View folders and m-files

Workspace
View program variables

Command History
View past commands

Editor
Writing the code

Variables & Arrays


Variables :

No need for types, and pre-defining.

int b;
double a;
float c;

Variable names must begin with a letter, followed by any

combination of letters, numbers and the underscore (_)


character

Example:
>>1B=1;
>>_A1=-8;

All variables are created with double precision(as matrices)

unless specified.

Example:
>>x=1;
>>x1=-8;

Above, the variables are 1x1 matrices with double precision


Note: Matlab is case sensitive.

Variables & Arrays

Arrays :

An array consisting of one element is called a scalar. i.e. A=4.

When B=[1, 2, 3], then B is an array of numbers.


Scalars are also treated as arrays by MATLAB (1 row and 1 column).
Row and column indices of an array start from 1.(not zero as in C,
or in others.
Arrays can be classified as vectors and matrices.
Row 1
Row 2
Row 3
array(2,4)

Row 4
Col 1 Col 2 Col 3 Col 4 Col 5

Variables & Arrays


Arrays :

An array consisting of one element is called a scalar.

i.e. A=4. When B=[1, 2, 3], then B is an array of


numbers.

Scalars are also treated as arrays by MATLAB (1 row

and 1 column).

Row and column indices of an array start from 1.(not

zero as in C, or in others.

Arrays can be classified as vectors and matrices.

Vector and Matrix


Vector: Array with one dimension
C=[1 2 3 4]

1x4 array 4 elements, row vector

1
B= 3
5

3x1 array 3 elements, column vector

Matrix: Array with more than one dimension


A=

1 2
3 4
5 6

3x2 matrix 6 elements


A(3,2)=6
Row #

Column #

B(3)=5

C(2)=2

Practice Exercise 2.1


Type the following expressions into MATLAB at the

command prompt, and observe the results:


5+2
5*2
3+2*(4+3)
2.54*8/2.6
6.3-2.1045
3.6^2
1+2^2
sqrt(5)
cos(pi)

Practice Exercise 2.2


Which of the following names are allowed in MATLAB? Make your

predictions, then test them with the isvarname, iskeyword, and which
commands.
1.
test
2. Test
3. if
4. my-book
5. my_book
6. Thisisoneeverylongnamebutisitstillallowed?
7. 1stgroup
8. group_1
9. zzaAbc
10. 34wAwy?12#
11. sin
12. log

Operators
+ addition
a+b
- subtraction
a-b
* multiplication
a*b
/ division
a/b
^ power
a^b
complex conjugate transpose a (apostrophe)

Examples:
Example 1: For a given A(3by3), and B(3by3) matrices,

find A+B, A-B, A*B, B*A,A, B, A^2, and B^2


Example 2:
2*x+3*y+4*z=-2,
4*x -y+ z= 3,
-3*x +3*y+ z=-4
Write the above system by using matrices and find
unkowns (x,y,z)

Operators(Element by element)
.* multiplication
./ division
.^power

a .* b
a ./ b
a .^ b

Example 3: In Example 1, calculate, A.*B, B.*A, A.^2,

B.^2, A./B, and B./A. What are the differences between


the calculations A.*B and A.*B, A^2 and A.^2, A/B and
A./B?

Examples
A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9

Example 4: For a given A on the

left, do the same or similar


operations as below.

x = A(1,:)

y = A(3 ,:)

b = x .* y

c=x./y

d = x .^2

x=

y=

b=

c=
0.14 0.25 0.33

d=

1 2 3

7 8 9

7 16 27

Examples
Example 5(in book, it is Example 2.1-2nd question):

Volume
V=1000
Temperature
T=300
Pressure
P=100
Molecular weight MW=29
Gas Constant
R=8.314
What is mass=m=? Solve it in Matlab
(PV=nRT, and n=m/MW)

Numeric Display Formats


Matlab Command

Display

format short

4 decimal digits

format long

14 decimal digits

3.145214569

format short e

4 decimal digits scientific notation

1.1234e+002

format long e

format +

14 decimal digits scientific notation


2 decimal digits only real values are
displayed
4 decimal digits engineering
notation(rounding)
14 decimal digits engineering
notation(rounding)
+,-,blank (for positive, negative and
zero numbers)

format rat

fractional form

355/54

format short g

MATLAB selects the best format

123.46

format long g

MATLAB selects the best format

3.1235468

format bank
format short eng
format long eng

Example
3.1416

3.12345678901234e+002
3.14
3.1235e+00
3.12345678901234e+000
+/-/

Saving Your Work


diary on / diary(filename) saves your work in the

fine name specified


save <filename> preserves your variables
load <filename> brings your data back, after clearing
whos command shows your variables again

Script M-Files
Using command window is an easy tool for

calculations. But once you close the Matlab, you loose


the calculations if you do not use the extra commands
explained above.
To overcome this issue, it is better to write the codes in
an M file. In this case, your work will be saved as an M
file into the directory you work.

Example
Example:6 (problem 2.20 from the book)
Create a matrix A equal to [-1/3,0,1/3,2/3], and use each
of the built-in format options below to display the
results:
format short (default format)
format long
format short e
format rat
format short g

Example
Example 7:
X=linspace(0,pi,100) % X is a vector
Y=sin(X)
% Y is a vector
plot(X,Y)
title(Plotting)
xlabel(This is x label)
ylabel(This is y label)

Example
Example 8: Plot the graphs below.
a) 0x 2*pi, 1+cos(x)
b) -2*pix pi, sin(x)-1
c) -4x 4, x^2-4

S-ar putea să vă placă și