how to solve system of linear equations with unorganized unknowns?

hi everybody. I have a system of n equations n unknowns and there are two forms of this system. in the first form the system of equations is in the form of A*X=Y. in the second form the system of equations is in the form of A*X=B*Y. A and B are the matrix of constant factors. some of the unknowns are in the matrix X and the others are in the matrix Y(X and Y are columns). so the problem is that unknowns are not in one matrix so they are unorganized. i can define unknowns as zero. if there is another way to define unknowns, please let me know. so how can i solve these systems? please guide me how to solve these systems and how to define unknowns? there are several ways to organize unknowns and put them in just one matrix but i want to know if there is any way to solve the system without organizing unknowns. if all the unknowns were in one matrix then it would be easy to solve the system with linsolve in matlab.

 Réponse acceptée

John D'Errico
John D'Errico le 23 Mar 2015
Modifié(e) : John D'Errico le 23 Mar 2015
Just move all of the unknowns to the left hand side.
A*X - B*Y = 0
Think of it like
[A,-B]*[X;Y] = 0
Any knowns in that set among the a and y variables, you move to the right hand side and sum them up. They are all constants. I fail to see the problem. How would you solve it using pencil and paper? (Answer: Exactly the same way.)
My guess is you are getting confused because you think you have TWO sets of unknowns, mixed in with knowns, in both X and Y. In fact, you can do the entire procedure in only several lines of code, fully vectorized. TRY IT! You might surprise yourself. (Take a shot and try it. REALLY. See how it goes. If you still get confused, then just comment here and I'll show you how.)

5 commentaires

Take a shot and try it. REALLY. See how it goes, on just a simple problem. If you still get confused, then just comment here, showing what you tried, and I'll show you how.
thanks for your response. i know what you are saying but i am speaking about a matrix that its size is 70*70 or it can be greater(I mean A and B). this problem is for calculating stress by using FEM method. for each row in these matrix, either X element or Y element is unknown and another element is known. for example if the system is in the first form like A*X=Y, then we use a method named penalty method(i don't know if it is the exact name) but it requires to write a special code. i want to know if there is any alternative method that is simpler and easier for coding. for example if there is any way that i just specify the unknowns and matlab obtains them.
So what? I never said the size of the array matters in the least. In fact, I have code that does similar things with many THOUSANDS of variables, some known, some unknown.
As for needing to use penalty functions, this is not remotely needed. Sorry, but not. As I said, this should all take only a very few lines, in EITHER form. So come on, MAKE AN EFFORT. TRY IT!
For example, suppose we have the problem
A = magic(3)
A =
8 1 6
3 5 7
4 9 2
B = (magic(3)-4)^2
B =
19 -5 -5
-5 19 -5
-5 -5 19
Yeah, I know, I'm not terribly creative with my matrices. Lets presume that A*X = B*Y.
Now, presume that we know the value of
X(1) == 1
X(3) == -2.5
Y(2) == 7
So, when we move everything to the left hand side combining the arrays such that
[A,-B]*[X;Y] = 0
knowns = [1 3 5];
unknowns = setdiff(1:6,knowns);
we can think of the vector of variables as
XY = NaN(6,1);
XY(knowns) = [1;-2.5;7];
Here, I've inserted NaN where we need to solve for an unknown.
OK, so I've done all the hard work here. What is the next step? If you don't think about it, you won't learn. There are three unknowns. Come on. What remains is just two lines of code.
let me try your method for the previous problems that i have solved and see what is the answer. i will respond later. thanks
In the example I just gave, here is my solution...
AB = [A,-B];
XY(unknowns) = AB(:,unknowns)\(-AB(:,knowns)*XY(knowns))
XY =
1
14.625
-2.5
4.875
7
10
(As I said, two lines of code.)
As a test to confirm this is indeed a solution...
A*XY(1:3) - B*XY(4:6)
ans =
0
0
0
In the other case, where you have
A*X = Y
with some elements of both X and Y unknown, this is IDENTICAl to the above more general problem, except that here we have B=eye(numel(Y)). No penalty functions are needed. That is simply a bad idea when the solution is so trivial. Honestly, this really is not hard to do. I call it trivial, but I've been writing the linear algebra code for things like this for years now. No matter what, it is still easy.

Connectez-vous pour commenter.

Plus de réponses (1)

thanks. it seems that it works. i tested your method on one of my previous problems and the answers was the same except one(the right answer was 31.3936 but the answer obtained from this method was 31.3963). maybe this little error is not important but i will test this method on other problems too. again thanks.

2 commentaires

Errors on that order of magnitude are often the result of how you input the data. That is, if you entered the numbers to that precision, expect errors in the last digit or so, since the true numbers that were stored in MATLAB were not exactly those values.
As I said above, for the A*X=Y problem, just use B as an identity matrix, then you can solve it the same way.
I tested your method for AX=Y. i have not tested for the second form yet. but i will do it as soon as possible. I should mention that i have the exact answers from Ansys. the two sets of answers from ansys and penalty method are exactly the same. but one of the answers of your method has error. I should mention that I have 35 answers that only one of them is not exactly the same. the others are correct.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by