Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:46:07+00:00 2026-05-25T21:46:07+00:00

How can I define a function f(x) in Mathematica that gives 1 if x

  • 0

How can I define a function f(x) in Mathematica that gives 1 if x is in [-5, -4] or [1, 3] and 0 otherwise? It’s probably something simple but I just can’t figure it out!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:46:07+00:00Added an answer on May 25, 2026 at 9:46 pm

    The basic construction you want is Piecewise, in particular the function you were asking for can be written as

    f[x_] := Piecewise[{{1, -5 <= x <= -3}, {1, 1 <= x <= 3}}, 0]
    

    or

    f[x_] := Piecewise[{{1, -5 <= x <= -3 || 1 <= x <= 3}}, 0]
    

    Note that the final argument, 0 defines the default (or “else”) value is not needed because the default default is 0.

    Also note that although Piecewise and Which are very similar in form, Piecewise is for constructing functions, while Which is for programming. Piecewise will play nicer with integration, simplification etc…, it also has the proper left-brace mathematical notation, see the examples in the documentation.


    Since the piecewise function you want is quite simple, it could also be constructed from step functions like Boole, UnitStep and UnitBox, e.g.

    UnitBox[(x + 4)/2] + UnitBox[(x - 2)/2]
    

    These are just special cases of Piecewise, as shown by PiecewiseExpand

    In[19]:= f[x] == UnitBox[(x+4)/2] + UnitBox[(x-2)/2]//PiecewiseExpand//Simplify
    Out[19]= True
    

    Alternatively, you can use switching functions like HeavisideTheta or HeavisidePi, e.g.

    HeavisidePi[(x + 4)/2] + HeavisidePi[(x - 2)/2]
    

    which are nice, because if treating the function as a distribution, then its derivative will return the correct combination of Dirac delta functions.


    For more discussion see the tutorial Piecewise Functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure that it is possible but I just can't do it, which is:
How can I define a function that will accept my type and return its
One can easily define a function that accepts a 1d array argument like this:
How can I define lists:append function with list comprehension? I want something like 1>
That is, in C, we can define a function like: func(){ static int foo
For throwing exceptions I know that you can declare/define the function with the implication
Can I define a function which, when called, inserts new locals into the caller's
I need an array to hold member-function-pointers of different classes. How can I define
In C# we can define a generic type that imposes constraints on the types
In Java you can define generic class that accepts only types that extend a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.