Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to define a function, which has very different behaviour in different regions. There are about 13 different regions. A sample of my function is the foloowing table:

enter image description here

I want to define it as a function of x, and I demand that for example for 0<=x<=1221.5, f[x_]:=13.088-8.84*x^2, and for 1221.5<=x<=3480, f[x_]:=12.58-1.26*x-3.64*x^2-5.53*x^3 and .... .

How can I do it in a wise way in Mathematica?

share|improve this question
3  
Take a look at Piecewise. –  VLC Mar 19 '13 at 18:44
    
@VLC, Thanks for your fast reply. You meen for example f[x_]:=Piecewise[{{x^2, x < 0}, {x, x > 0}}] ? –  ZKT Mar 19 '13 at 18:46
    
Indeed, that's how you can use it. –  VLC Mar 19 '13 at 18:48
    
@VLC Thanks a lot again. I love this forum, my problem got solved just 1 minute after posting my question in here! :) –  ZKT Mar 19 '13 at 18:51
add comment

closed as too localized by Artes, m_goldberg, Jens, Oleksandr R., rm -rf Mar 21 '13 at 20:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 4 down vote accepted

Your function is double valued at x=1221.5 but if you are happy to chose one of the limits to be strictly greater than (or less than), then you can define your function as:

f[x_]=Piecewise[{{13.088-8.84x^2,0<=x<1221.5},{12.58-1.26x-3.64x^2-5.53x^3,1221.5<=x<=3480}}]
share|improve this answer
    
If you're defining a function, it is important to use the SetDelayed command: f[x_]:= Piecewise[(* the rest *)] –  zentient Mar 20 '13 at 7:05
    
@zentient why is that? I don't see that it is necessary in this case. –  Jonathan Shock Mar 20 '13 at 7:44
    
If this function stands alone, and you don't want to do anything else with f[x_] or x elsewhere, then it appears to produce the same result. However, among other things this may cause scoping issues. The difference can produce unexpected results elsewhere in your code. SetDelayed,f[x_] := Piecewise[(* the rest *)], will keep all x's in the RHS local (they stay green). If you use Set, f[x_] = Piecewise[(* the rest *)], then RHS x's are global (they're blue) and may conflict with your use of x elsewhere. See Documentation > SetDelayed > Properties > ...scope –  zentient Mar 20 '13 at 9:03
    
@zentient, thanks, I'd not thought about it like that. –  Jonathan Shock Mar 20 '13 at 9:24
1  
Try executing x = 1. before your definition using =, and see what happens. :) That is why you need :=. –  J. M. Mar 23 '13 at 16:17
add comment

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