A Quick Introduction to Matlab

A Quick Introduction to Matlab

<p> 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.</p><p>Electric Circuit: Solutions to Simultaneous Equations </p><p>For the circuit shown find the current flowing in each branch. All R R potential differences in volts and 1 2 resistances in ohms. r 1 r 3 r 2 V1 15 R1 4 r1 2 I 3 V2 6 R2 3 r2 2 I 2 I 1 V3 4 r3 1</p><p>V 1 V 3 V 2 Kirchoff's laws Look at a junction and get:</p><p>I 1  I 3  I 2</p><p>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.   1I1  1I 2 1I 3  0</p><p> 6I1  0I 2 1I 3  11</p><p>0I1  5I 2 1I 3  10 Solving a set of simultaneous equations</p><p>These are now in the standard form for simultaneous equations:</p><p>1 1 1 I 1   0       M  C  V  6 0 1 I 2  1 1  where M is the matrix of the current coefficients,      0 5 1 I 3  1 0  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  1 V  C  M  1 V . </p><p>Page 1 Matrices and Matrix Algebra Let’s solve the matrix equation: C= M-1V using Matlab.</p><p>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; -11; -10] And that’s all the input data. </p><p>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.</p><p>Assignment: M1_Simul 1. Here’s the same assignment you did in a Spreadsheet.</p><p>For the circuit shown find </p><p>-the current flowing in R , R , and R , 1 2 3 R 3 V - the power dissipated in each of the batteries. 2</p><p>All potential differences are in volts and R 2 resistances are in ohms.</p><p>R 1 V 1 V1 12 R1 20</p><p>V2 9 R2 40</p><p>R3 30</p><p>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.</p><p>Page 2 Use the “det” function to find the determinant of the matrix M. Multiply M by its transpose. M’ is the transpose of M. What is the transpose of V? How does it differ from V?</p><p>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 2  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 2 x  x1 x2 x3 *x1 x2 x3  isn’t defined in linear algebra. </p><p>To get the dot product, the second vector has to be a column vector.</p><p> x1  2   x  x1 x2 x3 * x2     x3  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 ?</p><p>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</p><p>2 2 If I want a normal function of x then what I want to calculate is xn . 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</p><p>Page 3 Take a look at the plots: plot(x, x.*x) % element by element multiplication plot(x, x.^2) % element by element powers plot(x, 1./x) % element by element division</p><p>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.</p><p>Assignment: M1_ElementAlg</p><p>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.</p><p>Don’t forget…. Please include Analysis, Tests, and Results in your write up. </p><p>Page 4</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us