cece matlab intro1

35
MATLAB CECE ACADEMIC TEAM Prepared by: Sameh Attia

Upload: rashad

Post on 07-Apr-2018

233 views

Category:

Documents


1 download

TRANSCRIPT

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 1/35

MATLABCECE ACADEMIC TEAM

Prepared by: Sameh Attia

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 2/35

Course Contents

Introduction to MatlabMatlab ProgrammingMath (Symbolic)GUIIntro to Simulink

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 3/35

Introduction to Matlab

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 4/35

Contents

What‟s MATLAB?Important CommandsMatlab As a CalculatorM-File

VectorsMatricesFind & operatorsElementary operationsPlotting & Finding MaximumObtaining zero Crossing (roots)Integration & DifferentiationObtaining Peaks

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 5/35

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 6/35

What‟s MATLAB?

UsesAcademic

Solving Equations

Integration & DifferentiationData Exploration ,Analyzing &Visualization

ApplicationsSignals ProcessingCommunicationsImage ProcessingMilitary (Air Defense)

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 7/35

What‟s MATLAB?

Differences from CYou don‟t have to define a variable in MATLAB beforeusing it.

It is easier in Mathematical operations like Matrices &Complex.No need to Compile in Matlab.

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 8/35

What‟s MATLAB?

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 9/35

Important Commands

19/7/2011Matlab CECE Academic Team

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 "MAT-file“.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.

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 10/35

Matlab As a Calculator

19/7/2011Matlab CECE Academic Team

x=9; y=8;z=sin(2*x)+exp(-y)+sqrt(x+y)+log(x*y)+x^2+ye3

Note log(x) means Ln xye3 y*10^3asin(x) sin inverse of xsind(x) x in degrees

Complex z=3+5iabs(z) - angle(z) - imag(z) - real(z) - conj(z)

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 11/35

Matlab As a Calculator

19/7/2011Matlab CECE Academic Team

Useful constantspi π

i,j Imaginary unit

eps Floating-point relative accuracyrealmin Smallest floating-point numberrealmax Largest floating-point number

Inf InfinityNaN Not-a-number

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 12/35

Matlab As a Calculator

19/7/2011Matlab CECE Academic Team

Display FormatsFormat short: four decimals ( the default)

π = 3.141 6

Format long:15 decimalsπ = 3.141592653589793

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 13/35

M-File

19/7/2011Matlab CECE Academic Team

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

Functions

need input & output argumentsVariables are local

ScriptsVariables are global

Can be called by writing its name in the command windowTo open a new M- file, click on „new‟ icon or write„edit‟ in the command window

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 14/35

M-File

19/7/2011Matlab CECE Academic Team

Test Programclc,clearx=3;

y=6;z=x+sin(y)/6

Save it (Don‟t use reserved words), Run it „F5‟

To make comments in M-file, use %

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 15/35

M-File

19/7/2011Matlab CECE Academic Team

The input FunctionPrompt for user input

x = input(„enter the value of x „)

To allow the user to enter a Stringy = input(„enter your name ‟, ‟s‟)

The disp Functiondisp(x) If x is a string, the text is displayed.

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 16/35

M-File

19/7/2011Matlab CECE Academic Team

ExerciseWrite a program to encourage the user to enter hisname & age, then display

user_name is user_age years old

Solutionclc,clearx=input(„your name? „ , „s‟);

y=input(„ your age? „ );

Disp([x „is‟ num2str(x) „years old‟])

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 17/35

Vectors

19/7/2011Matlab CECE Academic Team

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)

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 18/35

Vectors

19/7/2011Matlab CECE Academic Team

Operations on vectorsx=[ 2 4 8 -6 10 ];

x(1) 2x(end) 10x(3:end) [ 8 -6 10 ]x(2:2:end) [ 4 -6 ]

X‟ TransposeSum(x) 2+4+8+-6+10Prod(x) 2*4*8*-6*10Mean(x) averageLength (x) 5Max(x) 10

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 19/35

