Error: Undefined operator '+' for input arguments of type 'function_handle'

Here is my code;
function z = joy(t,z)
joymin = @(x) 150 - x;
joyadd = @(z) z;
z = integral2(@(x,y) normpdf(x,100,10).*normpdf(y,200,10),t,inf,joymin + joyadd,inf);
end
When I try to run joy(100,10), I get an error saying Undefined operator '+' for input arguments of type 'function_handle'.
The problem comes from joyadd because the code runs smoothly if I remove it.

 채택된 답변

You need to create a new anonymous function that adds the functions:
@(x)joymin(x) + joyadd(x)
so your full integral2 call becomes:
z = integral2(@(x,y) normpdf(x,100,10).*normpdf(y,200,10),t,inf,@(x)joymin(x) + joyadd(x),inf);
Your code will at least not throw that error.

댓글 수: 4

I tried that and now it doesn't give an error, but I feel like variable z didn't affect outcome of the integral at all.
I get same results if I completely delete function and only add the constant.
z = integral2(@(x,y) normpdf(x,100,10).*normpdf(y,200,10),t,inf,150,inf);
Okay, I just changed your solution a bit and it works. Thank you!
z = integral2(@(x,y) normpdf(x,100,10).*normpdf(y,200,10),t,inf,@(x)joymin(x) + joyadd(z),inf);
It is confusing that you are using z as an input argument and that your output argument is also z.
It is better to only use the same variable for input and output if you are "updating" the variable. For example,
function x = addone(x)
x = x + 1;
or
function options = eval50(options)
options.MaxFunEvals = 50;
Those kinds of uses are clear, but the way you used z is confusing.
@Emre Yazicioglu — My pleasure!
@Walter Roberson — Thank you. It was confusing for me as well.

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

추가 답변 (0개)

카테고리

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

질문:

2017년 6월 4일

댓글:

2017년 6월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by