how define a conditional handle function?

As you know, for
f=@(x) sin(x)/x;
we get f(0)=NaN
if we would like get f(0)=0
again with
f=@(x) (x==0)*0+(x~=0)*sin(x)/x;
we will receive f(0)=NaN.
how can i dominate this problem?

1 件のコメント

Adam
Adam 2017 年 5 月 4 日
編集済み: Adam 2017 年 5 月 4 日
Please try to ensure you format your question in a readable way. The
if true
end
lines should not keep appearing around every bit of code. I don't really know why the '{} Code' button is setup to do this as so many people end up with it included in their code. Also don't include the lines of your question in the middle of one large code block - it is very confusing to read and separate out what is actually code and what is part of the question information.
I did the editing for you this time.

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

回答 (2 件)

Torsten
Torsten 2017 年 5 月 4 日
編集済み: Torsten 2017 年 5 月 4 日

0 投票

function main
x = [3 7.5 0 -0.03];
y = f(x)
function y = f(x)
i = find(x==0);
x(i) = 1;
y = sin(x)./x;
y(i) = 0;
end
Best wishes
Torsten.

3 件のコメント

xosro
xosro 2017 年 5 月 4 日
編集済み: xosro 2017 年 5 月 4 日
Thank you , but i would like use a handle function inside a script, so i can't define another script.
Jan
Jan 2017 年 5 月 4 日
編集済み: Jan 2017 年 5 月 4 日
Or a little bit simpler:
function y = f(x)
y = sin(x) ./ x;
y(x==0) = 0;
end
@xosro: Do not confuse a "handle function" with an "anonymous function", which is accessed by a "function handle".
Torsten
Torsten 2017 年 5 月 4 日
編集済み: Torsten 2017 年 5 月 4 日
My guess is that this function cannot be defined as a "one-liner" as
f=@(x)....
Best wishes
Torsten.

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

Jan
Jan 2017 年 5 月 4 日
編集済み: Jan 2017 年 5 月 4 日

0 投票

A function (see Torsten's answer) will be nicer and more efficient than an anonymous function. But if you realy have any good reasons:
f = @(x) sin(x) / [Inf(x==0), x(x~=0)];
This works for scalar x only. This would be better, because it is immediately clear, what it does:
function y = f(x)
y = sin(x) ./ x;
y(x==0) = 0;
end

7 件のコメント

xosro
xosro 2017 年 5 月 4 日
編集済み: Stephen23 2017 年 5 月 4 日
How can i use
function y = f(x)
y = sin(x) ./ x;
y(x==0) = 0;
end
inside a code?
Stephen23
Stephen23 2017 年 5 月 4 日
outval = f(inval)
Jan
Jan 2017 年 5 月 4 日
@xosro: What exactly is your question? Using a function is trivial: as Stephen wrote you use it exactly like you would call an anonymous function also. The only difference is that the function can use multiple commands and has more power. So please explain, what your problem is with using this function, because I do not see any difficulties yet.
xosro
xosro 2017 年 5 月 4 日
well, i have a code that use a function, where this function is undefined on zero point, Because of this i will have a conditional handle function that for zero returns zero and other point returns real value, i can't use another script because there are many variable that are used in the function and when define new script (as Stephen wrote), i receive: Undefined function or variable ' a variable'.
Jan
Jan 2017 年 5 月 5 日
@xosro: The term "conditional handle function" is not defined.
If your function needs many variables, simply define them as inputs or write a "nested function" (search in the documentation for details). If the function is larger, using an anonymous function is even a worse idea, because you cannot exmine it using the debugger.
xosro
xosro 2017 年 5 月 5 日
@Jan Simon: Ok, "nested function" is a good idea, but for using variables as inputs, i had some limitation so i have used "global variables" for using variables between scripts.
Thank you and other dears
Jan
Jan 2017 年 5 月 8 日
Avoid global variables. They cause more troubles than they solve. If you do not want to provide too many arguments, store them in a struct.

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

カテゴリ

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

タグ

タグが未入力です。

質問済み:

2017 年 5 月 4 日

コメント済み:

Jan
2017 年 5 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by