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.

 Risposta accettata

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 Commenti

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?
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.

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 10 Mag 2015

Commentato:

il 12 Mag 2015

Community Treasure Hunt

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

Start Hunting!

Translated by