Solving Matrix elements by using while iteration in Matlab

I have a known 5X6 sized M matrix, and a 6x1 sized K matrix which its 4 elements unknowns and two knowns. P matrix obtained by multiplication of these two matrices and has different 5 elements all which function of an x variable. Altogether, there are 5 equations and 5 unknowns (four from the K, and one from the P(which is x))
How can I obtain a while iteration in Matlab to solve these 5 unknowns? Is this possible in Matlab with the while or for loop? `
M[5X6]*K[6X1]=P[5X1]
P=[constant*x, constant2*x, constant3*x, constant4*x, constant5*x)

 Accepted Answer

Hello nut, I suppose you could figure a convoluted way to use a while loop if you dreamed up some iterative process and stopped when you got to a small enough tolerance. However, given the responses you are getting, maybe it's time to give up on the while loop. Anyway, that idea is not necessary since you have a linear problem in five equations five unknowns, so you can just solve for them. I will take the dimensions in the example you have, and let's assume that the known quantities in K are in rows 2 and 5.
create the 5x2 matrix A by taking columns 2 and 5 from M, in order.
create the 5x4 matrix B by taking the remaining columns of M, in order.
create the 2x1 column vector u by taking the known 2nd and 5th elements of K, in order.
make a knowns vector, knowns = A*u which is 5x1.
make a 5x1 column vector from your known constants in p.
let mB = -B.
create the 5x5 matrix mBp by concatenating the column vector p onto the right hand side of mB.
now you have the linear system
knowns = mBp*yx
which you can just solve with backslash to find unknowns vector yx which is 5x1.
In yx, the first four elements are the unknown y's and the last element is unknown x.

More Answers (1)

Please stop asking the same question over and over again.
NO you cannot solve it using a while or for loop.
P = sym('P',[6 1])
syms x
M = rand(5,6);
P(1) = rand(1);
P(6) = rand(1);
rhs = rand(5,1)*x;
result = solve(M*P == rhs)
result =
struct with fields:
P2: [1×1 sym]
P3: [1×1 sym]
P4: [1×1 sym]
P5: [1×1 sym]
x: [1×1 sym]
vpa(result.P2)
ans =
-0.18252717543791916818862612728557
...
vpa(result.x)
ans =
-0.096463182153227195863683959815006
Now please stop asking if this can be solved using a while loop.
(Yes, in fact, you could have put a while loop in there that does absolutely nothing. Will that make you happier?)

2 Comments

Thank you but as far as I know here is not connected to the Stackoverflow. And I couldn't get it why is not possible :) What are you saying man?
Yes, you COULD come up with some iterative scheme that would solve the problem using a while loop. But why would you bother? I just showed you how to solve the problem, but there was NO while loop needed. In fact, there is no conceivable reason why you would want to use a while loop here when one is not needed.
For some reason, it seems you think this problem needs a while loop. If someone told you to use a while loop, they simply did not know what they were talking about.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!