I can not use the " lhs " instruction,but it seems that lots of people can use it.What is the problem ?

1 次查看(过去 30 天)
I take this " https://de.mathworks.com/matlabcentral/answers/436044-is-there-anyone-to-help-me-for-lagrange-multipliers-method " as reference of writing the matlab coding of lagrange function,however,when i just copy it into my matlab and simulate,the window said that
Undefined function 'lhs' for input arguments of type 'sym'.
Error in Untitled4 (line 4)
L = A + lambda * lhs(V); % Langrange function
Why will i have this problem?because it seems that there is nobody has this problem when they simulate this file,the matlab version i use is R2015a,is the version problem ?
syms l b h lambda
A = l * b + 2 * b * h + 2 * l * h; % area of needed sheet metal (thickness = 1, because we are lazy guys)
V = l * b * h - 10 == 0; % volume constraint 10 m³
L = A + lambda * lhs(V); % Langrange function
dL_dl = diff(L,l) == 0; % derivative of L with respect to l
dL_db = diff(L,b) == 0; % derivative of L with respect to b
dL_dh = diff(L,h) == 0; % derivative of L with respect to h
dL_dlambda = diff(L,lambda) == 0; % derivative of L with respect to lambda
system = [dL_dl; dL_db; dL_dh; dL_dlambda]; % build the system of equations
[l_val, b_val, h_val,lambda_val] = solve(system, [l b h lambda], 'Real', true) % solve the system of equations and display the results
results_numeric = double([l_val, b_val, h_val,lambda_val]) % show results in a vector of data type double
V_res = l_val * b_val * h_val % calculate the resulting volume (should be 10, if correct)
A_res = l_val * b_val + 2 * b_val * h_val + 2 * l_val * h_val % calculate the needed amount (area) of sheet metal

采纳的回答

Walter Roberson
Walter Roberson 2019-1-25
lhs() was not added until R2017a.
In R2012a and later, you can define:
First = @(expr) expr(1);
lhs = @(V) First(children(V));

更多回答(1 个)

madhan ravi
madhan ravi 2019-1-25

类别

Help CenterFile Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by