Solving 3 equations with 3 unknowns using for loop ?
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
John D'Errico
2022-3-22
编辑:John D'Errico
2022-3-22
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
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)
E2 = expand(E2)
E3 = expand(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])
E2 = subs(E2,[Xm*a,Xb*a],[Xma,Xba])
E3 = subs(E3,[Xm*a,Xb*a],[Xma,Xba])
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])
And now we can solve for the two unknowns as the vector:
XmaXba = double(A)\double(B)
So we now have
Xma = XmaXba(1)
Xba = XmaXba(2)
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])
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
Xb = Xba/a
The solution is TOTALLY arbitrary of course. Just pick your favorite number for b, as long as it is more negative than -117.4524.
Torsten
2022-3-22
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])
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!