[Mathematica] Simple Problem with the plot function

In summary, the conversation discussed the use of Mathematica to plot the fibonacci sequence, specifically looking at the 12th fibonacci number, which was found to be 144. The individual then attempted to plot the two graphs for the formula, but ran into issues with the phih term not being a real-valued function. It was suggested to use DiscretePlot for plotting at integer values of x. The conversation then delved into the nature of the function and its piecewise real-valued properties.
  • #1
PEZenfuego
48
0
I have a formula for the fibonacci sequence (with 1 being the first) and I noticed that the 12th fibonacci number was 144. I thought that was a neat coincidence, so I I headed over to mathematica to see if this (and 1) were the only numbers that had this property. I was almost certain that it was. So I plotted the two graphs, but only the x^2 graphed showed up. Here was my input. My question is about what I did wrong.

phi := (1 + Sqrt[5])/2

phih := (1 - Sqrt[5])/2

f:={(phi^n)-(phih^n)}/{Sqrt[5]}

y:=n^2

Plot[{f, y}, {n, -20, 20}]

Now, this works when I remove the phih term, which works well enough that I can clearly see that only 1 and 12 are solutions. On the other hand, this is not plotting something that it should be plotting...

Anyway, thank you in advance for the help.
 
Physics news on Phys.org
  • #2
The problem is that plot tries to make a continuous plot. It then runs into the problem that phih^x is not a real-valued function for non-integer x, and refuses to plot it altogether. To just plot the values at integer values of x, you can use DiscretePlot:

DiscretePlot[{f, y}, {n, -20, 20, 1}]
 
  • #3
Hypersphere said:
The problem is that plot tries to make a continuous plot. It then runs into the problem that phih^x is not a real-valued function for non-integer x, and refuses to plot it altogether. To just plot the values at integer values of x, you can use DiscretePlot:

DiscretePlot[{f, y}, {n, -20, 20, 1}]

That's really neat and useful. I was so accustomed to just glazing over (1-Sqrt[5])/2, that I forgot that it was negative. If you raise it to the power of 1/2 for example, then the answer is imaginary. That's why it is not a real-valued function, correct? Thank you, sir.
 
  • #4
PEZenfuego said:
That's really neat and useful. I was so accustomed to just glazing over (1-Sqrt[5])/2, that I forgot that it was negative. If you raise it to the power of 1/2 for example, then the answer is imaginary. That's why it is not a real-valued function, correct? Thank you, sir.

You are welcome. And, yeah, if the function has a non-zero imaginary part for at least one value of x, then clearly it's not real-valued on the whole domain (i.e. the plot range). However, Mathematica is able to handle cases where the function is piecewise real-valued, it just doesn't plot the part where the values are comples, see eg.
Plot[(1 + I*HeavisideTheta[x - 5]*HeavisideTheta[6 - x]), {x, 0, 10}]
(I would prefer it to give an error message or a warning, but it doesn't seem to do that.)Your function is a bit worse though as it is only real-valued on integer values of x. The proof just uses the general definition of the power of a number a (negative, complex etc.), which makes use of the complex logarithm:
[itex]a^b=e^{b log a}[/itex].
If a is real and negative, then this simplifies to
[itex]a^b=e^{bLog(|a|)+ib\pi}=|a|^b e^{ib\pi}[/itex],
which is only real for integer values of b.
 
  • #5


It looks like you have a small typo in your code. Instead of using curly braces around the f and y functions, you should use parentheses. The correct code should be:

phi := (1 + Sqrt[5])/2

phih := (1 - Sqrt[5])/2

f := ((phi^n) - (phih^n))/Sqrt[5]

y := n^2

Plot[{f, y}, {n, -20, 20}]

Curly braces are used for lists of expressions in Mathematica, while parentheses are used for grouping expressions or functions. This small change should fix the issue and allow you to plot both functions correctly.

In general, when encountering problems with plotting in Mathematica, it's always a good idea to double check your syntax and make sure you are using the correct symbols and functions. You can also refer to the Mathematica documentation or seek help from the Mathematica community for assistance.
 

1. What is the syntax for using the plot function in Mathematica?

The basic syntax for using the plot function in Mathematica is Plot[expression, {x, xmin, xmax}], where expression is the mathematical function or expression you want to plot, and xmin and xmax are the lower and upper limits for the x-axis, respectively.

2. Can I plot multiple functions in one plot using the plot function?

Yes, you can plot multiple functions in one plot using the plot function by including a list of expressions as the first argument, such as Plot[{f1[x], f2[x], f3[x]}, {x, xmin, xmax}]. This will plot all three functions on the same graph.

3. How do I customize the appearance of my plot in Mathematica?

You can customize the appearance of your plot in Mathematica by using various options in the Plot function, such as changing the color, thickness, or style of the plot lines. You can also add labels, legends, and other annotations to your plot using different options.

4. What are some common errors I may encounter when using the plot function in Mathematica?

Some common errors you may encounter when using the plot function in Mathematica include syntax errors, such as forgetting to include the closing bracket or using incorrect capitalization, and mathematical errors, such as dividing by zero or trying to take the square root of a negative number. It is important to carefully check your code and make sure your mathematical expressions are correct before running the plot function.

5. Can I save my plot as an image or a file in Mathematica?

Yes, you can save your plot as an image or a file in Mathematica by using the Export function. This allows you to save your plot in a variety of formats, such as PNG, JPEG, PDF, or even as a vector graphic. You can also specify the size and resolution of the image when exporting.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
201
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
722
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
661
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
595
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
Back
Top