0
$\begingroup$

I am new to Mathematica, and I am unsure on how to do this.

I am working with piecewise function

$$ f_N(x) := \begin{cases} 1 & \text{if}\;\;x = j\sqrt{3}, \quad j=1,...,N \\ 0 & \text{otherwise},\end{cases} $$

for some arbitrary $N$ that I define when I call the function.

I understand that I have to use the built in function Piecewise to be able to perform this, but am not sure how to include the fact that $N$ is a variable and not have to define the function again and again for every use case.

$\endgroup$

1 Answer 1

4
$\begingroup$
f[n_, x_] := Piecewise[{
   {1, (Sqrt[3] <= x <= n Sqrt[3]) && Mod[x, Sqrt[3]] == 0}
   }, 0]
$\endgroup$
2
  • $\begingroup$ Note in this answer I've avoided using IntegerQ and Q-functions because those won't work as expected with symbolic arguments as they must evaluate to True/False. $\endgroup$
    – flinty
    Aug 31, 2021 at 14:38
  • $\begingroup$ Thank you for your help, that works perfectly $\endgroup$
    – spaceman
    Aug 31, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.