Matrices

19/7/2011Matlab CECE Academic Team

Operations on MatricesTo write A Matrix

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

a(2,3) 2a(2:end, 2:end) [ 7 2 ; 1 8 ]

a( : , 1) [ 4 ; 9 ; 11 ]a(4) 6

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 20/35

Matrices

19/7/2011Matlab CECE Academic Team

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

a‟ TransposeSize(a) 3x3det(a) determinantinv(a) inverserank(a) 3Sum(a) vector with the sum over each column[m,n]=eig (a) m eigenvectors , n eigenvalues

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 21/35

Matrices

19/7/2011Matlab CECE Academic Team

Special Matriceszeros(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)

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 22/35

Matrices

19/7/2011Matlab CECE Academic Team

Matrix Concatenationa= b=

To make c= c=[ a ; b]

To make c= c=[ a b‟ ]

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 23/35

Find & Operators

19/7/2011Matlab CECE Academic Team

The Find FunctionFind indices of nonzero elements.I=find(EXPR)

Evaluates the logical expression EXPR and returns theindices of those elements of the resulting matrix thatare equal to the logical TRUE state.

x=[ 2 4 8 -6 10 ]i=find(x==8)

then i=3

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 24/35

Find & Operators

19/7/2011Matlab CECE Academic Team

Relational operatorsEqual ==Not equal ~=Greater than >Less than or equal <=Greater than or equal >=

Logical Operatorsand Element-wise logical AND &or Element-wise logical OR |

not Logical NOT ~xor Logical EXCLUSIVE ORany True if any element of vector is nonzeroall True if all elements of vector are nonzero

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 25/35

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 26/35

Finding Maximum

19/7/2011Matlab CECE Academic Team

ExerciseWrite a program to find the maximum of

y = -x^2 + 3x + 2

Plot the Function indicating the maximum point

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 27/35

Finding Maximum

19/7/2011Matlab CECE Academic Team

Solutionx=-6:.1:6;y=-x.^2+3*x+2;

plot(x,y), hold on, gridi=find(y==max(y));plot(x(i),y(i),'ro')

hold off

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 28/35

Obtaining zero Crossing (roots)

19/7/2011Matlab CECE Academic Team

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

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 29/35

Obtaining zero Crossing (roots)

19/7/2011Matlab CECE Academic Team

Solutionx=linspace(0,10,1000);

y=cos(x).*exp(-.3*x);

plot(x,y), hold on ,gridys=[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()

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 30/35

Integration & Differentiation

19/7/2011Matlab CECE Academic Team

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

To ComputeX=0:0.01:10;Y=sin(x);I=trapz(x,y)

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 31/35

Integration & Differentiation

19/7/2011Matlab CECE Academic Team

DifferentiationTo Compute

x=linspace(1,10,200);

y=sin(x).*exp(-.3*x);plot(x,y), grid, hold alldydx=diff(y)./diff(x);

dydx=[dydx dydx(end)];plot(x,dydx)

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 32/35

Obtaining Peaks

19/7/2011Matlab CECE Academic Team

ExerciseWrite a program to obtain the peaks of y=sin x*e^-.3xPlot it indicating the peaks points.

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 33/35

Obtaining Peaks

19/7/2011Matlab CECE Academic Team

Solutionx=0:0.05:10;y=sin(x).*exp(-.3*x);

plot(x,y), grid,hold ondydx=diff(y)./diff(x);dydxs=[dydx(1) dydx(1:end-1)];

dydxm=dydx.*dydxs;iz=find(dydxm<=0);plot(x(iz),y(iz),'ro')

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 34/35

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.

19/7/2011Matlab CECE Academic Team

8/6/2019 Cece Matlab Intro1

http://slidepdf.com/reader/full/cece-matlab-intro1 35/35

THANK YOU!Prepared by: Sameh Attia

E il S h tti 91@ il