Académique Documents
Professionnel Documents
Culture Documents
Example
>> 7+8/2
ans =
11
>> (7+8)/2
ans =
7.5000
>> 4+5/3+2
ans =
7.6667
>> 5^3/2
ans =
62.5000
>> 5^(3/2)
ans =
11.1803
>> 27^(1/3)+32^0.2
ans =
5
>> 27/3^2-32^0.2
ans =
1
5.585714285714285e+001
format short g Best of 5-digit fixed or floating point. >> 391/7
ans =
55.857
format long g Best of 15-digit fixed or floating point. >> 391/7
ans =
55.8571428571429
format bank Two decimal digits. >> 391/7
ans =
55.86
format rat Ratio of small integers. >> 20/8
ans =
5/2
format compact Eliminates empty lines to allow more lines with information displayed on
the screen.
format loose Adds empty lines (opposite of compact).
The hyperbolic trigonometric functions are sinh(x), cosh(x), tanh(x) and coth(x).
Rounding functions
-6
rem(x,y) Returns the remainder after x is >> rem(22,5)
divide by y. ans =
2
sign(x) Signum function. >> sign(-4.5)
Returns 1 if x 0 . ans =
Returns -1 if x 0 . -1
Returns 0 if x 0 .
Assignment Calculate:
28.5 3 2 1500
1. ?
112 37.3
2.
24 4.53 ?
e 4.4 log(12560)
tan ln 8
5 2 7 6 ?
3. cos sin
6 8 7 2
Example
>> a=12
a =
12
>> B=4
B =
4
>> C=(a-B)+40-a/B*10
C =
18
>> a=12;
>> B=4;
>> C=(a-B)+40-a/B*10;
>> C
-6-
C =
18
There are seventeen keywords that are reserved by MATLAB. Cannot be used as variable names.
break case catch continue else elseif end for function global
if otherwise persistent return switch try while
pi The number .
eps The smallest difference between two numbers, which is approximately 2.2204e-016.
j Same as i.
Command Outcome
clear Removes all variables from the memory.
clear x y z Removes only variable x, y, and z from the memory.
who Display a list of the variables currently in the memory.
whos Display a list of the variables currently in the memory and their size together
with information about their bytes and class.
Assignment
1. Define two variables: alpha=5/9, beta=/7. Using these variables, show that the following
trigonometric identity is correct by calculating the value of the left-and right-hand sides of the
equation.
1
sin sin cos cos
2
2. The temperature dependence of vapor pressure p can be estimated by the Antoine equation:
B
ln p A
C T
Where ln is the natural logarithm, p is in mm Hg, T is in Kelvin, and A,B, and C are material
constants. For toluene(C6H5CH3) in the temperature range from 280 to 410 K the material
constants are: A = 16.0137, B = 3096.52, and C = -53.67. Calculate the vapor pressure of toluene
at 315 and 405 K.
-8-
Scalar 25
Vector
Row vector 10 20 30
1
5
Column vector
6
7
2 3 1 2 10
matrix ,
4 5 22 5 6 1 23
Variable_name = [m:q:n]
Or Variable_name = m:q:n
q is step size
n is end value
Variable_name=linspace(xi , xf ,n)
xf is end value
Example
>> PNTAH=[2,3,4 5]
PNTAH =
2 3 4 5
>> PNTAV=[2;3;4;5]
PNTAV =
2
3
4
5
>> x=[1:2:14]
x =
1 3 5 7 9 11 13
>> y=[-3:7]
y =
-3 -2 -1 0 1 2 3 4 5 6 7
>> w=linspace(2,12,7)
w =
2.0000 3.6667 5.3333 7.0000 8.6667 10.3333 12.0000
Variable_name=[ 1st row element; 2nd row element; 3rd row element; ; last row element+
Example
4 76 81
21 32 40
>> b=[3 -4 5;3,4,9]
b =
3 -4 5
3 4 9
zeros(m,n) [0]mn
ones(m,n)[1]mn
eye(n)[I]nn
Example
>> zr=zeros(3,4)
zr =
0 0 0 0
0 0 0 0
0 0 0 0
>> ne=ones(4,3)
ne =
1 1 1
1 1 1
1 1 1
1 1 1
>> idn=eye(3)
idn =
1 0 0
0 1 0
0 0 1
The transpose operator is applied by typing a single quote ' following the variable to be transposed.
- 11 -
Example
>> aa=[2 7 9]
aa =
2 7 9
>> bb=aa'
bb =
2
7
9
>> C=[1 5 -2;4 5 8]
C =
1 5 -2
4 5 8
>> D=C'
D =
1 4
5 5
-2 8
2.5 Array Addressing
Vector
Example
>> VCT=[3 6 9 7 5 -2 8]
VCT =
3 6 9 7 5 -2 8
>> VCT(3)
ans =
9
>> VCT(5)
ans =
5
>> VCT(3)+VCT(5)
ans =
- 12 -
14
>> VCT(3)^VCT(6)
ans =
0.0123
Matrix
For a matrix named ma, ma(i,j) refers to the element in row i and column j.
Example
For a vector:
va(:) Refers to all the elements of the vector va (either a row or column vector).
Example
A(:,m,n) Refers to the elements in all rows between column m and n of matrix A.
A(m,n,:) Refers to the elements in all columns between row m and n of matrix A.
A(m:n,p:q) Refers to the elements in rows m through n and column p through q of matrix A.
Example
Example
9 7 -1
20 -5 2
The multiplication operation * is executed by MATLAB according to the rules of linear algebra. If A and
B are two matrices, the operation A*B can be carried out only if the number of columns in matrix A is
equal to the number of rows in matrix B.
Example
Symbol Description
.* Multiplication
.^ Exponentiation
./ Right division
.\ Left division
- 17 -
A [a1 a2 a3 a n ]
B [b1 b2 b3 bn ]
A B [a1 b1 a 2 b2 a3 b3 a n bn ]
A . * B [a1b1 a 2 b2 a3b3 a n bn ]
A .^ B [a1 1 a 2 b a3 b a n b
b 2 3
n
]
A . / B [a1 / b1 a 2 / b2 a3 / b3 a n / bn ]
A . \ B [a1 \ b1 a 2 \ b2 a3 \ b3 a n \ bn ]
A c [a1 c a 2 c a3 c a n c]
A * c [a1 * c a 2 * c a3 * c a n * c]
A .^ c [a1 a 2 c a3 c a n c ]
c
A / c [a1 / c a 2 / c a3 / c a n / c]
A \ c [a1 \ c a 2 \ c a3 \ c a n \ c]
Assignment
1. Write only three commands to compute the volume of cone, which have various height and
radius as shown in table, by using a following formula
1
V r 2 h
3
r 1 2 3 4 5 6 7 8 9 10
H 5 10 15 20 25 30 35 40 45 50
V
2. Define x and y as the vectors x = 2,4,6,8,10 and y=3,6,9,12,15. Then use them in the following
expression to calculate z using element-by-element calculations
2
y yx
z x y x
x
CH 4 Script Files
-5.2f
Flag Field width Conversion character (required)
(optional)
i Integer.
Example Write a script files to calculate the mean and the standard deviation of sample.
x i
x i 1
n
n
x x
2
i
SD i 1
n 1
Type I
x=[1 2 3 4 5 6];
n=length(x);
mean_x=sum(x)/n;
SD=sqrt(sum((x-mean_x).^2)/(n-1));
disp([mean_x,SD])
Type II
CH 5 Two-Dimensional Plots
plot(x,y)
Plot(x,y,line specifiers,PropertyName,PropertyValue)
Line Specifiers
Some examples:
plot(x,y) A blue solid line connects the point with no markers (default).
plot(x,y,*) The points are marks with * (no line between the points).
plot(x,y,g:d) A green dotted line connects the points that are marked with diamond markers.
filled markers.
MarkerFaceColor (or Specifies the color of the filling Color specifiers from the table
markerfacecolor) for filled markers. above, typed as string.
Example
Plot the function y=3x3-20x+12 and its first and second derivatives, for -2x4, all in the same
plot
Solution
140
120
100
80
60
40
20
-20
-40
-2 -1 0 1 2 3 4
- 25 -
fplot(function,limit,line specifiers)
function: The function can be typed directly as a string inside the command.
limits: The limits is a vector with two element that specify the domain of x
[xmin,xmax], or a vector with four elements that specifies the domain of x
and the limits of the y-axis [xmin,xmax,ymin,ymax].
Line specifiers : The line specifiers are the same as in the plot command.
10
-2
-4
-6
-8
-3 -2 -1 0 1 2 3
xlabel(text as string)
ylabel(text as string)
>> x=[-2:0.001:4];
>> y=3*x.^3-20*x+12;
>> yd=9*x.^2-20;
>> ydd=18*x;
>> plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
>> x=[10:0.1:22];
>> y=95000./x.^2;
>> plot(x,y,'-','LineWidth',1.0)
>> xlabel('DISTANCE (cm)')
>> ylabel('INTENSITY (lux)')
>> title('Light Intensity as a Function of Distance')
>> axis([8 24 0 1200])
>> grid on
Light Intensity as a Function of Distance
1200
1000
800
INTENSITY (lux)
600
400
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
- 27 -
global variable_name
Example Write a function files to calculate the mean and the standard deviation of sample.
x i
x i 1
n
n
x x
2
i
SD i 1
n 1
function [mean_x,SD]=mean_SD(x)
n=length(x);
mean_x=sum(x)/n;
SD=sqrt(sum((x-mean_x).^2)/(n-1));
- 28 -
Ch 7 Programming in MATLAB
x=2;
if x>=1
x=x+3;
end
- 29 -
disp(x)
20 x 5
f ( x) 5 x 10 5 x 10
10 x 2 35 x 20 x 10
And calculate f 6 , f 0
7.4 Loops
7.4.1) The for-end Loops
for k=f:s:t
. . .
. . . A group of MATLAB commands
. . .
end
k = index variable
f = first value
s = step size
t = end value
20
1
Example Compute 2i 12i 2
i 1
s=0;
for i=1:1:20
s=s+1/((2*i+1)*(2*i+2));
end
disp(s)
20
1
Example Compute 2i 12i 2
i 1
s=0;
i=1;
while i<=20
s=s+1/((2*i+1)*(2*i+2));
i=i+1;
end
disp(s)