0
$\begingroup$

I have two main issues. The first concerns Mathematica interpreting the same definition differently based on what I call the function, even though the name never occurs in any other part of the document. Instance:

f2[x_ /; 1 <= x <= 3] := (x + 1) (x - 2) 
Plot[f2[x], {x, -5, 5}]

givesenter image description herebut when I replace f2 with f3, no errors come up. Of course, I'm aware that this has to do with my previous usage of the name f2 (when I tried to compose a periodic function) but all the definitions concerning f2 are long gone from the document. Are such variables being attributed permanent values in this program?

Second issue is the aforementioned periodic function. All my attempts at composing one have been futile. Function is the following:

f3[x_ /; 1 <= x <= 3] := (x + 1) (x - 2) 
f3[x_] := f3[Mod[x, 2]]
Plot[f3[x], {x, -10, 10}]

Which yields the same error as the one above. Interestingly enough, typing in

Plot[f2[x], {x, -10, 10}]

Gives me the correct graph, even though f2 isn't even defined or mentioned anywhere else. The is an obvious relation between the functions which I have failed to observe. I would be grateful for any help.

$\endgroup$
  • $\begingroup$ It's stored in memory. Evaluation -> Quit Kernel -> Local -> Quit. It should be obvious why f3 is giving you recursion error. For instance you can runf4[x_] := f3[Mod[x, 2]], Plot[f4[x], {x, -10, 10}] $\endgroup$ – Feyre Oct 22 '16 at 15:57
  • $\begingroup$ Thanks for the answer! What about the periodic function, how do I construct it without getting the iteration limit? I've also noticed that the error appears for some intervals of x and not for others. $\endgroup$ – Marek Oct 22 '16 at 16:00
1
$\begingroup$

You're overthinking:

f3[x_ /; 1 <= x <= 3] := (x + 1) (x - 2)
Plot[f3[Mod[x + 1, 2] + 1], {x, -10, 10}, 
 Exclusions -> Range[-9, 9, 2]]

enter image description here

$\endgroup$
  • $\begingroup$ But that's not how the function is supposed to look like! It cuts all the values above 0. I've noticed that using the Mod argument within the Plot command sets the maximum f3 value at 0, regardless of the function. Tried that with a piecewise function with values of 1 and -1 and it only showed the -1 ones. $\endgroup$ – Marek Oct 22 '16 at 16:09
  • $\begingroup$ @Marek Then that's because your code is wrong, this is the same as what you'd get if f3[x_] := f3[Mod[x, 2]] Plot[f3[x], {x, -10, 10}] didn't give recursion error. $\endgroup$ – Feyre Oct 22 '16 at 16:13
  • $\begingroup$ I'm glad you made it work, but I really don't understand the method. Also, would it work the same way had I moved the mod outside the plot command? $\endgroup$ – Marek Oct 22 '16 at 16:27
  • $\begingroup$ @Marek, updated my code. You need to think, that you need to rescale every number x to between 1,3 clearly, Mod[x,2] isn't sufficient to do that. $\endgroup$ – Feyre Oct 22 '16 at 16:38
  • $\begingroup$ Thanks, although I'm having a hard time understanding the extra plus after mod and the exclusions concerning the range. $\endgroup$ – Marek Oct 22 '16 at 17:02

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