calling function output variable names from a defined vector

3 次查看(过去 30 天)
i am defining very simple constraints function as below:
function[c_1 c_2 c_3] = nlcon(m)
c_1 = -0.875 + m(1) - m(2) + 1.75
c_2 = -1.75 - m(1) + 2*m(2) - m(3)
c_3 = -0.875 - m(2) + m(3) + 1.75
end
but instead of inserting the output variables manually (c1, c_2, c_3), i would like to define a set containing a variable number of constraints and i want to call the output variables from this sit. for example :
function [c] = const(m);
for i=1:3;
if i ==1;
c(i) = -0.875 + m(i) - m(i+1) + 1.75;
elseif i == 3;
c(i) = -0.875 - m(i-1) + m(i) + 1.75;
else
c(i) = -1.75 - m(i-1) + 2*m(i) - m(i+1);
end
end
but i get a wrong definition, any help?
then i tried to include that in the out

回答(1 个)

David Hill
David Hill 2020-9-28
Make sure you are saving the function as a code file (hit new function and enter and save the function there...not the command line)

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by