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.

 채택된 답변

Guillaume
Guillaume 2015년 5월 11일

0 개 추천

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

Anjali
Anjali 2015년 5월 11일
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 2015년 5월 11일
편집: Stephen23 2015년 5월 11일
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 2015년 5월 12일
Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 5월 10일

댓글:

2015년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by