passing a function to another function

1 回表示 (過去 30 日間)
R yan
R yan 2015 年 2 月 3 日
コメント済み: R yan 2015 年 2 月 3 日
Hi
I am trying to pass the following function "haar" to the function integral. I am getting the following error. plz suggest. thanks
function val = haar(x)
if ((0<=x) && (x<0.5))
val=1;
elseif ((0.5<= x) && (x < 1))
val =-1;
else
val =0;
end
end
EDU>> fhnadle=@haar;
EDU>> integral(fhnadle,0,1)
Operands to the || and && operators must be convertible to logical scalar values

採用された回答

Sean de Wolski
Sean de Wolski 2015 年 2 月 3 日
編集済み: Sean de Wolski 2015 年 2 月 3 日
integral calls the function with a vector x, you're expecting x to be a scalar in your haar function. E.g:
>> [1 2 3] && 1
The easiest thing for you to do is to modify haar to use logical indexing on val:
function val = haar(x)
val = zeros(size(x)); % assume zero
val(0<=x & x<0.5) = 1; % set ones
val(0.5<=x & x<1) = -1; % set -1s
end
  1 件のコメント
R yan
R yan 2015 年 2 月 3 日
thank you. it works fine.
Can I write the same using symbolic?? I tried using symfun but dont know how to write the IF-else part.
thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by