Input argument contains an empty equation or variable.

4 次查看(过去 30 天)
I am trying to build an mlapp for lagrange's multiplier. I need to solve a system of equations to get the value of x,y,z. So, This is the code for solving.
syms x y lambda;
f=app.Function1EditField.Value;
func=str2sym(f);
g=app.Constrain1EditField.Value;
cons=str2sym(g);
L = func + lambda*lhs(cons);
%L=str2sym(l);
dL_dx=diff(L,x)==0;
dL_dy=diff(L,y)==0;
dL_dlambda=diff(L,lambda)==0;
system = [dL_dx; dL_dy; dL_dlambda];
[x_val, y_val,lambda_val] = solve(system, [x y lambda], 'Real', true);
the code doesn't show any error when I use it normally, outside the matlab gui code. But when I run the above code I get an error saying:
Error using sym/solve>getEqns
Input argument contains an empty equation or variable.
Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
please help!!
  3 个评论
Walter Roberson
Walter Roberson 2023-2-24
put a breakpoint at the line
L = func + lambda*lhs(cons);
and execute. When it gets there show us func and cons

请先登录,再进行评论。

回答(1 个)

Sarthak
Sarthak 2023-3-7
Hi,
The error message suggests that one of the equations in your system is empty or contains an empty variable. This can happen if the user does not provide a valid input for the function or constraint, resulting in an empty symbolic expression.
To fix this error, you can add some input validation to your code to ensure that valid expressions are entered by the user. For example, you can check that the input function and constraint are non-empty before proceeding with the Lagrange multiplier calculation:
f = app.Function1EditField.Value;
if isempty(f)
error('Please enter a valid function.');
end
g = app.Constrain1EditField.Value;
if isempty(g)
error('Please enter a valid constraint.');
end
func = str2sym(f);
cons = str2sym(g);
Refer to the isempty() documentation below:

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by