Skip to main content

Matrix calculation with MATLAB Programming

First, let's create a simple vector with 9 elements called a.

a = [1 2 3 4 6 4 3 4 5]
a =

1 2 3 4 6 4 3 4 5

Now let's add 2 to each element of our vector, a, and store the result in a new vector.

Notice how MATLAB requires no special handling of vector or matrix math.

b = a + 2
b =

3 4 5 6 8 6 5 6 7

Creating graphs in MATLAB is as easy as one command. Let's plot the result of our vector addition with grid lines.

plot(b)
grid on

MATLAB can make other graph types as well, with axis labels.

bar(b)
xlabel('Sample #')
ylabel('Pounds')

MATLAB can use symbols in plots as well. Here is an example using stars to mark the points. MATLAB offers a variety of other symbols and line types.

plot(b,'*')
axis([0 10 0 10])

One area in which MATLAB excels is matrix computation.

Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.

A = [1 2 0; 2 5 -1; 4 10 -1]
A =

1 2 0
2 5 -1
4 10 -1

We can easily find the transpose of the matrix A.

B = A'
B =

1 2 4
2 5 10
0 -1 -1

Now let's multiply these two matrices together.

Note again that MATLAB doesn't require you to deal with matrices as a collection of numbers. MATLAB knows when you are dealing with matrices and adjusts your calculations accordingly.

C = A * B
C =

5 12 24
12 30 59
24 59 117

Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or vectors using the .* operator.

C = A .* B
C =

1 4 0
4 25 -10
0 -10 1

Let's find the inverse of a matrix ...

X = inv(A)
X =

5 2 -2
-2 -1 1
0 -2 1

... and then illustrate the fact that a matrix times its inverse is the identity matrix.

I = inv(A) * A
I =

1 0 0
0 1 0
0 0 1

MATLAB has functions for nearly every type of common matrix calculation.

There are functions to obtain eigenvalues ...

eig(A)
ans =

3.7321
0.2679
1.0000

... as well as the singular value decomposition.

svd(A)
ans =

12.3171
0.5149
0.1577

The "poly" function generates a vector containing the coefficients of the characteristic polynomial.

The characteristic polynomial of a matrix A is

$$det(\lambda I - A)$$

p = round(poly(A))
p =

1 -5 5 -1

We can easily find the roots of a polynomial using the roots function.

These are actually the eigenvalues of the original matrix.

roots(p)
ans =

3.7321
1.0000
0.2679

MATLAB has many applications beyond just matrix computation.

To convolve two vectors ...

q = conv(p,p)
q =

1 -10 35 -52 35 -10 1

... or convolve again and plot the result.

r = conv(p,q)
plot(r);
r =

1 -15 90 -278 480 -480 278 -90 15 -1

At any time, we can get a listing of the variables we have stored in memory using the who or whos command.

whos
  Name      Size            Bytes  Class     Attributes

A 3x3 72 double
B 3x3 72 double
C 3x3 72 double
I 3x3 72 double
X 3x3 72 double
a 1x9 72 double
ans 3x1 24 double
b 1x9 72 double
p 1x4 32 double
q 1x7 56 double
r 1x10 80 double

You can get the value of a particular variable by typing its name.

A
A =

1 2 0
2 5 -1
4 10 -1

You can have more than one statement on a single line by separating each statement with commas or semicolons.

If you don't assign a variable to store the result of an operation, the result is stored in a temporary variable called ans.

sqrt(-1)
ans =

0 + 1.0000i

As you can see, MATLAB easily deals with complex numbers in its calculations.

Powered by www.mathworks.com

Comments

Popular Posts

Studying The Charecteristics of a Transmission Line by MATLAB Programming

M-File Programming for Transmission Line: clc clear disp( 'STUDYING THE CHARECTERISTICS OF A TRANSMISSION LINE' ) disp( '***************************************************' ) disp( ' ' ) V=input( 'Receiving end line voltage in volt =' ); L=input( 'length of transmission line in km =' ); Pr=input( 'Rated power at receiving end load =' ); pf=input( 'power factor=' ); I=Pr/(sqrt(3)*V*pf); a1=acos(pf); Vr=V/sqrt(3) Ir=I*cos(a1)-j*I*sin(a1) r=input( 'resistance/km/phase=' ); x=input( 'inductive reactance/km/phase=' ); R=r*L; X=j*x*L; Z=R+X if V>20000 y=input( 'capacitive susceptance/km/phase=' ); Y=j*y*L m=Y*Z; end if V disp( 'SHORT TRANSMISSON LINE' ) disp( '**********************' ) A=1 B=Z C=0 D=A end if V>20000 & V disp( ...

Plotting Voltage

M-File Programming For Subplotting: clc clear subplot(2,2,1) x=0:pi/1800:2*pi; y=sin(x); plot(x,y); title( 'Plot of sin(x)' ); grid subplot(2,2,2) x=0:pi/1800:2*pi; y=sin(x-2*pi/3) plot(x,y); title( 'Plot of sin(x-120)' ); grid subplot(2,2,3) x=0:pi/1800:2*pi; y=sin(x+2*pi/3) plot(x,y); title( 'Plot of sin(x-240)' ); grid (try it) M-File Programming For Three-Phase Voltage: clc clear t=0:pi/1800:2*pi; v1=100*cos(t); plot(t,v1); grid hold on v2=100*cos(t-2*pi/3); plot(t,v2); v3=100*cos(t+2*pi/3); plot (t,v3); title( 'Plotting Three-Phase Voltage of v1(t), v2(t) and v3(t)' ) xlabel ( 'Time in seconds' ) ylabel ( 'Voltage in volts' ) text (6,6, 'v1(t)' ) text (3,4, 'v2(t)' ) text (1,1, 'v3(t)' ) (try it) Discussion: · Plotting figure is also called graphing figure in MATLAB. · In this programming, we can easily ...

Per-Unit Quantities

M-File Programming For Finding Per-Unit Impedance: -clc -clear -disp('CHANGING THE BASE OF PER-UNIT QUANTITIES') -x=input('given base MVA='); -y=input('given base KV='); -z=input('given per unit impedence='); -a=input('new base MVA='); -b=input('new base KV='); -disp('new per unit impedence='); -pu=z*(y/b)^2*(a/x) (Try it) M-File Programming For Finding Per-Unit Impedance: -clc -clear -disp('PERCENTAGE TRANSMISSION EFFICIENCY OF TX-LINE') -x=input('Receiving end voltage='); -y=input('Load current per-phase='); -z=input('Receiving end power factor='); -a=input('Sending end current='); -b=input('Resistance per-phase='); -disp('Percentage voltage transmission efficiency='); -PVTE=(((x*y*z)/((x*y*z)+(a^2*b)))*100 (try it)