2
$\begingroup$

I'm trying to make a function that outputs a plot given a particular parameter, whilst using the Mathematica functions Module, Fourier, and Table. This is what I've come up with thus far.

plotit[function_, min_, max_, step_, fourier_] := Module[{data, fdata},
       data = Table[{i, function[i]}, {i, min, max, step}];
       fdata = Fourier[Part[data, All, If[fourier == 0, 1, 2]]];
       If [fourier == 0 , ListPlot[data], 
       ListPlot[Transpose[{Part[data, All, 1], fdata}]]]
     ];

The reason for this is I want to easily be able to plot functions such as Sin, RiemannSiegelZ, ect and easily plot a Fourier of them. This sounds quite specific, I found this question in an online free workbook to help learn Mathematica.

For example I want to input

plotit[Function[x, RiemannSiegelZ[x]^2], 0, 50, .1, 0]

which outputs

%CorrectPlot%

I couldn't figure out how to upload my output into this question, sorry. Anyways I do get a plot of what I want, if I set fourier=0, butt not when fourier=1.

My last working plotit function is

plotit[function_, min_, max_, step_, fourier_] := Module[{data, fdata},
   data = Table[{i, function[i]}, {i, min, max, step}];
   ListPlot[data]
  ];

I get the desired output, of a plot, but there's nothing in there to easily change the outputted plot to be a Fourier transform of the plot.

I've tried

plotit[Function[x, Fourier[RiemannSiegelZ[x]^2]], 0, 50, .1, 1]

But it seems like the Mathematica function 'Fourier' only works on lists, so it cannot be used this way.

My goal is essentially to create a function that plots the Fourier Transform of any input function over a specific range, but also be general enough to just plot the input function over the specific range without creating another function.

$\endgroup$
  • $\begingroup$ If not IF and don't make a whitespace. $\endgroup$ – halirutan Oct 17 '17 at 16:21
  • $\begingroup$ Thank you @halirutan but my final output plot when doing the fourier transform is just a dot. $\endgroup$ – Mike D. Danh Oct 17 '17 at 16:33
  • $\begingroup$ Is it maybe possible, that the fourier transform in general creates compex values and that your plot contains of the only non-complex values? $\endgroup$ – halirutan Oct 17 '17 at 16:38
  • $\begingroup$ Why not figure out how to plot the Fourier transform of some data first, and then try to make a function that can take any old function as input? $\endgroup$ – bill s Oct 17 '17 at 16:40
  • 2
    $\begingroup$ If you take a Fourier transform then the result is in general complex. To be complete you should plot either the real and imaginary parts or the modulus and phase. Thus you should have two Fourier plots for each of your functions. If you use Fourier then you need to start with a list so you also need to define a resolution or a number of points to discretize the function. More details on how Fourer works may be found here. $\endgroup$ – Hugh Oct 17 '17 at 17:44
1
$\begingroup$

Thank you @halirutan it seems like I've formatted my function properly, the fourier transform in general was creating complex numbers, but I had only been plotting its non-complex values. Simply taking fdata = Abs[Fourier[Part[data, All, If[fourier == 0, 1, 2]]]] I was able to get the function to work.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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