Solving 3 equations with 3 unknowns using for loop ?

1 view (last 30 days)
I want to use 'for' loop to find 3 unknowns, how can I do that in matlab? Can someone please help me?
Xb and Xm have to be between -1 to 0.
My code for equations:
syms Xb Xm a
E1 = (Xb-Xm*25e-3)*(-230.6815e-6*a) == (1.05e3-1.01038e3)*9.81*1.2566e-6 ;
E2 = (Xb-Xm*50e-3)*(-108.65e-6*a) == (1.05e3-1.01075e3)*9.81*1.2566e-6 ;
E3 = (Xb-Xm*75e-3)*(-52.808e-6*a) == (1.05e3-1.01113e3)*9.81*1.2566e-6 ;
eqns = [E1,E2,E3];
S = vpasolve (eqns, [Xb,Xm a]);
Xb_slut = S.Xb
a_solut=S.a
Xm_solut=S.Xm
How can I solve these equations with 'for' loop?

Answers (2)

John D'Errico
John D'Errico on 22 Mar 2022
Edited: John D'Errico on 22 Mar 2022
syms Xb Xm a
E1 = (Xb-Xm*25e-3)*(-230.6815e-6*a) == (1.05e3-1.01038e3)*9.81*1.2566e-6 ;
E2 = (Xb-Xm*50e-3)*(-108.65e-6*a) == (1.05e3-1.01075e3)*9.81*1.2566e-6 ;
E3 = (Xb-Xm*75e-3)*(-52.808e-6*a) == (1.05e3-1.01113e3)*9.81*1.2566e-6 ;
eqns = [E1,E2,E3];
S = vpasolve (eqns, [Xb,Xm a]);
Xb_slut = S.Xb
Xb_slut = Empty sym: 0-by-1
a_solut=S.a
a_solut = Empty sym: 0-by-1
Xm_solut=S.Xm
Xm_solut = Empty sym: 0-by-1
Why would you want to use a for loop, to solve something that has no solution at all?
MATLAB returned empty results. That is a hint that it could find no solutions. But does that mean no solution can possibly exist? First, I'll expand those equations to see what happens.
E1 = expand(E1)
E1 = 
E2 = expand(E2)
E2 = 
E3 = expand(E3)
E3 = 
In each case, we see a linear equation formed from variables, but here, we see always the equations are a function of the product of exactly two variables, We always see Xm*a and Xb*a appear together.
Suppose we called the product XM*a as just a new variable then? Call it Xma. Similarly, since XB*a always apprar together, call it Xba.
syms Xma Xba
E1 = subs(E1,[Xm*a,Xb*a],[Xma,Xba])
E1 = 
E2 = subs(E2,[Xm*a,Xb*a],[Xma,Xba])
E2 = 
E3 = subs(E3,[Xm*a,Xb*a],[Xma,Xba])
E3 = 
That leaves us with THREE linear equations in the TWO unknowns, Xma, and Xba. This happens because in your equations, those variables are inextricably confounded. You cannot ever solve that confounding problem.
At best, you can solve the problem in the form of a minimum error formulation, what backslash does in MATLAB. Thus...
[A,B] = equationsToMatrix([E1,E2,E3],[Xma,Xba])
A = 
B = 
And now we can solve for the two unknowns as the vector:
XmaXba = double(A)\double(B)
XmaXba = 2×1
117.4524 0.8790
So we now have
Xma = XmaXba(1)
Xma = 117.4524
Xba = XmaXba(2)
Xba = 0.8790
Do those solutions exactly solve the original linear system of equations? NO. In fact, this is called an over-determeined linear system of equations. It has no exact solution. Backslash did as well as it could.
How well did it do? I'll compare the results here as two columns. You can see the solution is not perfect. But it is the closest possible solution.
vpa([A*XmaXba,B])
ans = 
Could you have done the above using a loop? Well, yes, if you were willing to wait essentially an immensely long time, you could have done that. But why in the name of god and little green apples would you want to do so? And even then, in your attempt to solve for THREE variables, thus Xm,Xb, and a, you would find there are infinitely many possible exactly equivalent solutions, since the variables are hopelessly confounded.
Finally, I see you want to find a "solution" where Xm and Xb are between -1 and 0. Is that possible? Sigh. You can arbitrarily choose infinitely many solutions that have that property.
As long as a is a NEGATIVE number with magnitude greater than 117.4524, then you can arbitrarily find Xm*a, such that this is satisfied. For example, choose a = -200. Then we would have
a = -200;
Xm = Xma/a
Xm = -0.5873
Xb = Xba/a
Xb = -0.0044
The solution is TOTALLY arbitrary of course. Just pick your favorite number for b, as long as it is more negative than -117.4524.
  1 Comment
Betul Karakuzu
Betul Karakuzu on 22 Mar 2022
Thank you for your suggestion. You said that I can give a number to 'a' value. However, I'm trying to find all value; Xb, Xm, and a.
I need to find something similar to 3.2E-4 of Xm. I cannot find them in the solution you suggested. However, I guess the values found differ for each equation.

Sign in to comment.


Torsten
Torsten on 22 Mar 2022
You can't get a solution that fulfills all three equations exactly.
The reason is that
((Xb-Xm*25e-3)*(-230.6815e-6))/((1.05e3-1.01038e3)*9.81*1.2566e-6) =
((Xb-Xm*50e-3)*(-108.65e-6))/((1.05e3-1.01075e3)*9.81*1.2566e-6) =
((Xb-Xm*75e-3)*(-52.808e-6))/((1.05e3-1.01113e3)*9.81*1.2566e-6)
simultaneously had to hold.
This would lead to Xb = Xm = 0.
This is the best you can get for your requirement:
fun = @(Xb,Xm,a)...
[(Xb-Xm*25e-3)*(-230.6815e-6*a) - (1.05e3-1.01038e3)*9.81*1.2566e-6; ...
(Xb-Xm*50e-3)*(-108.65e-6*a) - (1.05e3-1.01075e3)*9.81*1.2566e-6; ...
(Xb-Xm*75e-3)*(-52.808e-6*a) - (1.05e3-1.01113e3)*9.81*1.2566e-6];
x0 = [1;1;1];
x = lsqnonlin(@(x)fun(x(1),x(2),x(3)),x0,[0 0 -Inf],[1 1 Inf])

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!