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

MATLAB CECE ACADEMIC TEAM

Prepared by: Sameh Attia

Course Contents

Introduction to Matlab Matlab Programming Math (Symbolic) GUI Intro to Simulink

Matlab

CECE Academic Team

19/7/2011

Introduction to Matlab

Matlab

CECE Academic Team

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

CECE Academic Team

19/7/2011

Whats MATLAB?

Uses
Academic
Solving

Equations Integration & Differentiation Data Exploration ,Analyzing &Visualization


Applications
Signals

Processing Communications Image Processing Military (Air Defense)


Matlab CECE Academic Team 19/7/2011

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

CECE Academic Team

19/7/2011

Whats MATLAB?

Matlab

CECE Academic Team

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.

load Load workspace variables from disk.


Matlab CECE Academic Team 19/7/2011

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 CECE Academic Team 19/7/2011

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

CECE Academic Team

19/7/2011

Matlab As a Calculator

Display Formats
Format

short: four decimals (the default) =3.1416 Format long:15 decimals =3.141592653589793

Matlab

CECE Academic Team

19/7/2011

M-File

The best way to write a big program. M-Files can be used to write Scripts & Functions
Functions

need input & output arguments Variables are local

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

Save it (Dont use reserved words), Run it F5

To make comments in M-file, use %


Matlab CECE Academic Team 19/7/2011

M-File

The input Function


Prompt

for user input x = input(enter the value of x ) To allow the user to enter a String y = input(enter your name , s)

The disp Function


disp(x)

If x is a string, the text is displayed.

Matlab

CECE Academic Team

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

A Matrix of one row or one coloumn. To enter a vector


x=[

3 5 2 5 3]; To define an interval


y=start:step:stop; z=linspace(start,

stop,no. of elements)

Matlab

CECE Academic Team

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

write A Matrix a=[ 4 6 -1 ; 9 7 2 ; 11 1 8 ];

a(2,3) 2 a(2:end, 2:end) [ 7 2 ; 1 8 ] a( : , 1) [ 4 ; 9 ; 11 ] a(4) 6

Matlab

CECE Academic Team

19/7/2011

Matrices

Operations on Matrices a=[ 4 6 -1 ; 9 7 2 ; 11 1 8 ];


a Transpose Size(a) 3x3 det(a) determinant inv(a) inverse rank(a) 3 Sum(a) vector with the sum over each column [m,n]=eig (a) m eigenvectors , n eigenvalues
Matlab CECE Academic Team 19/7/2011

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

CECE Academic Team

19/7/2011

Find & Operators

The Find Function


Find

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

Find & Operators

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

Elementry Matrix Operations


To

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

CECE Academic Team

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

CECE Academic Team

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

CECE Academic Team

19/7/2011

Obtaining zero Crossing (roots)

Exercise
Write a program to obtain the zero crossing (roots) of the function y=cos x*e^-.3x ,Plot it indicating the zeros

Matlab

CECE Academic Team

19/7/2011

Obtaining zero Crossing (roots)

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')

Note If the Function is polynomial, use roots()


Matlab CECE Academic Team 19/7/2011

Integration & Differentiation

Integration
Z

= TRAPZ(X,Y) computes the integral of Y with respect to X using the trapezoidal method.

To

Compute X=0:0.01:10; Y=sin(x); I=trapz(x,y)

Matlab

CECE Academic Team

19/7/2011

Integration & Differentiation

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

CECE Academic Team

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

CECE Academic Team

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

CECE Academic Team

19/7/2011

THANK YOU!
Prepared by: Sameh Attia Email: Samehattia91@gmail.com

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