Top Banner
An introduction to MATLAB By S. Ziaei Rad
17

An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Jun 19, 2020

Download

Documents

dariahiddleston
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: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

An introduction to MATLAB

ByS. Ziaei Rad

Page 2: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

About MATLABMATLAB is an interactive softwareMATLAB is easy to learn and easy to develop a program inside it.Numerical calculation in MATLAB uses collections of well written scientific/mathematical subroutines such as LINPACK and EISPACK.In general, MATLAB is a useful tool for vector and matrix manipulations.The FEM method is a well defined candidate for which MATLAB can be very useful as a solution tool.

Page 3: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Vector and Matrix Manipulations

Once we get into Matlab, we meet a prompt >> called Matlabprompt.

Define a matrix/vector Transpose of a matrix

>> A‘

ans =

1 2 03 7 36 8 9

>> A=[1 3 6; 2 7 8; 0 3 9]

A =

1 3 6

2 7 8

0 3 9

>> size(A)

ans =

3 3

Page 4: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Vector and Matrix Manipulations

Column or row components>> A(:,3)ans =

689

>> A(1,:)

ans =

1 3 6We can try

>> A(1,:)+A(3,:)

ans =

1 6 15

Matrix AdditionLet’s define another matrix>> B=[3 4 5; 6 7 2; 8 1 10];>> BB =

3 4 56 7 28 1 10

>> C=A+B

C =4 7 118 14 108 4 19

Matrix Subtraction>> C=A-B

C =-2 -1 1-4 0 6-8 2 -1

Page 5: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Vector and Matrix Manipulations

Matrix Multiplication>> C=A*B

C =69 31 71

112 65 10490 30 96

>> C=A*B‘;>> C=A’*B;Matrix Function>> inv(A)

ans =1.8571 -0.4286 -0.8571

-0.8571 0.4286 0.19050.2857 -0.1429 0.0476

>> A*inv(A)

ans =1.0000 0.0000 00.0000 1.0000 0

0 0 1.0000

Basic matrix Functions

Symbol Explanation----------------------------------inv Inverse of a matrixdet Determinate of a matrixrank Rank of a matrixcond Condition number of a matrixeye(n) The n by n identity matrixtrace Summation of diagonal

elements of a matrixzeros(n,m) The n by m matrix

consistings of all zerosones(n,m) The n by m matrix

consistings of all onesExamples:inv(A);det(A);rank(A);eye(5);ones(6,7);

Page 6: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Basic Matrix Function

Matrix of random number

>> A=rand(3,3)

A =

0.3529 0.1389 0.60380.8132 0.2028 0.27220.0099 0.1987 0.1988

Matrix exponential

>> expm(A)

ans =

1.5267 0.2680 0.84721.1116 1.3533 0.66510.1191 0.2521 1.2783

EigenvalusThe eigenvalus problem of a matrix is defined

>> A=[5 3 2; 1 4 6; 9 7 2]

A =5 3 21 4 69 7 2

>> e=eig(A)

e =

12.53611.7486

-3.2847

λφφ =A

Page 7: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Basic Matrix Function

Eigenvalues and Eignvectors

>> [V,D]=eig(A)

V =

-0.4127 -0.5992 0.0459-0.5557 0.7773 -0.6388-0.7217 -0.1918 0.7680

D =

12.5361 0 00 1.7486 00 0 -3.2847

LU decomposition

>> A=[1 3 5; 2 4 8; 4 7 3];>> [L,U]=lu(A)

L =0.2500 1.0000 00.5000 0.4000 1.00001.0000 0 0

U =4.0000 7.0000 3.0000

0 1.2500 4.25000 0 4.8000

>> L*Uans =

1 3 52 4 84 7 3

Page 8: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Basic Matrix Function

QR Decomposition

Q is a matrix with orthonormalColumn and R is the upper triangularMatrix.>>A=[1 3 5; 2 4 8; 4 7 3];>> [Q,R]=qr(A)

Q =-0.2182 0.9117 0.3482-0.4364 0.2279 -0.8704-0.8729 -0.3419 0.3482

R =-4.5826 -8.5105 -7.2012

0 1.2536 5.35610 0 -4.1779

Singular Value Decomposition (SVD)

>> D=[1 3 7; 2 9 5; 2 8 5];>> [U,sigma,V]=svd(D)

U =-0.4295 0.8998 -0.0775-0.6629 -0.3723 -0.6495-0.6133 -0.2276 0.7564

sigma =15.6492 0 0

0 4.1333 00 0 0.1391

V =-0.1905 -0.0726 0.9790-0.7771 -0.5982 -0.1956-0.5999 0.7980 -0.0576

'VUA Σ= QRA =

Page 9: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Basic Matrix Function

Solution of linear equations

Ax=y

>> A=[1 3 5; 2 4 8; 4 7 3];>> A=[1 3 4; 5 7 8; 2 3 5];>> y=[10; 9; 8];>> x=inv(A)*y

x =

-4.25001.75002.2500

