I would like to code the following function:
$$f(x)=\begin{cases} \dfrac{\sin(x)}{x} & x\neq 0 \\ 1 & x = 0. \end{cases} $$
I am doing the following:
f = @(x) (sin(x)./x.*(x~=0) + 1.*(x==0));
However, f(0)returns NaN, while it must return 1.
Do you see what I am missing? Thanks
The problem is that $\sin(0)./0$ is NaN, and multiplying by $0$ doesn't change that. The best solution is probably to use an .m file to write a function with an IF to deal with this.
One "hack" which "works" is by changing the function to evaluate at $x+eps$