MATLAB Answers

mr mo
0

How can I solve an equation in a While loop?

Asked by mr mo
on 12 Apr 2018
Latest activity Commented on by Walter Roberson
on 16 Apr 2018
Hi. I have a equation like below.
[m1 m2 ; m3 m4] * [d1 ; d2] = [c1 ; c2]
The values of m1, m2, m3, m4 and c1, c2 change in a while loop.
I want to find the d1 and d2 values in every Iteration of the mentioned while loop.
I wrote this, but some times it return Nan values.
[d1 ; d2] = [m1 m2 ; m3 m4] \ [c1 ; c2];
How can I do that?
Thanks a lot.

  0 Comments

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 12 Apr 2018

That equation does not always have solutions. Your m might be singular or the calculation might overflow or might contain inf in ways lead to nan.
If you have the symbolic toolbox sometimes you can get further by putting sym() around the two sides of the \ operator, but if that helps you probably need to go back and do the calculation of the m and c values as sym.

  3 Comments

At the command line type
dbstop if naninf
and run. It should stop when it detects that a nan has come into existance. At that point use
dbup
as many times as needed to get to your workspace for your function. Tell us what the m1, m2, m3, m4, c1, c2 values are. As this can be sensitive to the last bit, it would be good if you could also provide the output of num2hex() on those variables.
Hi. Thanks. m1, m2, m3, m4, c1 and c2 are some values that changes in every iteration of the while loop.
For example they might be equal to these values 2.31, 0.94, 0.94, 0.44, -0.15, -0.03 respectively.
These values changes in the next iteration and so on. but at the end by using the above mentioned code, I receive Nan values of d1 and d2, and I am confused what to do.
Please follow the debugging instructions I indicated with dbstop and so on.

Sign in to comment.


Answer by Torsten
on 12 Apr 2018

Maybe m1, m2, m3, m4, c1, c2 are NaN ?
And use
D = [m1 m2 ; m3 m4] \ [c1 ; c2];
d1 = D(1);
d2 = D(2);
And always make sure that det(M) does not equal 0 before computing D.
Best wishes
Torsten.

  0 Comments

Sign in to comment.