简体   繁体   中英

Using MATLAB's symbolic engine to solve a 4 by 4 linear system

I have what seems to me to be a very simple symbolic math problem. I have a linear system of 4 equations and 4 unknowns. The coefficients are non-numerical constants. I coded the problem up in MATLAB. My code is below. It ran for a couple of hours before I shut it down. To me, I should be getting an answer within minutes. I'm not sure what the problem is.

syms a b c d e f g h k l m n o p q r W X Y Z A B
eqn1=a*W+b*X+c*Y+d*Z==A;
eqn2=e*W+f*X+g*Y+h*Z==B;
eqn3=k*W+l*X+m*Y+n*Z==0;
eqn4=o*W+p*X+q*Y+r*Z==0;

Soln=solve([eqn1,eqn2,eqn3,eqn4],[W,X,Y,Z],'ReturnConditions',true);
SolnW=Soln.W
SolnX=Soln.X
SolnY=Soln.Y
SolnZ=Soln.Z
Conditions=Soln.conditions
Parameters=Soln.parameters

I have two questions.

(1) Is the way I have approached the problem efficient? For example, is perhaps MATHEMATICA or MAPLE more suited to the job?

(2) I anticipated a Cramer's-like solution with terms representing expanded forms of determinants. Of course, this is going to be ugly. Is there a way to get MATLAB to simplify the result algebraically?

With matlab and linear systems you should take a different approach, working with matrix like this, matlab really likes working with matrix, so this is the way that you should work using matlab. then your code is pretty fast:

>> syms a b c d e f g h k l m n o p q r W X Y Z A B
>> eqn1=a*W+b*X+c*Y+d*Z==A;
eqn2=e*W+f*X+g*Y+h*Z==B;
eqn3=k*W+l*X+m*Y+n*Z==0;
eqn4=o*W+p*X+q*Y+r*Z==0;
>> [A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [W, X, Y,Z])

A =

[ a, b, c, d]
[ e, f, g, h]
[ k, l, m, n]
[ o, p, q, r]


B =

 A
 B
 0
 0

>> linsolve(A,B)

ans =

 -(B*b*m*r - B*b*n*q - B*c*l*r + B*c*n*p + B*d*l*q - B*d*m*p - A*f*m*r + A*f*n*q + A*g*l*r - A*g*n*p - A*h*l*q + A*h*m*p)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
  (B*a*m*r - B*a*n*q - B*c*k*r + B*c*n*o + B*d*k*q - B*d*m*o - A*e*m*r + A*e*n*q + A*g*k*r - A*g*n*o - A*h*k*q + A*h*m*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
 -(B*a*l*r - B*a*n*p - B*b*k*r + B*b*n*o + B*d*k*p - B*d*l*o - A*e*l*r + A*e*n*p + A*f*k*r - A*f*n*o - A*h*k*p + A*h*l*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
  (B*a*l*q - B*a*m*p - B*b*k*q + B*b*m*o + B*c*k*p - B*c*l*o - A*e*l*q + A*e*m*p + A*f*k*q - A*f*m*o - A*g*k*p + A*g*l*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)

I hope this helps. this is a very general answer, so you shoul limit the posible values with symbolic assumption

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM