Matlab Code for Matrix Multiplication

Click For Summary

Discussion Overview

The discussion revolves around using MATLAB for matrix multiplication and solving systems of linear equations. Participants explore how to express linear equations in matrix form and how to extract coefficients from equations to form matrices, as well as the tools required for symbolic manipulation in MATLAB.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a system of linear equations and seeks to express it in matrix multiplication form.
  • Another participant suggests using MATLAB's left divide operator to solve for the unknowns, indicating that symbolic math toolbox is not necessary for basic operations.
  • A different participant questions the formulation of the equations and suggests a specific format for defining matrices and vectors in MATLAB.
  • There is a discussion about the need for the symbolic math toolbox to extract coefficients from equations, with some participants proposing alternative methods using string representations of equations.
  • One participant expresses a desire to convert equations stored in a matrix format into strings for further manipulation, highlighting a limitation in MATLAB's handling of such cases.
  • Participants discuss the challenges of working without the symbolic math toolbox and the implications for solving equations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the necessity of the symbolic math toolbox, with some arguing it is essential for certain tasks while others believe it is not required for basic matrix operations. The discussion remains unresolved regarding the best approach to extract coefficients from equations stored in matrix form.

Contextual Notes

Participants express uncertainty about the capabilities of their MATLAB versions, particularly regarding the symbolic math toolbox, which affects their ability to manipulate equations and extract coefficients. There are also unresolved questions about how to effectively convert matrix equations into a format suitable for string manipulation.

cks
Messages
164
Reaction score
0
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.
 
Physics news on Phys.org
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
 
Ok. Thanks. Seem like if I want to perform this operation, I need symbolic math toolbox.
 
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:
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?
 
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
 
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
 
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?
 
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
Yes, I already have the equations in matrix form. The matrix is not a string, unfortunately.

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

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K