solving large number of simultaneous linear equations

조회 수: 2 (최근 30일)
Kamal Bera
Kamal Bera 2017년 4월 21일
댓글: Walter Roberson 2017년 4월 21일
Suppose I am solving the following problem:
dK=sym('dK',[2 2]);
I_LHS=[3 4;2 2]*dK*[1 2;1 1];
I_RHS=[36 47;20 26];
eqn1=I_LHS(1,1)==I_RHS(1,1);eqn2=I_LHS(1,2)==I_RHS(1,2);
eqn3=I_LHS(2,1)==I_RHS(2,1);eqn4=I_LHS(2,2)==I_RHS(2,2);
eqns=[eqn1,eqn2,eqn3,eqn4];
Sol=vpasolve(eqns);
dK=double([Sol.dK1_1,Sol.dK1_2;Sol.dK2_1,Sol.dK2_2])
the solution it is giving is dK=[1 3;2 4], which is absolutely fine. Now for my actual larger problem (having 11200 equations and 11200 variables) it is really difficult to implement this way, because I have to write eqn1=I_LHS(1,1)==I_RHS(1,1);........;eqn11200=I_LHS(20,560)==I_RHS(20,560); and eqns=[eqn1,.....;eqn11200]; and so for dK of last line of code. Note that obtaining dK is just a part of my original code. In fact for this large problem MATLAB shows: " earlier syntax errors confuse code analyzer (or a possible analyzer bug)" at the end of for loop of the code. Can anyone help me to solve this problem is a better way?

채택된 답변

Walter Roberson
Walter Roberson 2017년 4월 21일
eqns = I_LHS - I_RHS;
sol = solve(eqns);
dK = reshape( structfun(@double, sol), size(I_LHS.')) .';
Notice the two transposes. My tests indicate that the field names of the sol structure would be in the order
dK1_1: [1×1 sym]
dK1_2: [1×1 sym]
dK2_1: [1×1 sym]
dK2_2: [1×1 sym]
which is row order fastest. To convert to column order fastest as is needed for your dK array, you reshape with the number of columns as the first size, and then transpose.
  댓글 수: 3
Bjorn Gustavsson
Bjorn Gustavsson 2017년 4월 21일
Why such an obfuscated solution? Why not using something that looks sensible:
M1 = vpa([3,4;2 2]);
M2 = vpa([1 2;1 1]);
I_RHS = vpa([36 47;20 26]);
dK = M1\(I_RHS/M2);
Nice neat clean, linear equations solved as linear equations should be solved.
Walter Roberson
Walter Roberson 2017년 4월 21일
In my opinion, using both mldivide and mrdivide in an expression is more obfuscated than solve.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2017년 4월 21일
If you simply use the standard numerical matrix capabilities of matlab something like this should be your solution:
% I_LHS == [3 4;2 2]*dK*[1 2;1 1]
M1 = [3 4;2 2];
M2 = [1 2;1 1];
% I_LHS == M1*dK*M2
% I_LHS/M2 == M1*dK
% M1\(I_LHS/M2) == dK
dK = M1\(I_LHS/M2)
Provided that your larger matrices are well-conditioned.
HTH

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by