主要内容

获取生成函数的细节

这个例子展示了如何找到额外的参数的值在函数生成的prob2struct

创建一个非线性问题,将问题转换为一个结构prob2struct。名称生成的目标函数和非线性约束函数。

x = optimvar (“x”2);有趣= 100 * (x (2) - (1) ^ 2) ^ 2 + (1 - x (1)) ^ 2;概率= optimproblem (“目标”、有趣的);mycon =点(x, x) < = 4;prob.Constraints。mycon = mycon;x0。x = [1, 1.5);问题= prob2struct (x0,概率“ObjectiveFunctionName”,“。”,“ConstraintFunctionName”,“circle2”);

检查生成的约束函数的第一行circle2

类型circle2
函数[cineq,测查,cineqGrad ceqGrad] = circle2(数据源、extraParams) % circle2计算约束值和梯度% % [cineq,测查]= circle2(数据源、extraParams)计算%不等式约束值cineq测查和等式约束值%的数据源,在% extraParams使用额外的参数。% % [CINEQ,测查,CINEQGRAD CEQGRAD] = circle2(数据源,% EXTRAPARAMS)另外计算不等式约束梯度%值CINEQGRAD和等式约束梯度值在当前点CEQGRAD %。% %自动生成到2022 - 2月26日prob2struct 14:04:02 % %计算不等式约束。Hineq = extraParams {1};fineq = extraParams {2};rhsineq = extraParams {3};Hineqmvec = Hineq *数据源(:);cineq = 0.5 *点(数据源(:),Hineqmvec) +点(fineq,数据源(:))+ rhsineq;% %计算等式约束。测查= []; if nargout > 2 %% Compute constraint gradients. % To call the gradient code, notify the solver by setting the % SpecifyConstraintGradient option to true. cineqGrad = Hineqmvec + fineq; ceqGrad = []; end

circle2有第二个输入命名函数extraParams。为了找到这个输入的值,使用功能函数的函数处理存储在problem.nonlcon

F =函数(problem.nonlcon)
F =结构体字段:功能:“@ (x)乐趣(x, extraParams)类型:“匿名”文件:“mathworks /猛击/棒/ Bdoc22a /构建/ matlab /工具箱/ optim / problemdef optim / + / + / +内部problemdef / + / snapExtraParams编译。p '工作区:{[1 x1 struct]} within_file_path:“

访问额外的参数,查看工作空间领域的F

ws = F.workspace
ws =1 x1单元阵列{1 x1 struct}

继续在更深层次提取信息,直到你看到所有的额外的参数。

ws1 = ws {1}
ws1 =结构体字段:乐趣:@circle2 extraParams: {[2 x2双][2 x1双][4]}
ep = ws1.extraParams
ep =1×3单元阵列{2 x2双}{2 x1双}{[4]}
ep {1}
ans = (1,1) 2 (2, 2) 2
ep {2}
ans = 0稀疏:2 x1
ep {3}
ans = 4

现在你可以看了circle2文件清单和理解所有的变量是什么意思。

Hineq = 2 * speye (2);fineq =稀疏((0,0));rhsineq = 4;

另请参阅

相关的话题