Вы находитесь на странице: 1из 6

CHE555 NUMERICAL METHODS & OPTIMIZATION

LAB 1: INTRODUCTION TO MATLAB


Part 1: Introduction on Matlab interface
A typical Matlab interface as shown below:

Note: To change current interface into this typical interface Click Layout Select Layout

Click on Default Then click Layout again Click on command history Choose Docked
a) Command Window To type commands and usually answers (or error messages) will be
appear at this area. >> means Matlab waiting for further instruction.
b) Workspace window Variables that has been define in command window (as well in mfiles) should be listed here.
c) Command history window The past commands are remembered here. We can re-run the
previous command or to edit the command by drag it to command window.

Simple entering Commands


ans
Most recent answer
clc
Clear Command Window
format Set Command Window output display format
Note:
format long for 16 decimal points
format short for 4 d.p.
format short e for x 10power

Example: pi
>> format long
>> pi
ans =
3.141592653589793
>> format short
>> pi
ans =
3.1416
>> format short e
>> pi
ans =
3.1416e+00
Part 2: Simple arithmetic operator
a)
b)
c)
d)
e)

Additional: +
Subtraction: - ,
Multiplication: *
Division: /
Power: ^

Example:
>>3+5-7
ans=
1
Or
>>t=3+5-7
t=
1
>>T=2*t+2
T=
4
Note: Variable names Capital letter (A) and small letter (a) will brings difference variable
value.

Part 3: Elementary Mathematic


(a) Exponent
Example: 5 x e-8
>> 5*exp(-8)
ans =
0.0017

(b) Natural logarithm (ln)


Example: ln 100
>> log(100)
ans =
4.6052

(c) Common (base 10) logarithm


Example: Log10 5
>> log10(5)
ans =
0.6990

(d) Power and square root


Example: (15)
>> sqrt(15)
ans =
3.8730
Or
>> 15^.5
ans =
3.8730

Part 4: Trigonometric
(a) Tangent, sine & cosine of argument
Example: cos (15) in radian
>> cos(15)

% without d : radians

ans =
-0.7597

Example: cos (15) in degree


>> cosd(15)

% with d: degree

ans =
0.9659

(b) Inverse of tangent, sine & cosine


Example: cos-1 (0.5) in radian
>> acos(0.5)

% without d : radians

ans =
1.0472

Example: cos-1 (0.5) in degree


>> acosd(0.5)

% with d: degree

ans =
60.0000

Part 5: Define the variable in matrix form


Example:
>> A = [1 2 3 4 5] % row vector
A=
1 2 3 4 5

%column vector (semi colons ; means next row)

>> B = [1;2;3;4;5]
B=
1
2
3
4
5
>> C = [1 2; 3 4; 5 6]
C=
1 2
3 4
5 6

Commands relate to matrix C:


C(row, column)
Example:
>> C(1,2)
ans =
2
>> C(2,1)
ans =
3
%: for all data

>> C(2,:)
ans =
3

Part 6: Define the large data set


Variables name = initial value: increment: final value
Example:
<< A=1:2:10
A=
1

>> max (A)


ans =

% largest value in vector


9

>> min (A)


ans =

% smallest value in vector


1

>> size (A)


ans =

%size of the matrix (row,column)


1

Part 7: Logical
In Matlab the integer 1 represents true and 0 represent false.
Example:
>>X = 14
>>X > 10
ans =
1
Example:
>>Y=[0 -1 2 4]
>>Y>2
ans =
0001
Command

Result

Description

Y==2

[0 0 1 0]

Entries equal to 2

Y>2

[0 0 0 1]

greater than 2

Y>=2

[0 0 1 1]

greater than or equal to

Y<2

[1 1 0 0]

less than

Y<=2

[1 1 1 0]

less than or equal to

Y~=2

[1 1 0 1]

not equal to

Advanced uses of logical: Operators of & (and) and | (or)


>>Y>=0&Y<=1
ans =
1000

>>Y<0|Y>1
ans =
0111

Вам также может понравиться