1
$\begingroup$

I want to create a function which only accepts integers. if a value other than integers are given it should output zero. This is what I've tried so far. I'm trying to do some discrete time signal experiments like time shifting.

specifically i want to create a function y[n] = u[n] - u[n-5] and create another using it, x[n] = y[n/2] + 2 y[n/2-1/2] so that i can get the following graph,

Here's my copy paste-able code: https://www.wolframcloud.com/objects/a0b8ba05-a8cc-4cae-a075-6435e301d41a

the function i need

enter image description here

$\endgroup$
  • 2
    $\begingroup$ For example, use IntegerQ, for example: gamma[n_] := If[IntegerQ[n], (n - 1)!, 0]. You shouls also look at gamma[n_Integer] := (n - 1)!. $\endgroup$ – Moo Dec 10 '18 at 6:00
  • $\begingroup$ Thank you! so I was able to define y[n_] := If[IntegerQ[n], UnitStep[n] - UnitStep[n - 5], 0] $\endgroup$ – Ramith Hettiarachchi Dec 10 '18 at 6:20
  • 1
    $\begingroup$ y[_] = 0; y[n_Integer] := UnitStep[n] - UnitStep[n - 5] $\endgroup$ – Fred Simons Dec 10 '18 at 7:47
  • 1
    $\begingroup$ What about y[n_ /; IntegerQ[n]] := ... $\endgroup$ – Ulrich Neumann Dec 10 '18 at 9:59
1
$\begingroup$

Please upvote my answer if you consider it helpful.

I have been hurt many times.

In your code y[n_Integer] := (UnitStep[n] - UnitStep[n - 5]);, y[n] is only defined when its variable is Integer. That means if the variable of y[n] is a fractional number it outputs nothing. So please refer to my following code:

y[n_] := If[FractionalPart[n] == 0, UnitStep[n] - UnitStep[n - 5], 0]
DiscretePlot[{y[n/2] + 2 y[n/2 - 1/2]}, {n, -2, 12}, PlotRange -> All, PlotStyle -> Directive[PointSize[0.02], Red]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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