solving linear equations using matrices in MATLAB

조회 수: 7 (최근 30일)
Vitaly
Vitaly 2013년 4월 23일
Hi,
my script creates a matrix and 2 vectors using several 'for' loops and as an example they are returned as follows:
K =
1.0e+006 *
1.2409 0.6250 0.8153 0.1250
0.6250 3.6591 -0.1250 3.5375
0.8153 -0.1250 1.2409 -0.6250
0.1250 3.5375 -0.6250 3.6591
F =
1.0e+006 *
0.1733
1.3533
-0.1066
1.3371
U =
u3
v3
u4
v4
As can be seen, the 'U' vector is a set of variables and I need to solve 'K*U=F' for variables contained in 'U'.
When I try to do that using 'linsolve' or 'solve' I get unexpected results and a message that the inverse of my matrix is close to singular.
HOWEVER, when I make another script and put in the SAME matrix and vectors but BY HANDS it all works fine and I can't figure out what's wrong. I use the same commands 'linsolve' or 'solve' and the only difference is that the matrix in a script is calculated automatically.
Is that somehow related to the way MATLAB stores matrices created by loop functions and I need to change the state of the matrix to something after the loop?
Also, when I put the matrix by hands it displays it without the 1.0e+006 multiplier in front of it:
K11 =
1240900 625000 815300 125000
625000 3659100 -125000 3537500
815300 -125000 1240900 -625000
125000 3537500 -625000 3659100
can that be related??
Thanks in advance.

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 4월 23일
What you see on the command line may not be all of the data to the last precision.
Try
>> format longg
And then run your previous code that generated K etc. All this does is increase the precision that is shown on the commandline (doesn't affect the data itself.)
I would trust linprog or solve. When you manually put in the values you may only be considering only few significant digits which may be well conditioned but is actually ill conditioned.
  댓글 수: 1
Vitaly
Vitaly 2013년 4월 24일
I solved this problem yesterday and forgot to close the question here. The 'manual' matrix was rounded up and the inverse of that does exist, however, the original matrix was close to singular and that caused me problems.

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

추가 답변 (1개)

Kye Taylor
Kye Taylor 2013년 4월 23일
편집: Kye Taylor 2013년 4월 24일
If you're trying to solve for U in the expression K*U = F, don't use linprog or solve... use mldivide (the backslash operator)
U = K\F;
If you want better feedback, can you provide the comments that created the matrices?
  댓글 수: 1
Vitaly
Vitaly 2013년 4월 24일
Thanks, the problem is solved (see comments above). Different people recommend me different solvers and they all give me different results. I think this problem occurs because the problem is over-defined, i.e. more than one solution exists.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by