Urgent. Matlab Code of matrix multiplication

  • MATLAB
  • Thread starter cks
  • Start date
  • #1
cks
165
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
175
1
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
 
  • #3
cks
165
0
Ok. Thanks. Seem like if I want to perform this operation, I need symbolic math toolbox.
 
  • #4
175
1
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:
  • #5
cks
165
0
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?
 
  • #6
cks
165
0
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
 
  • #7
175
1
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
 
  • #8
cks
165
0
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?
 
  • #9
175
1
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
 
  • #10
cks
165
0
Yes, I already have the equations in matrix form. The matrix is not a string, unfortunately.

Thanks again.
 
  • #11
175
1
So then you have the symbolic math toolbox?
 
  • #12
cks
165
0
I don't have symbolic math toolbox. My matlab is just a student version.
 
  • #13
175
1
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
 
  • #14
cks
165
0
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.
 

Related Threads on Urgent. Matlab Code of matrix multiplication

Replies
1
Views
873
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
2
Views
1K
Replies
2
Views
3K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
4
Views
622
  • Last Post
Replies
1
Views
1K
Top