Defining a piecewise function in Mathematica

In summary, the speaker is having trouble using a sinc function in a 2D Shannon interpolation due to compatibility issues with an optimization software. They have defined their own version of sinc and are using it to construct the interpolation function, but are encountering an error. It is discovered that an extra pair of brackets is needed to enclose all of the pieces of the piecewise function.
  • #1
confused_engineer
39
2
TL;DR Summary
I can't define a piecewise function which reads data from a table as #
Hello everyone.

I am trying to do a 2D Shannon interpolation, but I cannot use a sinc because later on this expression goes in an optimization software that doesn't recognize it. I have defined my own version of sinc as:

sincC = Piecewise[{(Sin[Pi* #]/(Pi*(#))), # >= 1}, {1 - (#^2)/6 + (#^4)/120 - (#^6)/5040, # < 1}] & ;

The problem comes when I use the aforementioned sincC to construct the Shannon interpolation function.

shannonIPC[v_, w_] = Total[#3* sincC[(v - #1)/dDelta]*sincC[(w - #2)/dDelta] & @@@interpolatedData]

interpolatedData is a matrix of three columns and dDelta is a constant equal to 10. The error can be seen in the attached image, but it reads as
Piecewise:The first argument {(0.95493 Sin[1.0472 (-1.+v)])/(-1.+v),0.333333 (-1.+v)>=1} of Piecewise is not a list of pairs. and I am clueless about what to do.

Any advice is welcome.
Thanks for reading.
Regards.
 

Attachments

  • physics_forum.png
    physics_forum.png
    16.3 KB · Views: 349
Physics news on Phys.org
  • #2
You need an extra pair of {...} to enclose all of the pieces of your piecewise function
 
  • Like
Likes confused_engineer
  • #3
Dale said:
You need an extra pair of {...} to enclose all of the pieces of your piecewise function

Thank you very much. That was it.
 
  • Like
Likes Dale

What is a piecewise function in Mathematica?

A piecewise function in Mathematica is a function that is defined by different expressions or rules for different intervals or domains of the independent variable. It allows for a more complex and specific representation of a mathematical relationship.

How do I define a piecewise function in Mathematica?

To define a piecewise function in Mathematica, you can use the Piecewise function and specify the different expressions or rules for each domain. For example: f[x_] := Piecewise[{{x^2, x < 0}, {x, x >= 0}}] defines a piecewise function with x^2 for x values less than 0 and x for x values greater than or equal to 0.

Can I use conditions with a piecewise function in Mathematica?

Yes, you can use conditions to specify when each expression or rule should be applied in a piecewise function. These conditions can include mathematical expressions, logical operators, and pattern matching.

How do I evaluate a piecewise function at a specific point in Mathematica?

To evaluate a piecewise function at a specific point, you can simply input the value into the function. For example, using the function defined in the previous question, f[-3] would evaluate to 9, and f[2] would evaluate to 2.

Can I plot a piecewise function in Mathematica?

Yes, you can plot a piecewise function in Mathematica using the Plot function. Make sure to specify a range of values for the independent variable so that all the different expressions or rules are accounted for in the plot.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
248
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
902
  • Calculus and Beyond Homework Help
Replies
4
Views
2K
Back
Top