OrAx=y

>> x=A\y

x =

-4.25001.75002.2500

Page 10: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Data Analysis FunctionsSymbol Explanations-------------------------------------------------------------

min (max) minimum (maximum) of a vectorMIN(X) is a row vector containing the minimum element from each[Y,I] = MIN(X) returns the indices of the minimum values in vector I.

sum sum of elements of a vectorstd standard deviation of a data collectionsort sort of element of a vector

[Y,I] = SORT(X) also returns an index matrix I. If X is a vector, then Y = X(I).mean mean value of a vector

Vector componentwise operation>> v1=[1 5 6 7]; v2=[0 2 3 5];>> v3=v1.*v2

v3 =0 10 18 35

>> v4=v2./v1

v4 =0 0.4000 0.5000 0.7143

Page 11: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Polynomial FunctionsSymbol Explanations-------------------------------------------------------------poly convert collection of roots into a polynomial equationroots finds the roots of a polynomial equationpolyval evaluate a polynomial for a given valueconv multiply two polynomialdeconv decompose a polynomial into a dividend and a residualpolyfit curve fitting of a given polynomial

Example:If C has N+1 components, the polynomial is C(1)*X^N + ... + C(N)*X + C(N+1).>> C=[1 15 136 498 968 592];>> roots(C)ans =

-5.0000 + 7.0000i-5.0000 - 7.0000i-2.0000 + 2.0000i-2.0000 - 2.0000i-1.0000

Page 12: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Making Complex NumberIn order to make a complex number For example 2+3*I>> 2+3*i

ans =2.0000 + 3.0000i

Or>> 2+3*jans =

2.0000 + 3.0000iNote: Matlab takes i and j as a pure complex number>> i=sqrt(-1)

i = 0 + 1.0000i>> k=sqrt(-1)

k =0 + 1.0000i

Commands for complex numberabs the magnitude of a numberangle the phase anglereal the real part of a complex numberimag the imaginary partconj the complex conjugate

>> c=-1+i

c =-1.0000 + 1.0000i

>> [ abs(c) angle(c) real(c) imag(c)]

ans =1.4142 2.3562 -1.0000 1.0000

>> conj(c)

ans =-1.0000 - 1.0000i

Page 13: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Some Numerical Techniquesfminbnd find minimum of a function of one variablefzero solves a nonlinear algebric equation of one variable

>> fminbnd('x*cos(x)',-2,2)

ans =-0.8603

>> x=fzero('tan(x)',2)

x =1.5708

ode23 solution using the 2nd/3rd order Runge-Kutta algorithmode45 solution using the 4th/5th order Runge-Kutta algorithm

[t,y]=ode45(‘func’,[t0 tf],[x0,v0]);>> [t,y]=ode45('vdp1',[0 20],[2 0]); >> [size(t) size(y)]ans =

237 1 237 2

Page 14: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Plotting ToolsAssume that t , y are the solution of ODE of previous slide>> plot(t,y) >> xlabel('Time (s)')>> plot(t,y(:,1)) >> ylabel('Displacement and velocity')>> plot(t,y(:,1),t,y(:,2)) >> legend('Displacement','Velocity')

Page 15: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Loop and logical statement Symbol Explanations-------------------------------------------------------------for loop command similar to other languagewhile used for a loop combined with conditional statementif produce a conditional statementelseif, else used in conjugate with if commandbreak breaks a loop when a condition is satisfied

== two conditions are equal~= two conditions are not equal<=(>=) one is less (greater) than the other

& and operator| or operator~ not operatorA(i,j) element i and j of matrix AV(i) element I of vector V A(:,i) all elements of matrix A in column i A(i,:) all elements of matrix A in row i

Page 16: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Writing Function SubroutineFunction(ov1,ov2,…]=func1(iv1,iv2,…)iv1, iv2, … are input variablesov1, ov2, … are output variablesthe file then will save as func1.m and can be called inside Matlab by >> [ov1,ov2, …]=func1(iv1,iv2,…)

Example: ax^2+b*x+c=0Function[r1,r2]=secroot(a,b,c)Det=b^2-4*a*cif (Det<0)R1=(-b+j*sqrt(-Det))/2/a; R2=(-b-j*sqrt(-Det))/2/a;elseif(Det==0)R1=-b/2/a; R2=-b/2/a;else(Det>0)R1=(-b+sqrt(Det))/2/a; R2=(-b-sqrt(Det))/2/a;end

Page 17: An introduction to MATLABAn introduction to MATLAB By S. Ziaei Rad About MATLAB MATLAB is an interactive software MATLAB is easy to learn and easy to develop a program inside it. Numerical

Writing Function Subroutine

Then save the function as secroot.mIn order to call it inside Matlab:

>> [r1,r2]=secroot(3,4,5)r1=

-0.6667 + 1.1055ir2=

-0.6667 – 1.1055i

>> a=3; b=0; c=-5;>> [p1,p2]=secroot(a,b,c)

p1=1.2910

p2=-1.2910