25
3/30/2015 1 COMPUTER LAB CPEG415 Dr. Munaf Salim Dr. Munaf Salim 1 Unit One Unit One Unit One Unit One Basics of MatLab Dr. Munaf Salim 2

Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

1

COMPUTER LAB

CPEG415Dr. Munaf Salim

Dr. Munaf Salim 1

Unit OneUnit OneUnit OneUnit OneBasics of MatLab

Dr. Munaf Salim 2

Page 2: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

2

Use Matlab as a calculator

Dr. Munaf Salim 3

>> 1.2 + 2.3

>> e

>> pi

>> (3+i) + (2+i*4)

• But most importantly it's great with matrices

• Row vector

>> r = [1 2 3 4]

• Column vector

>> c = [1+i; 2+3*i; 3+2*i; 4+5*i]

• >> a = r*c

• >> m = c*r

Use Matlab as a calculator

Dr. Munaf Salim 4

• Multiply a matrix by a scalar:

>> m = 10*m

>> m = a*m

>> m = c*r;

>> m

>> m2 = [1 2; 3 4]

>> c'

>> c.'

>> v = [1:1:100];

>> v = [2:2:100];

>> v = [4:5:39]

>> v = [4:5:40]

Page 3: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

3

Use Matlab as a calculator

Dr. Munaf Salim 5

• Concatenating matrices:

>> c = [2; 4]

>> d = [1; 3]

>> z = [c d]

OR

>> z = [c; d]

• Notice the size of the matrices matters

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

• Try

>> z =[c e]

Use Matlab as a calculator

Dr. Munaf Salim 6

• So start with a matrix >> A = [1 3 5 7; 2 4 6 8; 11 13 15 17];

• Get the element from row 2, column 3 >> A(2,3)

• Get the entire 2nd row >> A(2,:)

• Get the entire 3rd column >> A(:,3)

• How about this: >> A(1:3, 2:3)

• Or you can assign number to those locations:

>> A(2,3) = 100

>> A(2,:) = [100 100 100 100]

>> A(:,3) = [100; 100; 100]

>> A(1:3, 2:3) = [200 200; 200 200; 200 200]

Page 4: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

4

Use Matlab as a calculator

Dr. Munaf Salim 7

>> ones(1,4)

>> A(2,:) = 400.*ones(1,4)

>> A(:,3) = 400.*ones(3,1)

Find the size of a matrix >> size(A)

• Produce a matrix of zeros with the same dimensions >> zeros(size(A))

• The identity matrix is useful >> eye(3)

>> A = rand(3,3)

• Adding the elements of a matrix >> sum(A)

>> sum(sum(A))

• Finding the maximum / minimum elements

>> max(A)

>> max(max(A))

Use Matlab as a calculator

Dr. Munaf Salim 8

• [x, k] = min(A)

• Square every element of A >> A.^2

• Cube every element of A >> A.^3

• Find the square root of every element of A >> sqrt(A)

• Define an array Y of 100 values between -5 and +6, inclusive.

>> Y = linspace(-5,6,100)

• Define an array F of 100 values evenly spaced on a logarithmic scale from 1E-5 to 1E6.

>> Y = logspace(-5,6,100)

Page 5: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

5

Use Matlab as a calculator

Dr. Munaf Salim 9

• Just a preview of how useful Matlab can be:

• >> x = 0:0.01:pi;

• >> y = sin(x)

• >> z = cos(x)

• >> plot(x, y)

• >> hold on

• >> plot(x, z, ′r′)

• If you want to delete the figure, try

• >> clf

Use Matlab as a calculator

Dr. Munaf Salim 10

• Some more useful commands

• >> who

• >> doc

• >> help

• >> lookfor >> lookfor inverse

Page 6: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

6

Use Matlab as a calculator

Dr. Munaf Salim 11

• Many functions are available in Matlab:

