I keep getting the message: Unbalanced or unexpected parenthesis or bracket. How would I fix it? I'm trying to solve for the maximum and minimum with a function and it's constraint.

1 visualización (últimos 30 días)
syms x y lam
f = 2*x^4-x^2+3*y^2
g = x^2+y^2+3*y-9
fx = diff(f,x)
fy = diff(f,y)
gx = diff(g,x)
gy = diff(g,y)
[alam ax ay] = solve( fx-lam*gx, fy-lam*gy)
[bx by] = solve( gx,gy )
T = [ ax ay subs(g,{x y},{ax ay}) subs(f,{x y}, {ax ay })({bx by}) subs(g,{x y},{
bx by}) subs(f,{x y}{bx by})]
double(T)
double(T([1,2,3,4,7,8],[1,2,3,5]))

Respuesta aceptada

Steven Lord
Steven Lord el 22 de Abr. de 2019
T = [ ax ay subs(g,{x y},{ax ay}) subs(f,{x y}, {ax ay })({bx by}) ...
subs(g,{x y},{bx by}) subs(f,{x y}{bx by})]
This part is not valid MATLAB syntax.
subs(f,{x y}, {ax ay })({bx by})
I suspect what you want it to make T a two row matrix where the first consists of ax and ay and the results of substituting those into g and f and the second consists of bx and by and the results of substituting those into g and f. If that's the case:
T = [ ax, ay, subs(g, {x y}, {ax ay}), subs(f, {x y}, {ax ay}); ...
bx, by, subs(g, {x y}, {bx by}), subs(f, {x y}, {bx by})]
If that's not what you want explain in words, not code, what you expect / want that section of code to do. Once we know your goal we may be able to help you express that in code.
  3 comentarios
Alejandro Avelar
Alejandro Avelar el 22 de Abr. de 2019
Essentialy, I want to make 3 colums with 4 rows. the 3 columns with x , y, and the max/min values . the 4 rows would be for fx,fy, gx, gy. I am stuck on the double T
Steven Lord
Steven Lord el 22 de Abr. de 2019
Your T matrix only has 3 rows and 8 columns, so asking for something in the 4th, 7th, or 8th row of T can't work. If you were to transpose T so it's an 8-by-3 those row indices would work, but you can't ask for something in the 5th column of a 3 column matrix.

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 22 de Abr. de 2019
The error message tells you where to look: Line: 11 Column: 21
  4 comentarios
Alejandro Avelar
Alejandro Avelar el 22 de Abr. de 2019
I put comma where you said and also put a comma on line 10 between ay and =bx.
I got output of
f =
2*x^4 - x^2 + 3*y^2
g =
x^2 + y^2 + 3*y - 9
fx =
8*x^3 - 2*x
fy =
6*y
gx =
2*x
gy =
2*y + 3
alam =
0
0
0
ax =
0
-1/2
1/2
ay =
0
0
0
bx =
0
by =
-3/2
T =
[ 0, 0, -9, 0, 0, -3/2, -45/4, 27/4]
[ -1/2, 0, -35/4, -1/8, 0, 0, 0, 0]
[ 1/2, 0, -35/4, -1/8, 0, 0, 0, 0]
ans =
0 0 -9.0000 0 0 -1.5000 -11.2500 6.7500
-0.5000 0 -8.7500 -0.1250 0 0 0 0
0.5000 0 -8.7500 -0.1250 0 0 0 0
Index exceeds matrix dimensions.
Error in sym/subsref (line 841)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Untitled (line 13)
double(T([1,2,3,4,7,8],[1,2,3,5]))
Any idea on the error for the last part?
Alejandro Avelar
Alejandro Avelar el 22 de Abr. de 2019
Essentialy, I want to make 3 colums with 4 rows. the 3 columns with x , y, and the max/min values . the 4 rows would be for fx,fy, gx, gy. I am stuck on the double T

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by