Professional Documents
Culture Documents
Introduction To Matlab - Lec - 3-SOKAR
Introduction To Matlab - Lec - 3-SOKAR
Prepared by
Dr. Mohamed Saber Sokar
Dr. Mohamed Saber Sokar 2
▌Outline:
Operators (Arithmetic, relational, logical )
Using of M-File
Input / Output of data using keyboard
Description of Matlab functions
Matlab Utility functions
PART (II-2)
FUNDAMENTALS
1
▌Operations on Matrices ▌Matrices Operations
Transpose B=A’
eye (n) returns an n x n identity matrix Given A and B:
Identity Matrix eye (m,n) returns an m x n matrix with ones on
the main diagonal and zeros elsewhere
Addition and Subtraction C =A + B , C = A - B
Addition Subtraction Product Transpose
Scalar Multiplication B = α A, where α is a scalar
Matrix Multiplication C=A*B
Matrix Inverse B = inv(A), A must be a square matrix in this case
Matrix powers B = A * A , A must be a square matrix
Determinant det (A), A must be a square matrix
2
▌Operators (Element by Element) ▌The use of “.” (Element Operation)
When two arrays have the same dimensions, A = [1 2 3; 5 1 2; 3 4 -1] x = A(1,:) y = A(3 ,:)
A=
addition, subtraction, multiplication, and division 1 2 3 x= y=
apply on an element-by-element basis. 5 1 2 1 2 3 3 4 -1
3 4 -1
b = x .* y c=x./y d = x .^2
b= c= d=
3 8 -3 0.33 0.5 -3 1 4 9
K= x^2
Erorr: ??? Error using mpower Matrix must be square.
B= x*y
Erorr: ??? Error using mtimes Inner matrix dimensions must
agree.
Dr. Mohamed Saber Sokar 13 Dr. Mohamed Saber Sokar 14
3
▌Input /Output data ▌Input/Output of Data Using Keyboard
4
▌FPRINTF command ▌Exampls
The fprintf function has allowed us to control the Operation Symbol Example Result
format of the output precisely. Decimal integers %d fprintf( ' %5d ', -32) -32
fprintf (’comment or format string [Width Floating point %f fprintf( ' %12.6f ', -1/pi) -0.318310
specifiers W.S]’, list of variables) Scientific notation %e fprintf( ' %14.6e ', -1/pi) -3.183099e-01
Strings %s fprintf( ' %14s', 'Hello world') Hello world
- The W.S (width specifiers) are optional. If they are left out, Percentage %% fprintf( ' Return = %5.2f%%', 6.8) Return = 6.80%
default values are used (i.e. short format) . Newline \n fprintf( ' \nBHP $%5.2f\n', 40.93) BHP $40.93
▌Class activity
M-file >> a = [1.02 3.04 5.06];
>> fprintf('%d\n',round(a));
>> name = input(' Enter your name: ' );
Hello John!
5
▌Class activity ▌Output to files (Write data to text file)
To capture all the results of a MATLAB session
in a file.
fprintf ('hello a =%g score=%% %4.2f ', av, a) 1st method:
Use the command diary
A_balance = 12345; diary my_file_name,
R_rate = 0.09; at the beginning of the session, use whatever
balance = A_balance + A_balance * R_rate; MATLAB commands you require, then use the
fprintf(... command
' Interest rate:%6.3f %% New balance:%8.2f\n',... diary off, at the end of the session.
balance, A_balance )
Everything printed in the command window will also
be recorded in the file specified.
▌Output to files (Write data to text file) ▌Output to files (Write data to text file)
2nd method: Write the output directly to a file 2nd method: Write the output directly to a file
This requires three steps: fprintf(fileID,formatSpec,A1,...,An)
1- Open a file with the appropriate name and permissions. Example: Write a short table of the exponential function to a
fid = fopen(my_file_name, 'w'); x text
= 0:file .1
called exp.txt
: 1;
It opens the file specified in the string my_file_name y = [x; exp(x)];
and discards any existing contents already in that file. fid = fopen('ttt.txt','w');
2- Use the fprintf specifying the file to write to. fprintf(fid,'%6s %12s\n','x‘,'exp(x)');
fprintf(fileID, formatSpecifications, variable_list);
fprintf(fid,'%6.2f %12.8f\n',y);
as required to write information to the file with identifier
fclose(fid); % Close the file
fileID.
3- At the end of your program, close the file using The first call to fprintf prints header text x and exp(x), and
fclose(fileID); the second call prints the values from variable A.
Dr. Mohamed Saber Sokar 33 Dr. Mohamed Saber Sokar 34
The first call to fprintf prints header text x and exp(x), and
the second call prints the values from variable A.
Dr. Mohamed Saber Sokar 35 Dr. Mohamed Saber Sokar 36
6
▌Matlab mathematical Functions -01 ▌Matlab mathematical Functions-02
Logarithmic functions: Functions for complex numbers:
log(x) (base e “ ln ”),, z=a+j b
log10(x) (base 10) or
exp(x) (ex), z r
O
Trigonometric functions:
sin(x), cos(x), tan(x),
asin(x), acos(x), atan(x)
Hyperbolic functions:
sinh(x), cosh(x), tanh(x),
asinh(x), acosh(x), atanh(x)
7
▌Date and Time functions ▌Date and Time functions
datevec(x) converts an internal date x into a date tic sets a “stopwatch” going.
vector toc outputs the value of the stopwatch (in seconds).
>> datevec(d)
ans = >> tic
5.0000 7.0000 4.0000 0 0 0 .....
0 1.0000 11.0000 0 0 0 .....
0 1.0000 1.0000 0 0 0 .....
0 1.0000 7.0000 0 0 0 >> toc
0 1.0000 19.0000 0 0 0 Elapsed time is 200.344312 seconds.
0 1.0000 16.0000 19.0000 3.0000 21.6000
Dr. Mohamed Saber Sokar 43 Dr. Mohamed Saber Sokar 44
▌Utility functions
They are used to interact with the file system
/workspace of the computer we are operating on.
Many thank for Your
attention