• exp, sqrt, log, log10 , abs, angle, conj ,imag, real , ceil, floor, fix, round, sign, cos, sin, csc, sec, cot, tan, acos, asin, acsc, asec, acot, atan, atan2(y,x) , cosh, sinh, csch, sech, coth, tanh , acosh, asinh, acsch, asech, acoth, atanh,

• You can use the functions as follows:

• >> abs(-2)

• >> abs(1+i)

• >> abs(1+i)

• >> A = [1 2 3]

• >> B = [1 1 1]

• >> dot(A,B)

• >> cross(A,B)

Matlab Program Files

Dr. Munaf Salim 12

• Script Files : Script files are just lists of commands. When you run the script file, they are executed as if they were typed into the computer. The script file can use any variable defined in your workspace (in Matlab) and all variables that it defines are now available from your workspace.

• Functions: Functions are script files with data encapsulation, that is, there is a list of inputs to the function file and a list of output. Any variable that you define in the function that is not in the list of outputs is not available in your workspace.

Page 7: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

7

Matlab Program Files

Dr. Munaf Salim 13

• Script Files : Script files are just lists of commands. When you run the script file, they are executed as if they were typed into the computer. The script file can use any variable defined in your workspace (in Matlab) and all variables that it defines are now available from your workspace.

• Functions: Functions are script files with data encapsulation, that is, there is a list of inputs to the function file and a list of output. Any variable that you define in the function that is not in the list of outputs is not available in your workspace.

function y=myfunction(x)

y = x.^2;

• >> y = myfunction(3)

• y = myfunction([1 2 3])

NODAL ANALYSIS

Dr. Munaf Salim 14

Page 8: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

8

NODAL ANALYSIS

Dr. Munaf Salim 15

NODAL ANALYSIS

Dr. Munaf Salim 16

diary ex4_1.dat

% program computes the nodal voltages

% given the admittance matrix Y and current vector I % Y is the admittance matrix and I is the current vector % initialize matrix y and vector I using YV=I form

Y = [ 0.15 -0.1 -0.05; -0.1 0.145 -0.025; -0.05 -0.025 0.075];

I = [5; 0; 2];

% solve for the voltage

fprintf('Nodal voltages V1, V2 and V3 are \n')

v = inv(Y)*I

Page 9: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

9

NODAL ANALYSIS

Dr. Munaf Salim 17

function [V] = testcircuit(Y,I )

%UNTITLED this function provides the general solution of

% set of algebraic equations

V=Y/I;

>> Y=[0.15 -0.1 -0.05; -0.1 0.145 -0.025; -0.05 -0.025 0.075];

>> I = [5; 0; 2];

>> V=testcircuit(Y,I)

NODAL ANALYSIS

Dr. Munaf Salim 18

>> Y = [0.75 -0.2 0 -0.5; -5 1 -1 5; -0.2 0.45 0.166666667 -0.0666666667; 0 0 0 1];

>> I = [5 0 0 10]';

>> V=testcircuit(Y,I)

Page 10: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

10

NODAL ANALYSIS

Dr. Munaf Salim 19

>> Z = [40 -10 -30; -10 30 -5; -30 -5 65];

>> V = [10 0 0]';

>> I=testcircuit(Z,V)

Matlab Program Files

Dr. Munaf Salim 20

• You can have functions with multiple input parameters

function f = myfunction2(x,c)

f= x(1)^2 + c*x(2)^2;

• Or

function [f, g] = myfunction2(x,c)

f = x(1)^2 + c*x(2)^2;

g = x(1)+x(2);

>> [a b] = myfunction2([3 2],4)

Page 11: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

11

Matlab Program Files

Dr. Munaf Salim 21

• Input/Output

>> disp('The speed is')

>> disp(speed)

Type the following into a file, save, then run the script.

x = input('Input the value for the variable x')

s = input('Input the expression for the string s', 's')

k = menu('Choose a data marker', 'o', '*', '+', 'x');

