How to transfer additional parameters to the objective function when using fmincon to find the minimum value?

The following picture shows my planning problem. I try to use the function 'fmincon' find the minimum value of the 'F'. Howecer, when I build the objective function F.m file,it's confusing to me. My objctive function have a known parameter matrix 'H' which was not input by keyboard. So I try to transfer it to the objctive function. But there is an error--'Too many input parameters.' in the processing. I want to know how to built the objective function inclouding additional parameters. Thank you very much.图片1.png
The incorrect code(Matlab 2015b):
fun1=@(EB)fun;
A=double(vpa(dB_n));
b=[0.05
0.05
0.05];
Aeq=[];
beq=[];
lb=[];
ub=[];
nonlcon=@(EB)constraints;
options = optimoptions('fmincon','Algorithm','sqp');
[sol,Fval]=fmincon(fun1,EB_n,A,b,Aeq,beq,lb,ub,nonlcon,options,H); %‘EB’ represents varibles in the code.
The objective function:
function obj=fun(EB,H)
F=H*[EB(1)
EB(2)
EB(3)];
fx=F(1);
fy=F(2);
Fc=fx^2+fy^2;
obj=(norm(Fc,2));
end
I want to know how to deal with the problem.
Best wishes.

4 Comments

H = some appropriate value
fun1 = @(EB) fun(EB, H);
A=double(vpa(dB_n));
b=[0.05
0.05
0.05];
Aeq=[];
beq=[];
lb=[];
ub=[];
nonlcon = @(EB) constraints(EB, H); %assuming H is needed in constraints as well
options = optimoptions('fmincon','Algorithm','sqp');
[sol,Fval] = fmincon(fun1,EB_n,A,b,Aeq,beq,lb,ub,nonlcon,options); %‘EB’ represents varibles in the code.
Firstly, thank you very much.
The program was modified according to your method. However, Matlab can't parse this sentence ' H = some appropriate value' , and it's not a MATLAB statement. The word 'appropriate' has a red wavy line under it.
图片3.png
B_n = input('Enter a scalar value for B_n? ');
dB_n = input('Enter a scalar value for dB_n? ');
Hbm_n = input('Enter a scalar value for Hbm_n? ');
dHbm_n = input('Enter a scalar value for dHbm_n? ');
The point is that you need to assign some particular value to those variables, through whatever means is appropriate for your situation. You cannot just use
fun1 = @(EB) fun(EB, B_n, dB_n, hbm_n, dHbm_n);
if B_n, dB_n, hbm_n, dHbm_n are unknown.
If the idea is to minimize with EB, B_n, dB_n, hbm_n, dHbm_n all as parameters to be varied in order to achieve the minimum value, then a different approach is needed.
Haha,I was too stupid...
I try to delete the sentence, and it works.
Thank you, best wishes to you!

Sign in to comment.

Answers (0)

Asked:

on 21 Jan 2020

Commented:

on 21 Jan 2020

Community Treasure Hunt

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

Start Hunting!