0
$\begingroup$

This is some code: The first plot (with the f[x,3]) gives me a graph. The second plot (with the 'numerical' form of f[x,3] given) does give me a plot

How can I fix this so that the first line gives me a plot?

f[x_, Nmax_] := 
 Sum[a[n] Cos[24.30538589` n x], {n, 1, Nmax}] + 
  Sum[b[n] Sin[24.30538589` n x], {n, 1, Nmax}] + a[0]/2;


f[x, 3]
(*-0.000663106 - 0.0216591 Cos[24.3054 x] - 0.00475935 Cos[48.6108 x] - 
 0.00889405 Cos[72.9162 x] + 0.0129294 Sin[24.3054 x] - 
 0.0228661 Sin[48.6108 x] + 0.00536402 Sin[72.9162 x]*)

Plot[{f[x, 3]}}, {x, 0.30383`, 0.56234`}]

Plot[{{-0.0006631064575023214` - 
    0.02165905118857869` Cos[24.30538589` x] - 
    0.004759352355590257` Cos[48.61077178` x] - 
    0.008894049060596227` Cos[72.91615767` x] + 
    0.012929362498750253` Sin[24.30538589` x] - 
    0.0228661379875381` Sin[48.61077178` x] + 
    0.005364020800842659` Sin[72.91615767` x]}}, {x, 0.30383`, 
  0.56234`}]

EDIT

enter image description here

EDIT 2

Below is the entire code that I have written: Note that I have given just the inputs and not the outputs, but everything up until the second plot does actually seem to work and give me something which seems right...

data = {{0.30383`, -0.06683`}, {0.30837`, -0.05289`}, {0.3129`, \
-0.01927`}, {0.31744`, 0.02776`}, {0.32197`, 0.07177`}, {0.32651`, 
   0.08597`}, {0.33104`, 0.06693`}, {0.33558`, 0.03273`}, {0.34011`, 
   0.01049`}, {0.34465`, -0.00002`}, {0.34918`, 0.00341`}, {0.35372`, 
   0.01329`}, {0.35825`, 0.03038`}, {0.36279`, 0.04189`}, {0.36732`, 
   0.04932`}, {0.37186`, 0.05579`}, {0.37639`, 0.06438`}, {0.38093`, 
   0.05812`}, {0.38546`, 0.04418`}, {0.39`, 0.03288`}, {0.39454`, 
   0.01151`}, {0.39907`, -0.00437`}, {0.40361`, -0.02415`}, \
{0.40814`, -0.04318`}, {0.41268`, -0.04488`}, {0.41721`, -0.02499`}, \
{0.42175`, 0.00254`}, {0.42628`, 
   0.01371`}, {0.43082`, -0.00089`}, {0.43535`, -0.03078`}, \
{0.43989`, -0.06009`}, {0.44442`, -0.06483`}, {0.44896`, -0.04092`}, \
{0.45349`, -0.00142`}, {0.45803`, 0.03525`}, {0.46256`, 
   0.06063`}, {0.4671`, 0.05745`}, {0.47163`, 
   0.02872`}, {0.47617`, -0.01207`}, {0.4807`, -0.03938`}, {0.48524`, \
-0.04251`}, {0.48977`, -0.0247`}, {0.49431`, -0.009`}, {0.49884`, \
-0.00429`}, {0.50338`, -0.00654`}, {0.50791`, -0.01283`}, {0.51245`, \
-0.02396`}, {0.51698`, -0.04457`}, {0.52152`, -0.0575`}, {0.52605`, \
-0.06183`}, {0.53059`, -0.04522`}, {0.53512`, -0.01328`}, {0.53966`, 
   0.01694`}, {0.5442`, 0.0262`}, {0.54873`, 
   0.00785`}, {0.55327`, -0.03305`}, {0.5578`, -0.07145`}, {0.56234`, \
-0.07393`}}
ListPlot[data, Joined -> True, Epilog -> {Black, Point[data]}]
ListPlot[data, Joined -> True]
nn = Length@data
fun = Interpolation[data]
{x1, x2} = fun[[1, 1]]
Plot[fun[x], {x, x1, x2}, Epilog -> {Black, Point[data]}]
NIntegrate[fun[x] Cos[x], {x, x1, x2}]
a[n_?NumericQ] := 
 NIntegrate[
  7.7366446172295085*fun[x]*Cos[7.7366446172295085*Pi*n x], {x, x1, 
   x2}]
7.7366446172295085*Integrate[fun[x] Cos[24.30538589 n x], {x, x1, x2}]
b[n_?NumericQ] := 
 NIntegrate[
  7.7366446172295085*fun[x]*Sin[7.7366446172295085*Pi*n x], {x, x1, 
   x2}]
Sum[a[n] Cos[24.30538589` n x], {n, 1, 3}] + 
 Sum[b[n] Sin[24.30538589` n x], {n, 1, 3}] + a[0]/2
f[x_, Nmax_] := 
 Sum[a[n] Cos[24.30538589` n x], {n, 1, Nmax}] + 
  Sum[b[n] Sin[24.30538589` n x], {n, 1, Nmax}] + a[0]/2
f[x, 3]
Plot[{{-0.0006631064575023214` - 
    0.02165905118857869` Cos[24.30538589` x] - 
    0.004759352355590257` Cos[48.61077178` x] - 
    0.008894049060596227` Cos[72.91615767` x] + 
    0.012929362498750253` Sin[24.30538589` x] - 
    0.0228661379875381` Sin[48.61077178` x] + 
    0.005364020800842659` Sin[72.91615767` x]}}, {x, 0.30383`, 
  0.56234`}]
