Reputation: 978
Say that I have an anonymous function with the inputs v and config:
obj_fun = @(v, config) config.dt*(config.e_w*(v(1)^2 + v(2)^2 + config.e_s))*config.m + 2*sqrt((config.G(1)^2 - config.p(1) - config.dt*v(1))^2 + (config.G(2) - config.p(2) -config.dt*v(2))^2)*sqrt(config.e_w*config.e_s)*config.m;
Now, let's say I want I have the values of config and I just an anonymous function in terms of v.
So, I will have:
obj_fun_2 = @(v)...
How can I do that. The main motivation behind this is that I want to use the function, fmincon, but it seems that fmincon only works if your anonymous function has only one input. How can I address this issue? I remember seeing this before. How can I solve this problem.
So, I want something like,
fmincon(obj_fun(..., config),guess, A,B).
where guess is where the algorithm initially starts and A and B are the parameters for the constraints. I just want some variant of this.
Upvotes: 0
Views: 108
Reputation: 1851
If you have an anonymous function obj_fun = @(v, config)
with two arguments and a known value for one called config_value
you can make a new anonymous function with just the first value by writing:
obj_fun2 = @(v) obj_fun(v, config_value);
Upvotes: 1
Reputation: 12908
In the past, I have done the following to use functions with ode45
that required more than the allowed x
and t
inputs. I do not know that the same approach will work with anonymous functions, but I expect that it will work if you save your function in a new file. I will adapt the method here to your example.
First, save your function in a new file myfunc.m
with a top line:
function val = myfunc(v, config);
% your function here, returning "val"
Next, in your calling m-file, wrap your function in a handle that basically disguises it as just a function of v
:
h = @(v)myfunc(v, config); % you might need to put "guess" in place of "v" here
where config
is defined in your calling m-file. Finally, pass this handle in place of the function to fmincon
:
fmincon(h, guess, A, B);
I do not think you provided everything I need to test this, but as I said I have used this approach in the past to wrap an ODE that is a function of several inputs in such a way that I can pass it to ode45
.
Upvotes: 0
Reputation: 3194
I don't know if this is what you want, but here my answer:
for an anonymous function defined as
test = @(a,v) 2*v
You can call it without a
like this
test([],3)
However, if you have test = @(a,v) a*v
, it won't work.
Upvotes: 0