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.

 採用された回答

Star Strider
Star Strider 2017 年 6 月 4 日

2 投票

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 件のコメント

Emre Yazicioglu
Emre Yazicioglu 2017 年 6 月 5 日
編集済み: Emre Yazicioglu 2017 年 6 月 5 日
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);
Emre Yazicioglu
Emre Yazicioglu 2017 年 6 月 5 日
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);
Walter Roberson
Walter Roberson 2017 年 6 月 5 日
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.
Star Strider
Star Strider 2017 年 6 月 5 日
@Emre Yazicioglu — My pleasure!
@Walter Roberson — Thank you. It was confusing for me as well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by