I'm using Matlab to solve for the constrained optimization of f(x,y,z) = 6x+6y+5z with a constraint of 3x^2+3y^2+5z^2 = 29.
My code looks like this:
clc
syms x y z t l
f = 6*x + 6*y +5*z
c = 3*x^2+3*y^2+5*z^2
c1 = 29
gFunc = gradient(f)
gConst = gradient(c)
eqn1 = gFunc(1,1) == l * gConst(1,1)
eqn2 = gFunc(2,1) == l * gConst(2,1)
eqn3 = gFunc(3,1) == l * gConst(3,1)
eqn4 = c == c1
[xsoln,ysoln,zsoln,lsoln] = solve([eqn1,eqn2,eqn3,eqn4],[x,y,z,l])
When I run the code, I get an error in saying that [xsoln,ysoln,zsoln,lsoln] = ... has too many output arguments, but I am solving 4 equations for 4 variables, and want 4 outputs, so I don't see how I am getting this error.
Any tips? Thanks!

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Okt. 2020

1 Stimme

clc
syms x y z t l
f = 6*x + 6*y +5*z
f = 
6x+6y+5z
c = 3*x^2+3*y^2+5*z^2
c = 
3x2+3y2+5z2
c1 = 29
c1 = 29
gFunc = gradient(f)
gFunc = 
665
gConst = gradient(c)
gConst = 
6x6y10z
eqn1 = gFunc(1,1) == l * gConst(1,1)
eqn1 = 
6=6lx
eqn2 = gFunc(2,1) == l * gConst(2,1)
eqn2 = 
6=6ly
eqn3 = gFunc(3,1) == l * gConst(3,1)
eqn3 = 
5=10lz
eqn4 = c == c1
eqn4 = 
3x2+3y2+5z2=29
[xsoln,ysoln,zsoln,lsoln] = solve([eqn1,eqn2,eqn3,eqn4],[x,y,z,l])
xsoln = 
(22)
ysoln = 
(22)
zsoln = 
(11)
lsoln = 
1212
Works for me in R2020a and R2020b . Which release are you using?

7 Kommentare

Walter Roberson
Walter Roberson am 7 Okt. 2020
Sorry about the output format. That last bit really looks like the following:
R2020a
I had it working for problems with only [x,y,l] but as soon as I added in z it gave me that error.
It works up to the line that is supposed to give me potential points where it could be a max or min:
[xsoln,ysoln,zsoln,lsoln] = solve([eqn1,eqn2,eqn3,eqn4], [x,y,z,l])
For the results from before that I get:
f =
6*x + 6*y + 5*z
c =
3*x^2 + 3*y^2 + 5*z^2
c1 =
29
gFunc =
6
6
5
gConst =
6*x
6*y
10*z
eqn1 =
6 == 6*l*x
eqn2 =
6 == 6*l*y
eqn3 =
5 == 10*l*z
eqn4 =
3*x^2 + 3*y^2 + 5*z^2 == 29
Tyler Reohr
Tyler Reohr am 7 Okt. 2020
Is it possible that it's something with the naming convention I used? I've read that it's possible for Matlab to think you're calling a function within the vector with certain names, but xsoln, ysoln, zsoln, and lsoln aren't any commands I could find.
What shows up for
which -all solve
>> which -all solve
solve is a variable.
C:\Program Files\MATLAB\R2020a\toolbox\symbolic\symbolic\@sym\solve.m % Shadowed sym method
Tyler Reohr
Tyler Reohr am 7 Okt. 2020
I couldn't get it to work so I reinstalled it to clear the cache or any settings I might have arbitrarily changed on accident. It worked after that so I don't believe it was a problem with the code. Don't know exactly what went wrong, but it's working now. Thanks!
Walter Roberson
Walter Roberson am 7 Okt. 2020
You had accidentally assigned a value to a variable named solve

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by