FUNCTION (INPUTS) Problem: I am unable to use a vector of inputs of a function as a substitute of listing the inputs each by each

3 views (last 30 days)
Imagine to have a function myfun such that [ output ] = myfun(a,b,c,d) and output=a+b+c+d where a,b,c,d are, for the sake of simplicity, scalars then It is possibile to create a vector(1x4) v such that myfun(v) provides the same result. This works for the Matlab's built-in function SUM.
My problem is that I am not able to manage this substitution of a vector of inputs in my function:
function [ sum_err ]=mydifference(a,s,vector_p,vector_t)
err=[];
for i=1:45
err(i)= ... blablabla...
i=i+1;
end;
sum_err=sum(err);
a and s are scalars, p and t are vectors
When I recall it from the command line putting mydifference(1,2,vector_p,vector_t) (i.e listing values for the scalars and vectors) everything works fine but once I create a vector_input=[1,2,vector_p,vector_t] and I evaluate my function in vector_input (i.e mydifference(vector_input) I receive this message/problem
>> mydifference(vector_input) Not enough input arguments.
I check the dimentions of my vector_input and of [a,s,vector_p,vector_t] and those match.
I don't understand how to cope with this difficulty
  2 Comments
Adam
Adam on 30 Mar 2017
Edited: Adam on 30 Mar 2017
I'm not sure why you are trying to combine all your arguments into a single vector. Your function takes explicitly 4 arguments and uses them by name so it will expect you to pass in all 4 unless one is not actually used, in which case it won't care if it was passed in or not (provided it is at the end of the list).
If you want to be able to program something that accepts either
[1, 2, 3] or 1, 2, 3
as arguments like some builtin functions then you have to do a fair amount of ugly work on handling the input arguments and almost certainly you would have to just use
doc varargin
to deal with the variable number of arguments. In a general situation it really isn't worth the effort.
enzo fiasco
enzo fiasco on 31 Mar 2017
Thanks for your answer! I managed to simplify my code taking vector_p and vector_t out from the input of my function and importing them few lines below using a script (i.e myimport). vectors p and t were data and I didn't know how to use them in my function without setting them as an input.
function [ sum_err ]=mydifference(a,s)
err=[];
myimport; %this import vector_p and vector_t
for i=1:45
err(i)=...blablabla
i=i+1;
end;
sum_err=sum(err);
end;
I wanted to put all my input in a vector because using the fmincon function you have to specify a starting value (ie X0 which is a vector) for mydifferent fuction and I think this implies giving values to my vector of parameters [a s vector_p vector_t]. (PS the only variables are "a" and "s" because p and t are data. After the simplification my X0 is a vector 1x2 [a s].)
For that reason I noticed that mydifference(X0) was not providing me any result as a simple build-in function does.

Sign in to comment.

Accepted Answer

Adam
Adam on 31 Mar 2017
I don't know anything about fmincon as I have never used it, but I assume it works like any other function that takes a function handle input.
When you convert your function to a function handle there are two distinct types of arguments that you can mix into it - those you hard-code at the time you define the function and those you give placeholders for which you will supply later e.g.
function y = myFunc( x, a )
y = x.^a;
end
This is a function of two arguments, but if you require to pass it in somewhere that requires just one argument you can 'bind' the other argument at the time you declare the function handle to it, i.e.
a = 7;
f = @(x) myFunc( x, a );
f( 5 );
Here you still have your myFunc defined with its two arguments, but your function handle, f, is a function of just 1 argument. The 'a' argument has been hard-coded into the function definition so that when you call f, you just give it x and a has already been defined as 7.
You can do the same thing to pass a function to something like fmincon. Your original function can take as many arguments as it likes so long as the function handle you create for it that you will pass to fmincon sets most of them at the time you define the function handle so that is satisfies the specification of 1 input or however many it needs.
  2 Comments
enzo fiasco
enzo fiasco on 31 Mar 2017
Thanks, for the answer and for Stephen's related link which explains a lot.
I will try to code the minimization of my function using fmincon and the function handle. Eventually you will see it as another question on the matlab forum.
I think this post has been greatly answered. Thanks

Sign in to comment.

More Answers (1)

Jeong-Hoon Park
Jeong-Hoon Park on 23 Jul 2017
Although there are many answer, I think I have the answer what you need to know. In MATLAB, function handle can get some parameters such as double, vector(or matrix) and so on. I think you can program this code using function handle(or inline function) which gets parameter type of vector. Like c++, vector is not need to fix the size of it. Good Luck.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!