Matlab Cece Academic Team: Prepared By: Sameh Attia
Matlab Cece Academic Team: Prepared By: Sameh Attia
Course Contents
Matlab
19/7/2011
Introduction to Matlab
Matlab
19/7/2011
Contents
Whats MATLAB? Important Commands Matlab As a Calculator M-File Vectors Matrices Find & operators Elementary operations Plotting & Finding Maximum Obtaining zero Crossing (roots) Integration & Differentiation Obtaining Peaks
CECE Academic Team 19/7/2011
Matlab
Whats MATLAB?
MATLAB is a high-performance language for technical computing. The name MATLAB stands for matrix laboratory. Cleve Moler -the cofounder of MathWorks- wrote the first version of Matlab in the 1970s.
Matlab
19/7/2011
Whats MATLAB?
Uses
Academic
Solving
Whats MATLAB?
Differences from C
You dont have to define a variable in MATLAB before using it. It is easier in Mathematical operations like Matrices & Complex. No need to Compile in Matlab.
Matlab
19/7/2011
Whats MATLAB?
Matlab
19/7/2011
Important Commands
clc Clear command window. clear Clear variables and functions from memory. help Display help text in Command Window. doc Display HTML documentation in the Help browser. who,whos List current variables. Save
save FILENAME saves all workspace variables to the binary "MATfile. save FILENAME X Y Z saves X, Y, and Z. Save FILENAME.dat ascii save workspace in readable format.
Matlab As a Calculator
x=9; y=8; z=sin(2*x)+exp(-y)+sqrt(x+y)+log(x*y)+x^2+ye3 Note log(x) means Ln x ye3 y*10^3 asin(x) sin inverse of x sind(x) x in degrees Complex z=3+5i abs(z) - angle(z) - imag(z) - real(z) - conj(z)
Matlab As a Calculator
Useful constants
pi i,j Imaginary unit eps Floating-point relative accuracy realmin Smallest floating-point number realmax Largest floating-point number Inf Infinity NaN Not-a-number
Matlab
19/7/2011
Matlab As a Calculator
Display Formats
Format
short: four decimals (the default) =3.1416 Format long:15 decimals =3.141592653589793
Matlab
19/7/2011
M-File
The best way to write a big program. M-Files can be used to write Scripts & Functions
Functions
Scripts
Variables
are global Can be called by writing its name in the command window
To open a new M-file, click on new icon or write edit in the command window
Matlab CECE Academic Team 19/7/2011
M-File
Test Program
clc,clear x=3; y=6; z=x+sin(y)/6
M-File
for user input x = input(enter the value of x ) To allow the user to enter a String y = input(enter your name , s)
Matlab
19/7/2011
M-File
Exercise Write a program to encourage the user to enter his name & age, then display user_name is user_age years old Solution
clc,clear x=input(your name? , s); y=input( your age? ); Disp([x is num2str(x) years old])
Matlab CECE Academic Team 19/7/2011
Vectors
stop,no. of elements)
Matlab
19/7/2011
Vectors
Operations on vectors
x=[ 2 4 8 -6 10 ];
x(1) 2 x(end) 10 x(3:end) [ 8 -6 10 ] x(2:2:end) [ 4 -6 ] X Transpose Sum(x) 2+4+8+-6+10 Prod(x) 2*4*8*-6*10 Mean(x) average Length (x) 5 Max(x) 10
Matlab CECE Academic Team 19/7/2011
Matrices
Operations on Matrices
To
Matlab
19/7/2011
Matrices
Matrices
Special Matrices
zeros(3,4) I=eye(4) p=ones(2,3) d=diag([-3 4 2]) r=rand(4) r=randint(4,4) r=randint(4,4,[2,10]) m=magic(3)
Matlab CECE Academic Team 19/7/2011
Matrices
Matrix Concatenation
a= b=
To make c=
c=[ a ; b]
To make c=
c=[ a b ]
Matlab
19/7/2011
indices of nonzero elements. I=find(EXPR) Evaluates the logical expression EXPR and returns the indices of those elements of the resulting matrix that are equal to the logical TRUE state. x=[ 2 4 8 -6 10 ] i=find(x==8) then i=3
Matlab CECE Academic Team 19/7/2011
Relational operators
Equal Not equal Greater than Less than or equal Greater than or equal == ~= > <= >=
Logical Operators
and or not xor any all
Matlab
Element-wise logical AND & Element-wise logical OR | Logical NOT ~ Logical EXCLUSIVE OR True if any element of vector is nonzero True if all elements of vector are nonzero
CECE Academic Team 19/7/2011
Elementary operations
make an operation between an element in a matrix and the corresponding element in the other matrix, use dot operator
Multiply the matrix by itself X.*X Square each element in the Matrix
X*X
Matlab
19/7/2011
Finding Maximum
Exercise
Write a program to find the maximum of y = -x^2 + 3x + 2 Plot the Function indicating the maximum point
Matlab
19/7/2011
Finding Maximum
Solution
x=-6:.1:6; y=-x.^2+3*x+2; plot(x,y), hold on, grid i=find(y==max(y)); plot(x(i),y(i),'ro') hold off
Matlab
19/7/2011
Exercise
Write a program to obtain the zero crossing (roots) of the function y=cos x*e^-.3x ,Plot it indicating the zeros
Matlab
19/7/2011
Solution
x=linspace(0,10,1000); y=cos(x).*exp(-.3*x); plot(x,y), hold on ,grid ys=[y(1) y(1:end-1)]; ym=ys.*y; iz=find(ym<=0); plot(x(iz),y(iz),'ro')
Integration
Z
= TRAPZ(X,Y) computes the integral of Y with respect to X using the trapezoidal method.
To
Matlab
19/7/2011
Differentiation
To
Compute
x=linspace(1,10,200); y=sin(x).*exp(-.3*x); plot(x,y), grid, hold all dydx=diff(y)./diff(x); dydx=[dydx dydx(end)]; plot(x,dydx)
Matlab
19/7/2011
Obtaining Peaks
Exercise
Write a program to obtain the peaks of y=sin x*e^-.3x Plot it indicating the peaks points.
Matlab
19/7/2011
Obtaining Peaks
Solution
x=0:0.05:10; y=sin(x).*exp(-.3*x); plot(x,y), grid,hold on dydx=diff(y)./diff(x); dydxs=[dydx(1) dydx(1:end-1)]; dydxm=dydx.*dydxs; iz=find(dydxm<=0); plot(x(iz),y(iz),'ro')
Matlab CECE Academic Team 19/7/2011
References
[1] Using Matlab version 6 , Mathworks [2] Introduction to Matlab 7 for engineers, William J [2] David F. Griffiths , An Introduction to Matlab. [3] Matlab Help.
Matlab
19/7/2011
THANK YOU!
Prepared by: Sameh Attia Email: [email protected]