Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Urgent. Matlab Code of matrix multiplication

  1. Apr 2, 2009 #1

    cks

    User Avatar

    Hihi,
    Let's say I have the linear equations ,A

    2x +3y +4z
    5x+6y-7z
    5x-4y+3z

    then I can always write as a matrix multiplication A= B*x
    matrix B =
    2 3 4
    5 6 -7
    5 -4 3

    times
    matrix x
    x
    y
    z


    Then., how can I tell matlab to express in the matrix multiplication form so that I can only have B.
     
  2. jcsd
  3. Apr 2, 2009 #2
    Search the help for "left divide." Basically, you'll rearrange your equation to x = B \ A, which is MATLAB notation for A = Bx, solve for x. MATLAB doesn't handle unknowns the same way other packages do (you need the symbolic math toolbox for that), but it does make it easy to solve this kind of problem. It's more similar to a programming language than a math package (always put your unknown on the left side of the equation...).

    -Kerry
     
  4. Apr 4, 2009 #3

    cks

    User Avatar

    Ok. Thanks. Seem like if I want to perform this operation, I need symbolic math toolbox.
     
  5. Apr 5, 2009 #4
    No no, you should be able to do this without any additional packages. Did you define your A and B matricies? Actually, looking at this again, it seems you have an error. Typically, the equation would be written A * x = b, but you have B * x = A (that's fine, it's just different from what I'm used to seeing).

    So continuing with your notation, how did you come up with your A matrix? B should be a matrix, and A and x are both vectors, right? The problem is that your system of equations are not equations (what are they equal to?).

    For the sake of an example, we'll say that your equations are:
    2x +3y +4z = -2
    5x+6y-7z = 5
    5x-4y+3z = 1

    So you can define B:
    B = [2 3 4;5 6 -7;5 -4 3];

    and a:
    a = [-2;5;1];

    Then solve for your x vector:
    x = B \ a

    Hope this helps,

    Kerry
     
    Last edited: Apr 5, 2009
  6. Apr 6, 2009 #5

    cks

    User Avatar

    Hihi,

    Kerry,

    You're right, you can write
    B = [2 3 4;5 6 -7;5 -4 3];
    BY HAND and then put into matlab so that with
    a = [-2;5;1];
    you can solve by using the notation \
    x = B \ a
    easily.

    But , MY problem is I want to the matlab to recognize A, I mean I want Matlab to get the matrix A from the equations

    2x +3y +4z = -2
    5x+6y-7z = 5
    5x-4y+3z = 1

    How can I do that?
     
  7. Apr 6, 2009 #6

    cks

    User Avatar

    I have a lot of these types of symbolic equations where I need to extract the coefficients in front of them in order to get a Matrix.

    2x +3y +4z = -2
    5x+6y-7z = 5
    5x-4y+3z = 1
     
  8. Apr 6, 2009 #7
    Ahh, I see. That's a different problem. Using the symbolic toolbox would be one way, but if you declare your equations as strings (like this):

    eqn1='2x +3y +4z = -2';
    eqn2='5x +6y -7z = 5';
    eqn3='5x -4y +3z = 1';

    then you can try to use sscanf(). Here is an example that builds your A matrix, b vector, and solves for x. If you have it in a different format, you can probably figure out how to modify this to suit.

    Row1=sscanf(eqn1, '%fx %fy %fz = %f')';
    Row2=sscanf(eqn2, '%fx %fy %fz = %f')';
    Row3=sscanf(eqn3, '%fx %fy %fz = %f')';

    A=[Row1(1, 1:3); Row2(1, 1:3); Row3(1, 1:3)];
    b=[Row1(1, 4); Row2(1, 4); Row3(1, 4)];

    x = A\b

    Hope this helps,

    Kerry
     
  9. Apr 6, 2009 #8

    cks

    User Avatar

    Thanks, that's a great help.

    But, I'd like to ask one more question .
    I mean for putting the equation as string
    eqn1='2x +3y +4z = -2';
    ,
    let's say i have all those equations in matrix
    A=[2*x+3*y+4*z;5*x+6*y-7*z;5*x-4*y+3*z]

    I cannot write
    eqn1 = 'A(1,1)'
    because Matlab can't recognize it as
    eqn1 = 2*x+3*y+4*z
    So, how can I write (acutally, I tried to figure this quite some time ago?
     
  10. Apr 6, 2009 #9
    Do you already have the equations in a matrix like that? It seems to me that you would need the symbolic math toolbox just to create your A matrix...

    If it's already a string, then you can do eqn1 = A(1,1).

    If it's not already a string, then I'd have to think about it some more. The first thing that comes to mind is to replace x, y and z with 1, so you're left with only your coefficients or maybe some other trickery like that.

    -Kerry
     
  11. Apr 6, 2009 #10

    cks

    User Avatar

    Yes, I already have the equations in matrix form. The matrix is not a string, unfortunately.

    Thanks again.
     
  12. Apr 6, 2009 #11
    So then you have the symbolic math toolbox?
     
  13. Apr 7, 2009 #12

    cks

    User Avatar

    I don't have symbolic math toolbox. My matlab is just a student version.
     
  14. Apr 7, 2009 #13
    Can you post code for how you created your equation matrix? I don't see how that is possible without having the symbolic math toolbox...

    -Kerry
     
  15. Apr 7, 2009 #14

    cks

    User Avatar

    Yes, my friend has math symbolic toolbox. Do you know what is the matlab command code for solving this?

    I have been working for my programme for quite some time, and I can't make it. I'm until this step. I hope I can get it done soon.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Urgent. Matlab Code of matrix multiplication
  1. Matlab codes (Replies: 2)

  2. Matlab code (Replies: 0)

  3. Matlab: Urgent (Replies: 1)

  4. Matlab Urgent (Replies: 6)

Loading...