warning all the time, don't know what is wrong
조회 수: 1 (최근 30일)
이전 댓글 표시
syms m n p a x y z lambda real
g = x + y + z -a;
L = (x).^m.*(y).^n.*(z).^p - lambda.*g
Lx = diff(L,x)
Ly = diff(L,y)
Lz = diff(L,z)
sol = solve([Lx==0,Ly==0,Lz==0,g==0],[x y z lambda])
sol.x
sol.y
sol.z
답변 (1개)
Walter Roberson
2024년 3월 22일
이동: Walter Roberson
2024년 3월 22일
The 0^n and 0^(m-1) occur because there are not constraints on m and n, so there is the possibility that 0^0 is being generated, and 0^0 is 1 whereas 0^anything_else is 0
syms m n p a x y z lambda real
g = x + y + z -a;
L = (x).^m.*(y).^n.*(z).^p - lambda.*g;
Lx = diff(L,x);
Ly = diff(L,y);
Lz = diff(L,z);
eqns = [Lx==0,Ly==0,Lz==0,g==0];
partial_lambda = solve(eqns(1), lambda, 'returnconditions', true);
%partial_lambda.conditions
eqns2 = subs(eqns(2:end), lambda, partial_lambda.lambda);
partial_x = solve(eqns2(3), x);
eqns3 = subs(eqns2([1:2 4:end]), x, partial_x);
partial_y = solve(eqns3(1), y, 'returnconditions', true);
%partial_y.y
%partial_y.conditions
eqns4 = subs(eqns3(2:end), y, partial_y.y);
syms parameter1 parameter2 real
partial_z1 = subs(solve(eqns4(1,1), z, 'returnconditions', true), sym('x'), parameter1);
partial_z2 = subs(solve(eqns4(2,1), z, 'returnconditions', true), sym('x'), parameter2);
partial_z3 = solve(eqns4(3,1), z, 'returnconditions', true);
%partial_z3.z
%partial_z3.conditions
back_z1 = partial_z1.z;
back_y1 = subs(partial_y.y(1), z, back_z1);
back_x1 = subs(partial_x, {y, z}, {back_y1, back_z1});
back_lambda1 = subs(partial_lambda, {x, y, z}, {back_x1, back_y1, back_z1});
solution1 = [x == back_x1, y == back_y1, z == back_z1, lambda == back_lambda1.lambda]
back_z2 = partial_z2.z;
back_y2 = subs(partial_y.y(2), z, back_z2);
back_x2 = subs(partial_x, {y, z}, {back_y2, back_z2});
back_lambda2 = subs(partial_lambda, {x, y, z}, {back_x2, back_y2, back_z2});
solution2 = [x == back_x2, y == back_y2, z == back_z2, lambda == back_lambda2.lambda]
back_z3a = partial_z3.z(1);
back_y3a = subs(partial_y.y(3), z, back_z3a);
back_x3a = subs(partial_x, {y, z}, {back_y3a, back_z3a});
back_lambda3a = subs(partial_lambda, {x, y, z}, {back_x3a, back_y3a, back_z3a});
solution3a = [x == back_x3a, y == back_y3a, z == back_z3a, lambda == back_lambda3a.lambda]
back_z3b = partial_z3.z(2);
back_y3b = subs(partial_y.y(3), z, back_z3b);
back_x3b = subs(partial_x, {y, z}, {back_y3b, back_z3b});
back_lambda3b = subs(partial_lambda, {x, y, z}, {back_x3b, back_y3b, back_z3b});
solution3b = [x == back_x3b, y == back_y3b, z == back_z3b, lambda == back_lambda3b.lambda]
back_z3c = partial_z3.z(3);
back_y3c = subs(partial_y.y(3), z, back_z3c);
back_x3c = subs(partial_x, {y, z}, {back_y3c, back_z3c});
back_lambda3c = subs(partial_lambda, {x, y, z}, {back_x3c, back_y3c, back_z3c});
solution3c = [x == back_x3c, y == back_y3c, z == back_z3c, lambda == back_lambda3c.lambda]
댓글 수: 5
Dyuman Joshi
2024년 3월 29일
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
참고 항목
카테고리
Help Center 및 File Exchange에서 Equation Solving에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!