Top Banner
MATLAB CECE ACADEMIC TEAM Prepared by: Sameh Attia
35

Cece Matlab Intro1

Apr 07, 2018

Download

Documents

Rashad Rashad
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cece Matlab Intro1

8/6/2019 Cece Matlab Intro1

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

MATLABCECE ACADEMIC TEAM

Prepared by: Sameh Attia

Page 2: Cece Matlab Intro1

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

Page 3: Cece Matlab Intro1

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

Page 4: Cece Matlab Intro1

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

Page 5: Cece Matlab Intro1

8/6/2019 Cece Matlab Intro1

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

Page 6: Cece Matlab Intro1

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

Page 7: Cece Matlab Intro1

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

Page 8: Cece Matlab Intro1

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

Page 9: Cece Matlab Intro1

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.

Page 10: Cece Matlab Intro1

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)

Page 11: Cece Matlab Intro1

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

Page 12: Cece Matlab Intro1

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

Page 13: Cece Matlab Intro1

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

Page 14: Cece Matlab Intro1

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 %

Page 15: Cece Matlab Intro1

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.

Page 16: Cece Matlab Intro1

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‟])

Page 17: Cece Matlab Intro1

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)

Page 18: Cece Matlab Intro1

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

Page 19: Cece Matlab Intro1

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

Page 20: Cece Matlab Intro1

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

Page 21: Cece Matlab Intro1

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)

Page 22: Cece Matlab Intro1

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‟ ]

Page 23: Cece Matlab Intro1

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

Page 24: Cece Matlab Intro1

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

Page 25: Cece Matlab Intro1

8/6/2019 Cece Matlab Intro1

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

Page 26: Cece Matlab Intro1

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

Page 27: Cece Matlab Intro1

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

Page 28: Cece Matlab Intro1

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

Page 29: Cece Matlab Intro1

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

Page 30: Cece Matlab Intro1

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)

Page 31: Cece Matlab Intro1

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)

Page 32: Cece Matlab Intro1

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.

Page 33: Cece Matlab Intro1

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

Page 34: Cece Matlab Intro1

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

Page 35: Cece Matlab Intro1

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