Error using solve command, says input and output are inconsistent?
20 views (last 30 days)
Show older comments
When I type in my code it tells me that an input of 2 variables and an output of 7 variables is inconsistent. What am i doing wrong? I'm pretty the problem is with this last bit of code
syms I x1 x2 x3 x4 x5 g1 g2 lambda1 lambda2
I==1.06*x1+1.08*x2+1.1*x3+1.12*x4+1.15*x5
g1==x1+x2+x3+x4+x5
g2==x2+1.5*x3+2.5*x4+5*x5
[x1 x2 x3 x4 x5 lambda1 lambda2]=solve(diff(I,x1)-lambda1==0,diff(I,x2)-lambda1-lambda2==0,diff(I,x3)-lambda1-1.5*lambda2==0,diff(I,x4)-lambda1-2.5*lambda2==0,diff(I,x5)-lambda1-5*lambda2==0,g1==250000,g2==500000,x1,x2,x3,x4,x5,lambda1,lambda2)
0 Comments
Answers (1)
Andrew Newell
on 8 Feb 2015
Edited: Andrew Newell
on 8 Feb 2015
Are you using the Matlab editor? If so, the orange marks indicate problems with syntax that are causing your problems. For example, hover your mouse over the double equals sign in
I==1.06*x1+1.08*x2+1.1*x3+1.12*x4+1.15*x5
and you will see a message like "Possible inappropriate use of == operator. Use = if assignment is intended." Hover it over the variable x2 in [x1 x2 ...] and you will see "Best practice is to separate output variables with commas." Fix all the highlighted problems and you get:
syms I x1 x2 x3 x4 x5 g1 g2 lambda1 lambda2
I=1.06*x1+1.08*x2+1.1*x3+1.12*x4+1.15*x5;
g1=x1+x2+x3+x4+x5;
g2=x2+1.5*x3+2.5*x4+5*x5;
[x1, x2, x3, x4, x5, lambda1, lambda2]=solve(diff(I,x1)-lambda1==0,diff(I,x2)-lambda1-lambda2==0,diff(I,x3)-lambda1-1.5*lambda2==0,diff(I,x4)-lambda1-2.5*lambda2==0,diff(I,x5)-lambda1-5*lambda2==0,g1==250000,g2==500000,x1,x2,x3,x4,x5,lambda1,lambda2);
For which you get the response, "Warning: Explicit solution could not be found." Oh, well.
NOTE ADDED: I see why you don't get any solutions. The first five equations in the solve command evaluate to:
53/50 - lambda1 = 0
27/25 - lambda2 - lambda1 = 0
11/10 - (3*lambda2)/2 - lambda1 = 0
8/25 - (5*lambda2)/2 - lambda1 = 0
23/20 - 5*lambda2 - lambda1 = 0
Five incompatible equations in two unknowns.
0 Comments
See Also
Categories
Find more on Get Started with MuPAD in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!