x = 0:0.1:pi;

f = sin(x);

type = ['o', '*', '+', 'x'];

plot(x, f, type(k))

Matlab Program Files

Dr. Munaf Salim 22

• Relational Operations

>> x = [3 2 1]

>> y = [2 4 1]

>> z = (x < y)

>> z = (x > y)

>> z = (x == y)

>> z = (x ~= y)

>> z = (x <= y)

>> z = (x >= y)

>> z = (x >= 2)

>> z = y(x)

>> nz = ~z

>> x(nz)

Page 12: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

12

Matlab Program Files

Dr. Munaf Salim 23

• More on conditionals:

• any(x) returns a scalar =1 if any element in the vector x is nonzero and 0 otherwise.

• any(A) returns a row vector with same number of columns as A with ones and zeros depending on whether the corresponding column has any nonzero elements

• all(x) returns a scalar =1 if all elements in the vector x are nonzero and 0 otherwise.

• all(A) returns a row vector with same number of columns as A with ones and zeros depending on whether the corresponding column has all nonzero elements.

• find(A) returns an array of indices of the nonzero elements of A

Matlab Program Files

Dr. Munaf Salim 24

>> any([1 2 3])

>> any([0 0 0])

• Does y have any zero elements? (0 if yes, 1 if no)

>> all([1 2 3])

>> all([1 0 3])

for matrices:

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

>> any(A)

>> all(A)

Page 13: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

13

Matlab Program Files

Dr. Munaf Salim 25

• Logical Operators

Matlab Program Files

Dr. Munaf Salim 26

• Logical Operators

>> A = [1 1 0 0];

>> B = [1 0 1 0];

>> ~A

>> A&B

>> A|B

>> xor(A,B)

Page 14: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

14

Matlab Program Files

Dr. Munaf Salim 27

• The “find” Command

• The find command is a powerful command that can be used to locate elements in a vector that satisfy a certain criterion.

• >>x=[-3 0 7 5 -8 -9 0 4 5];

• >>index=find(x<0)

• >>x(index)

• >>index2=find(x==0)

• >>x=(index2)

• >>index3=find(abs(x)<4)

• >>x(index3)

Matlab Program Files

Dr. Munaf Salim 28

• Conditional statements:

if expression

commands

elseif expression

commands

elseif expression

commands

OPTIONAL

elseif expression

commands

else

commands

end

Page 15: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

15

Matlab Program Files

Dr. Munaf Salim 29

The switchThe switchThe switchThe switch----case constructcase constructcase constructcase construct

Dr. Munaf Salim 30

% Four-function calculator

num1=input(‘Enter a number: ‘);

num2=input(‘Enter a number: ‘);

op=input(‘Enter the operation: ‘)

switch op

case ‘+’

x=num1+num2

case’-‘

x=num1-num2

case’*’

x=num1*num2

otherwise

x=num1/num2

end

Page 16: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

16

Loop Constructs

Dr. Munaf Salim 31

• for Loop:

for index= start : step : end

Command statements

end

n=1;

m=0;

x(1)=0;

for k=1:3:100

m=m+k;

x(n+1)=x(n)+m/k;

n=n+1;

end

• x

• Here is another example with matrices:

A=[1,2,5;7,9,0];

for v=A

disp(v)

end

• Here is a demonstration of a “while” loop

• k=1;

• n=1;

• m=0;

• x(1)=0;

• while k<=100

• m=m+k;

• x(n+1)=x(n)+m/k;

• n=n+1;

• k=k+3;

• end

• x

Loop Constructs

Dr. Munaf Salim 32

• for Loop:

for index= start : step : end

Command statements

end

n=1;

m=0;

x(1)=0;

for k=1:3:100

m=m+k;

x(n+1)=x(n)+m/k;

n=n+1;

end

• x

A=[1,2,5;7,9,0];

for v=A

disp(v)

end

for k=1:10

for m=1:4

