Beruflich Dokumente
Kultur Dokumente
Outline:
What is Matlab?
• Matlab Screen
• Variables, array, matrix, indexing
• Operators (Arithmetic, relational, logical )
• Display Facilities
• Flow Control
• Using of M-File
• Writing User Defined Functions
• Conclusion
What is Matlab?
• MATLAB is basically a high level language which
has many specialized toolboxes for making
things easier for us
• How high?
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
MATLAB stands for MATrix LABoratory. Cleve
Moler is a mathematician and computer
programmer. In the mid to late 1970s, he
invented MATLAB, a numerical computing
package, to give his students at the University of
New Mexico easy access to Fortran libraries for
numerical computing without writing Fortran.
What are we interested in?
• Matlab is too broad for our purposes in this
course.
• The features we are going to require is
Matlab
Series of
Matlab
commands
Command
m-files mat-files
Line
• Current Directory
– View folders and m-files
• Workspace
– View program variables
– Double click on a variable
to see it in the Array Editor
• Command History
– view past commands
– save a whole session
using diary
Main Matlab Window
Command Window
type commands
Workspace
Command History
Variables
• No need for types. i.e.,
int a;
double b;
float c;
x1 =
1 2 5 1
• a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x =
1 2 3
5 1 4
3 2 -1
• transpose y = x1’ y = 1
2
5
1
%Matrix computations and operations
>> %creating vectors and matrices
>> %create a row vector, we can form a row vector.
>> a
a=
1 3 6 8
>> %create a column vector,
>> b=[1;4;6;10];
>> b
b=
1
4
6
10
Create the matrix x of 3 rows and 4 columns
x=[12 23 21 3; 2 34 5 7; 31 32 33 34];
>> x
x=
12 23 21 3
2 34 5 7
31 32 33 34
%elements of a Matrix
>> z=x(2,3)
x=
z=
5 12 23 21 3
>> x1=x(1,:)
2 34 5 7
x1 = 31 32 33 34
12 23 21 3
>> x2=x(:,2)
x2 =
23 x=
34
32 12 23 21 3
2 34 5 7
>> s=size(x) 31 32 33 34
s=
3 4
Long Array, Matrix
• t =1:1:10
t = 1 2 3 4 5 6 7 8 9 10
• t =1:10
t = 1 2 3 4 5 6 7 8 9 10
• k =2:-0.5:-1
• B = [1:4; 5:8]
B =
1 2 3 4
5 6 7 8
Remove the n th
Row or column of the given matrix
X(n, : ) = [ ]
x(2, : ) = [ ]
X( : ,n ) = [ ]
x( : , 2 ) = [ ] x=
12 23 21 3
2 34 5 7
31 32 33 34
Add a column of 4,s to the end of matrix.
x( : , 4) = 4 x=
12 23 21 4
2 34 5 4
31 32 33 4
Remove the 2 nd Row
x= Remove the 2 nd Column
12 23 21 3 x( : , 2 ) = [ ]
2 34 5 7
31 32 33 34
x=
12 21 3
x(2, : ) = [ ] 2 5 7
x= 31 33 34
12 23 21 3
31 32 33 34
Add 4 th Column
Add 2 nd Row
x= x =12 23 21 23
12 23 21 3 2 34 5 34
12 23 21 3 31 32 33 32
31 32 33 34
We can extend the colon notation to specify a
sequence
Create a vector v which starts at 1, with increment of 2
and stops at 10:
v=1:2:10
v=
1 3 5 7 9
>> v=1:10
v=
1 2 3 4 5 6 7 8 9 10
Generating Vectors from functions
• zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x = 0 0 0
x = ones(1,3)
• ones(M,N) MxN matrix of ones x = 1 1 1
x = rand(1,3)
• rand(M,N) MxN matrix of x =
uniformly 0.9501 0.2311 0.6068
distributed random
numbers on (0,1)
Matrix Index
• The matrix indices begin from 1 (not 0 (as in C))
• The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices
• x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1 2 4 5
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Right Division
\ Left Division
^ Power/ Exponentiation
‘ Complex conjugate transpose
Examples:
>> a=7;
>> b=8;
>> a+b
ans =
15
>> (a+b)/2
ans =
7.5000
>> 5^3/2
ans =
62.5000
>> 27^(1/3)^0.2
ans =
1.2457
>> 27^(1/3)+32^0.2
ans =
5
ARITHMETIC OPRATIONS :
LHS=2^5/(2^5-1)
LHS =
1.0323
>> RHS=(1-1/2^5)^-1
RHS =
1.0323
2. Solution
>>3*((5^(0.5)-1)/(5^(0.5)+1)^2)-1
ans =
-0.6459
3. Solution
area = pi*(pi^(1/3) - 1)^2
𝐓𝐑𝐈𝐆𝐎𝐍𝐎𝐌𝐄𝐓𝐑𝐘
1(a). Solution
>> sin(pi/6)
ans =
0.5000
1(b). Solution
>> cos(pi)
ans =
-1
1(c). Solution
tan(pi/2)
2. Solution
>> (sin(pi/6))^2+(cos(pi/6))^2
ans =
1
3. Solution
>> x=32*pi;
>> y=(cosh(x))^2-(sinh(x))^2
y=
0
EXPONENTIAL AND LOGARITHMS
6. Solution 7. Solution
>> log10(exp(3))
>> exp(3)
ans =
ans =
1.3029
20.0855
>> x=log(17)/log(3)
>> log(exp(3))
x=
ans =
2.5789
3
𝐂𝐎𝐌𝐏𝐋𝐄𝐗 𝐍𝐔𝐌𝐁𝐄𝐑𝐒
>> %complex numbers:
>> (1+3i)/(1-3i)
ans =
-0.8000 + 0.6000i
>> exp(i*pi/4)
ans =
0.7071 + 0.7071i
Euler’s formula e ix = cosx + i sinx (x = pi/2)
ans =
0.0000 + 1.0000i
ans =
0.0000 - 1.0000i
exp(i*pi/2)
ans =
0.0000 + 1.0000i
Matrix
>> x=[1 2 3 4]
x=
1 2 3 4
>> y=x.'
y=
1
2
3
4
>> A=[1,2,3; 4,-5,6; 5,-6,7]
A=
1 2 3
4 -5 6
5 -6 7
>> b=A'
b=
1 4 5
2 -5 -6
3 6 7
>> inv(A)
ans =
>> A'
ans =
1 4 5
2 -5 -6
3 6 7
Notes:
• “%” is the neglect sign for Matlab (equaivalent
of “//” in C). Anything after it on the same line
is neglected by Matlab compiler.
• Sometimes slowing down the execution is
done deliberately for observation purposes.
You can use the command “pause” for this
purpose
pause %wait until any key
pause(3) %wait 3 seconds
Useful Commands
>>lookfor keyword
Example 1
Change the format to long by typing
format long
View the result for the value of pi by typing
pi
ans = 3.14159265358979
>>format short pi
ans = 3.1416
>>single(pi)
ans = 3.1416
Now set format to long, and pi displays a 15-digit
value while single(pi) display an 8-digit value:
>>format long pi
ans = 3.14159265358979
>>single(pi)
ans = 3.1415927
Example 4
A = 5.123456789;
for k=1:10
A = A * 10;
disp(A)
end
The values displayed for A are shown here. The power
of 10 is always a multiple of 3. The value itself is
expressed in 5 or more digits for the short eng format,
and in exactly 15 digits for long eng:
.*element-by-element multiplication
./ element-by-element division
.^element-by-element power
Matrices Operations
Given A and B:
b = x .* y c=x./y d = x .^2
x = A(1,:) y = A(3 ,:)
b= c= d=
x= y= 3 4 -3 0.33 1 -3 1 4 9
1 2 3 3 2 -1
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Vectors and Matrices
1 2 3 1 1 1 14 14 14
4 5 6 2 2 2 = 32 32 32
A*B
7 8 9 3 3 3 50 50 50
A/B ? (matrices
singular)
1 / 1 2 / 1 3 / 1 1.0000 2.0000 3.0000
A./B 4 / 2 5 / 2 6 / 2 = 2.0000 2.5000 3.0000
7 / 3 8 / 3 9 / 3 2.3333 2.6667 3.0000
Vectors and Matrices
11 21 31 1 2 3
A.^B 2 2 16 36
4 5 62 =
25
73 83 93 343 512 729
Points on a circle:
All points coordinates x =rcosθ and y rsinθ, where r is a
constant, lies on a circle with radius r, i.e they satisfy the
equation r2 = x2 + y2. Create a column vector for θ with
the values 0, pi/4, pi/2, 3*pi/4, pi, 5*pi/4. Take r =2 and
compute the column vectors x and y and check that x
and y indeed satisfy the equation of a circle, by
computing the radius r =
Solution: Points on a circle:
theta= [0; pi/4; pi/2; 3*pi/4; pi; 5*pi/4]
theta =
0
0.7854
1.5708
2.3562
3.1416
3.9270
Script:
r=2;
x=r*cos(theta);
y=r*sin(theta);
x.^2+y.^2
r=sqrt(x.^2+y.^2)
Output: r = 2
Basic Task: Plot the function sin(x) between
0≤x≤4π
• Create an x-array of 100 samples between 0 and
4π.
>>x=linspace(0,4*pi,100);
>>y=sin(x);
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
>>plot(y) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
Plot the function e-x/3sin(x) between 0≤x≤4π
• Create an x-array of 100 samples between 0
and 4π.
>>x=linspace(0,4*pi,100);
>>plot(y2) 0.6
0.5
0.4
0.3
0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
0.7
0.6
• plot(.)
0.5
0.4
0.3
Example:
0.2
0.1
>>x=linspace(0,4*pi,100); 0
>>y=sin(x); -0.1
>>plot(y) -0.2
>>plot(x,y) -0.3
0 10 20 30 40 50 60 70 80 90 100
0.7
• stem(.) 0.6
0.5
0.4
0.3
Example:
0.2
0.1
>>stem(y) 0
>>stem(x,y) -0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
• title(.)
>>title(‘This is the sinus function’)
This is the sinus function
1
0.8
• xlabel(.) 0.6
0.4
sin(x)
0
-0.2
• ylabel(.) -0.4
-0.6
-0.8
>>ylabel(‘sin(x)’) -1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
Built in functions (commands)
>>> hold
Current plot held
>>> plot(x,y2,'g--')
>>>
Built in functions (commands) eg1_plt.m
0.6
0.4
0.2
y1 and y2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
angular frequency (rad/s)
Built in functions (commands) eg2_srf.m
Script Function
Collections of commands User defined commands
• Extension “.m”
• A text file containing script or function or program to run
Use of M-File
Save file as Denem430.m
Same Name
Writing User Defined Functions
• Another function which takes an input array
and returns the sum and product of its
elements as outputs
Writing User Defined Functions
•The function sumprod(.) can be called from
command window or an m-file as
Assignments
squre_root: 1.000000
squre_root: 1.414214
squre_root: 1.732051
squre_root: 2.000000
Script:
squre_root: 1.000000
squre_root: 1.414214
squre_root: 1.732051
squre_root: 2.000000