How to use "@" functionhandle for multiple inputs? (to use with fmincon)

9 views (last 30 days)
Hallo,
I wrote a function (which works by itself), which can be called like this:
[output]=get_amps(input,par)
"output" can not be written as simple analytical function of "input".
Now I want to optimize the parameters "par" using fmincon:
input_a=a; % a is a vector in the workspace
par0=[100,5000];
out_a = @opt_a get_amps(input_a,par0); % here is the problem
objective = @opt_a sum((input_a-out_a).^2);
p_opt=fmincon(objective,par0);
In line3 I get the error:
" Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. ..."
How to use the @function handle with functions with multiple inputs?
Is there a better way to optimze parameters of a (non-analytical) function than fmincon?
I would be greatfull for any help.
best
Ralf
  2 Comments
Ralf Schmauder
Ralf Schmauder on 5 Mar 2022
This was indeed helpfull! However, now I am strugeling how to use this with fmincon:
I can specify which parameters to optimize - but is there a way to specify data (input_a) without making it global?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 5 Mar 2022
objective = @(p) sum( ( a - get_amps(a, p) ).^2);
par0=[100,5000];
p_opt=fmincon(objective,par0);

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!