Matrix Operations for Inputs as Function Handles

Hi!
I want to minimize this function with respect to h.
f1 = @(h) integral((psitildasq.*((1 - psibar).^2)),(-40),40,'ArrayValued',true);
where,
psitildasq = @(t) (1/n^2)*sum((cos(x.*t))).^2 + (1/n^2)*sum((sin(x.*t))).^2;
psibar = @(t,h) sum(exp((b.*t.*h).^2))./n;
I consider h and t as scalars. x is my (1,n) vector of input variables and b is another (1,n) vector of inputs.
But when I give the command
[h,hval] = fminbnd(f1,0,3);
Matlab gives me the error
"Undefined function 'minus' for input arguments of type 'function_handle'"
I don't quite understand where I am doing the mistake.
I would be grateful to learn about a way to write the function in the correct way.

 Respuesta aceptada

Guillaume
Guillaume el 11 de Mayo de 2015
The problem is caused by this part of f1:
1 - psibar
You're subtracting the function handle psibar from 1, which to matlab does not mean anything. What you want to subtract from 1 is the result of calling psibar with some argument, so you need to pass these arguments to psibar. You'll have a similar error with
psitilidasq.* ...
where you're multiplying a function handle with something. Again you need to pass some arguments to the function.
Possibly, you meant f1 to be:
f1 = @(h, t) integral(psitildasq(t) .* (1 - psibar(t, h).^2), -40, 40, 'ArrayValued', true);

3 comentarios

Anjali
Anjali el 11 de Mayo de 2015
Thank you for your answer. But when I correct the code again Matlab gives this error
"Error using @(h,t)integral((psitildasq(t).*((1-psibar(t,h)).^2)),(-40),40,'ArrayValued',true) Not enough input arguments."
Is it because the integral is removing the t from the function?
Stephen23
Stephen23 el 11 de Mayo de 2015
Editada: Stephen23 el 11 de Mayo de 2015
Defining f1 = @(h, t)... does not work because fminbnd expects a function in one variable.
The important thing is this: every variable in needs to have a value before you call fminbnd, except for the variable being optimized. This means you need to define values for t, b, n and x (did I miss any?). These values can be defined in the workspace:
x = 5;
fun = @(n) x*cos(n);
fun(val)
Or when the function is called:
fun = @(n,x) x*cos(n);
fun(val,5)
You can decide which of these works best for your situation.
Anjali
Anjali el 12 de Mayo de 2015
Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Mayo de 2015

Comentada:

el 12 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by