Search code examples
wolfram-mathematica

Defining a function on an interval


Fairly basic question, but one I can't seem to find an answer to- How can I define a function f(x) on an x interval [0, 10]?


Solution

  • There are almost always several different ways of writing anything using Mathematica.

    One way is:

    f[x_]:=Piecewise[{{x^2,0<=x<=10}}]
    

    which will have the value x^2 over [0,10] and 0 elsewhere. You can look at the documentation for Piecewise and see how you can change the value elsewhere.

    Another way is

    f[x_/;0<=x<=10]:=x^2
    

    which will have the value x^2 over [0,10] and f[x] elsewhere. You can look at the documentation for /; (also called Condition) and see how that is define.

    Another way is

    f[x_]:=If[0<=x<=10,x^2]