سؤال

Why can Matlab solve the following system of equations that way:

eq1 = 'x1+x2+x4=1';
eq2 = 'x3+x4 = 2';
eq3 = '2*x1+3*x2+1*x3+4*x4=3';
eq4 = '3*x1+4*x2+2*x3+6*x4=p';
[p a b c d] = solve(eq1,eq2,eq3,eq4,p,x1,x2,x3,x4);

but not if I use the following code?

A = sym([1 1 0 1; 0 0 1 1; 2 3 1 4; 3 4 2 6]);
x = [x1 x2 x3 x4].';
b = sym([1 2 3 p].');
[p a b c d] = solve(A(1,:)*x==b(1),A(2,:)*x==b(2),A(3,:)*x==b(3),A(4,:)*x==b(4),p,x)

The first thing gives a value for p, and x1 to x4, while the second does not find any solution.

Thanks for an answer!

هل كانت مفيدة؟

المحلول

In R2012b neither of those work. One obtains warnings and, if anything, only parametrized solutions. You should check what you entered.

Why? First, rank(A) returns three, which means that you don't have four independent equations for your four-dimensional system. Second, even if you had four equations, you're trying to solve for five unknowns (p as well). You may need another constraint. Also note that the output of your calls to solve overwrites your symbolic variable p.

If you're trying to solve systems of symbolic equations, don't use solve, look into linsolve. Or, you can solve for p and a subset of x with your non-full rank system:

x = sym('x',[4 1]);
syms p;
A = sym([1 1 0 1; 0 0 1 1; 2 3 1 4; 3 4 2 6]);
b = sym([1 2 3 p].');
[p_ x1 x2 x3] = solve(A*x==b,p,x(1),x(2),x(3)) % results will be function of x4
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top