x=0.005*k;

y=30*m;

z(k,m)=10*exp(-y*x)*cos(120*pi*x);

end

end

Page 17: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

17

Loop Constructs

Dr. Munaf Salim 33

• Find the sum of the following series s=1+3+5+7+…+99

>>s=0.0;

>>for k=1:2:99

s=s+k;

end

>>s

Loop Constructs

Dr. Munaf Salim 34

• while loop

>>while condition

Command statements

end

>>

Page 18: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

18

Loop Constructs

Dr. Munaf Salim 35

• while loop

>>while condition

Command statements

end

>>

Loop Constructs

Dr. Munaf Salim 36

• while loop

Page 19: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

19

PolynomialsPolynomialsPolynomialsPolynomials

Dr. Munaf Salim 37

• P(s) = 2s2 + 4s + 5 can be expressed as a vector by entering the statement P=[2 4 5],

• The “roots” Command

PolynomialsPolynomialsPolynomialsPolynomials

Dr. Munaf Salim 38

• The “poly” & “polyval” Commands

• The poly command is used to recover a polynomial from its roots. If r is a vector containing the roots of a polynomial, poly(r) return a row vector whose elements are the coefficients of the polynomial.

• The polyval command evaluates a polynomial at a specified value. If p is a row vector containing the coefficients of a polynomial, polyval(p,s) return the value of the polynomial at the specified value s.

Page 20: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

20

PolynomialsPolynomialsPolynomialsPolynomials

Dr. Munaf Salim 39

Polynomial multiplicationPolynomial multiplicationPolynomial multiplicationPolynomial multiplication

Dr. Munaf Salim 40

• The product of two polynomial p(x) and q(x) is found by taking the convolution of their coefficients. MATLAB makes use of the conv function to obtain the coefficients of the required polynomial product. The coefficients should be in the order of decreasing power. Multiplication of more than two polynomials requires repeated use of conv.

Page 21: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

21

Polynomial multiplicationPolynomial multiplicationPolynomial multiplicationPolynomial multiplication

Dr. Munaf Salim 41

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 42

• >>plot (x,y, ‘linestyle_mark_color’);

• Plot a polynomial function:

>> a=[1,4,-15,9,1];

>> x=[-2:0.01:2];

>> f=polyval(a,x);

>> plot(x,f,'+-r'),xlabel('x'),ylabel('f(x)'),axis([-2,2,-9,9]),grid

Page 22: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

22

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 43

• Symbol of Lines, Marks & Colors

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 44

Page 23: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

23

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 45

• for more than one plot when plot(x,y,x,z,x,w) will not work

>> f=logspace(-1,3,200);

>> hf=1./((j*2*pi*f).^2-0.1*( j*2*pi*f)+10^2);

>> plot(f,abs(hf))

>> semilogx(f,abs(hf));

>> loglog(f,abs(hf));

>> semilogx(f,angle(hf));

>> semilogx(f,atan2(imag(hf),real(hf))*180/pi);

>> loglog(x,y)

>> semilogx(x,y)

>> semilogy(x,y)

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 46

• polar(theta,f)

• stairs(x,y)

• bar(x,y)

• stem(x,y)

• plotyy(x,y,u,v,’function’,’function’)

• plotyy(x,y,u,v,’plot’,’stem’)

• plotyy(log10(f),(abs(hf)),log10(f),angle(hf),'semilogy','plot')

• figure(n)

Page 24: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

24

Introduction to PlottingIntroduction to PlottingIntroduction to PlottingIntroduction to Plotting

Dr. Munaf Salim 47

• Polyfit

Dr. Munaf Salim 48

Page 25: Dr. Munaf Salim › lmsdatapool... · Dr. Munaf Salim 16 diary ex4_1.dat % program computes the nodal voltages % given the admittance matrix Y and current vector I % Y is the admittance

3/30/2015

25

Dr. Munaf Salim 49