; A Quick Introduction to Matlab(1)
Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

A Quick Introduction to Matlab(1)


  • pg 1
									     Matrix Algebra vs Element by Element Algebra
     The Mat in Matlab stands for matrix. The fundamental design was created to do matrix
     algebra easily and efficiently. Perhaps unfortunately, this means that the shortcuts we use
     to do normal algebra are a bit more complicated. Let’s do both types of algebra in this
     assignment by starting with the solution to simultaneous equations that you did using a
     spreadsheet and then going into the shortcut algebra used for normal equations.

     Electric Circuit: Solutions to Simultaneous Equations

     For the circuit shown find the
     current flowing in each branch. All
                                                      R1                         R2
     potential differences in volts and
     resistances in ohms.
      V1 15 R1           4     r1     2             r1                     r3                        r2
      V2     6     R2    3     r2     2                                               I2
      V3     4                 r3     1                    I1
                                                     V1                     V3                      V2
          Kirchoff's laws
     Look at a junction and get:
     I1  I3  I2
     Now examine the left and right loops: 15  2I1  4I1 1I3  4  0 ,
     4 1I3  3I2  6  2I2  0 .
     This gives us a set of three equations that we can put in standard form with a number
     multiplying each of the currents and the constants on the other side of the equal sign.
        1I 1  1I 2  1I 3  0
      6 I 1  0 I 2  1I 3  11
        0 I 1  5I 2  1I 3  10

     Solving a set of simultaneous equations
     These are now in the standard form for simultaneous equations:
                    1 1 1  I1   0 
                                
                                    
      M  C  V  6 0 1  I2  11 where M is the matrix of the current coefficients,
                                
                                    
                                        10
                      0 5 1 I3   
     C is the vector of the currents and V is the vector of the potentials. We can solve for C by
     finding the inverse of M and then multiplying from the left,
                                   M 1  M  C  M 1V  C  M 1V .

                                                                                           Page 1
Matrices and Matrix Algebra
Let’s solve the matrix equation: C= M-1V using Matlab.

To enter a matrix in Matlab, we’ll just make a 3 by 3 matrix:
M = [ 1 -1 1;
        -6 0 1;
         0 -5 -1]
The semicolons inside the brackets make new rows in the matrix. We also need a column
vector for the voltages.
V = [ 0;
And that’s all the input data.

Now let’s do some algebra. To invert the matrix call
M^-1         % invert the matrix
To solve for the currents we need to calculate M-1V.
C = M^-1*V
This is far easier in Matlab than in Excel. Check that this is the same answer that you got
using a spreadsheet.

Assignment: M1_Simul
1. Here’s the same assignment you did in a Spreadsheet.

For the circuit shown find
-the current flowing in R1, R2, and R3,
- the power dissipated in each of the batteries.

All potential differences are in volts and                R2
resistances are in ohms.
     V1    12     R1    20                                      V1
     V2     9     R2    40
                  R3    30

2. Matlab has a powerful set of tools available for matrices.
For the matrix given for the first circuit above, show that M-1M is the identity matrix.
Use the “det” function to find the determinant of the matrix M.

                                                                                        Page 2
Multiply M by its transpose. M’ is the transpose of M. What is the transpose of V? How
does it differ from V?

Reminder…. Please include Analysis, Tests, and Results in your write up.

Assignment: M1_VectorProd
                                                                          
Another important thing to try is the dot product of two vectors, x  x  x . First you’ll

need some x values, so make an array: x = 1:0.5:10. If we “square” x, you’ll see that
xSquared = x*x and xSquared = x^2 both fail because
x 2  x1 x2 x3  * x1 x2 x3  isn’t defined in linear algebra.

To get the dot product, the second vector has to be a column vector.
                        x1 
                        
 x 2   x1 x 2 x3  *  x 2 
                       x 
                        3
Try taking the transpose of x to make x into a column vector. Put a print out of this in
your report. Then try multiplying x by its transpose. What is the value of x2? What is
                               
the magnitude of | x | x  x ?

Element by Element Algebra
Most of the time we’re going to use matrices in a different way. You’ve already seen that
we can plot functions using plot(x,y) where x is an array of numbers and y is an array of
numbers. We can also use arrays as a shortcut in functions like y  x 2 and y  1 / x .
Here, we don’t want to find the magnitude of the vector x, by taking its square. Nor do
we want to find the inverse of the vector x. To see what we’re trying to do we need some
x values:
x = 1:0.5:10
If I want a normal function of x2 then what I want to calculate is xn2 . Each element of x
needs to be squared. Matlab does this by adding a period before the *, like “.*” or a
period before the carrot, “.^”.
y = x.*x
y = x.^2
To divide, it’s the same thing; add a dot. So, y  1 / x is calculated for each element.
y = 1./x
Take a look at the plots:

                                                                                       Page 3
plot(x, x.*x)              % element by element multiplication
plot(x, x.^2)              % element by element powers
plot(x, 1./x)              % element by element division

Now, that’s all there is to doing the hard stuff. There are a couple things that don’t
require dots, namely addition z  x  y and multiplying or dividing by
constants z  2 * x / 35 .
plot(x, x+y)               % matrix addition is element by element
plot(x, 2*x/35)            % matrix mult/div by a const.
This can be pretty tricky and you will generate lots of error messages due to missing
periods while using Matlab. Therefore, this assignment using the shortcut algebra is
perhaps the most important assignment for Matlab.

Assignment: M1_ElementAlg
Plot the function, f (t )  (t  1) /(t  1) on the range -5<t<5 using 20 steps in t.
Plot the intensity, I ( )  cos 2 ( ) on the range 0<<2using 20 steps in .
Plot a Gaussian, G( x)  e  ( x ^ 2) /  on the range -5<x<5 using 20 steps in x. Let  = 10.

Don’t forget…. Please include Analysis, Tests, and Results in your write up.

                                                                                            Page 4

To top