Plot[f[x, 3], {x, 0.3083, 0.56234}]
$\endgroup$
11
  • $\begingroup$ What are a and b? $\endgroup$
    – MelaGo
    Jun 30 at 17:41
  • $\begingroup$ @MelaGo - a[n] and b[n] are Fourier coefficients. $\endgroup$
    – Rushi
    Jun 30 at 18:02
  • $\begingroup$ You have an extra curly brace in the first Plot that you need to remove. Actually, even Plot[f[x,3], {x, 0.3083, 0.56234}] should be fine. $\endgroup$
    – MassDefect
    Jun 30 at 18:19
  • $\begingroup$ @MassDefect - that's what I thought! But if you look at the edit above, I have added a screenshot - this still gives me no plot...Why!? $\endgroup$
    – Rushi
    Jun 30 at 18:37
  • 2
    $\begingroup$ Now can you provide how you define $a$ and $b$? I suspect that's where the issue will arise. In general, please provide a minimal working example, i.e. we can copy and paste your code into our own Mathematica and reproduce your issue. $\endgroup$
    – bRost03
    Jun 30 at 18:38
5
$\begingroup$

You are doing way! too much work in your code. The way it is set up, every time you evaluate f it reevaluates a and b which makes your code super slow. I suspect this is your issue, Mathematica will eventually produce the graph you desire - but it takes so long you say "it gives me no plot". I gave up on letting your code run after a minute or so.

Let's try to make the code a bit more efficient. You can tell Mathematica to store the expressions for a[n] and b[n] by doing the following (I named your constants because I hate to see wild numbers floating around in code)

c1 = 7.7366446172295085;
c2 = 24.30538589;
a[n_?NumericQ] := a[n] = NIntegrate[c1*fun[x]*Cos[c1*Pi*n x], {x, x1, x2}]
b[n_?NumericQ] := b[n] = NIntegrate[c1*fun[x]*Sin[c1*Pi*n x], {x, x1, x2}]
f[x_, Nmax_] := Sum[a[n] Cos[c2 n x], {n, 1, Nmax}] + 
     Sum[b[n] Sin[c2 n x], {n, 1, Nmax}] + a[0]/2

Notice the construction a[n_]:=a[n]=...? This tells Mathematica that when e.g. a[3] is calculated the first time - store that result so next time a[3] is used it just returns the stored value instead of recomputing it. Making this change

Plot[f[x, 3], {x, 0.3083, 0.56234}]

runs in a snap.

EDIT

OP said she wanted to make a Manipulate where Nmax varied. In this case I recommend directly storing f as well as a and b to avoid redoing the Sums. This runs very quickly on my machine

f[Nmax_] := f[Nmax] = Sum[a[n] Cos[c2 n #], {n, 1, Nmax}] + 
     Sum[b[n] Sin[c2 n #], {n, 1, Nmax}] + a[0]/2 &;
Table[f[M][1], {M, 50}] (* run to store everything before trying to plot *)
Manipulate[Plot[f[M][x], {x, 0.3083, 0.56234}], {M, 1, 50, 1}]
$\endgroup$
4
  • $\begingroup$ You are a genius @bRost03! And if I change the value I for Nmax e.g. I change it to 4 then 5 then 6 etc will I need to add any code? $\endgroup$
    – Rushi
    Jun 30 at 19:05
  • $\begingroup$ youtube.com/watch?v=VF1tOcEVGyU I essentially want to do something like this and have a slider like he does at the end $\endgroup$
    – Rushi
    Jun 30 at 19:05
  • $\begingroup$ @Rushi no, this should work fine for any Nmax. If Nmax gets very large - there are likely more optimizations that can be done. $\endgroup$
    – bRost03
    Jun 30 at 19:07
  • $\begingroup$ No I don't intend anything too large. Actually maybe 50 is quite large? $\endgroup$
    – Rushi
    Jun 30 at 19:08

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.