Hi everybody, I have a little trouble issues with my function handles.
In particular I am trying to describe a function handle which has a specific value (related to two variables) in a range of one of the two vairables, and 0 otherwise.
E_2rad=0.79;
@(freq_wave,wave_angle) (0<= wave_angle <= pi-E_2rad) .* ((2.25/4)*ro_w*g*B*0.75).*(sin(E_2rad+wave_angle).^2+(2.*freq_wave.*(vel_nave/g).*(cos(E_2rad).*cos(E_2rad+wave_angle)...
-cos(wave_angle)))) + (wave_angle > pi-E_2rad) .* 0
the function written as above mentioned returns a value different from 0 if wave_angle > pi-E_2rad instead and I cannot understand why and how to fix it. Thanks in advance for your help

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 21 日

0 投票

You need to define multiple conditions separately/individually i.e -
This
(0<= wave_angle <= pi-E_2rad)
should be -
(wave_angle >=0) && (wave_angle <= pi-E_2rad)

4 件のコメント

Gianluca Angelini
Gianluca Angelini 2023 年 11 月 21 日
Hi Dyuman: thanks again for your support.
in this case, I decided to avoid your solution because, after the definition of this function handle i need to sum it with other function handles and integrate them. In fact, after calling a double integration and after separating the multiple conditions, this message appears:
Operands to the || and && operators must be convertible to logical scalar values.
any other suggestion to solve this other trouble?
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 21 日
編集済み: Dyuman Joshi 2023 年 11 月 21 日
A silly mistake from my side - It should be a single ampersand i.e. & .
If there is still an error after making the aforementioned change, please share your code and state the error you get.
Gianluca Angelini
Gianluca Angelini 2023 年 11 月 21 日
Don't worry. You couldn't have known about integrals.
By the way all the problems aforementioned are fixed. Thanks again!
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 21 日
編集済み: Dyuman Joshi 2023 年 11 月 21 日
The anonymous function is vectorized and so, for the given comparison, I should have noted that it should be a single ampersand. (I really should be going to sleep earlier :P)
Nonetheless, glad to know that your issue is solved. Happy to have helped!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by