# Return to Question

6 added 7 characters in body
a0 = (1/2529) Integrate[f[x_]Integrate[f[x], {x, 0, 2529}];
ak1 =
Integrate[
(1/2)(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9) Cos[k π x]x/2],
{x, 0, 1265},
Assumptions -> k ∈ Integers];
ak2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Cos[k π x]x/2],
{x, 1266, 2592},
Assumptions -> k ∈ Integers];
bk1 =
Integrate[
(1/2) Sin[k π x]*x/2]*(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9),
{x, 0, 1265},
Assumptions -> k ∈ Integers]];
bk2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Sin[k π x]x/2],
{x, 1266, 2529},
Assumptions -> k ∈ Integers];
ak = FullSimplify[ak1 + ak2, k ∈ Integers]
bk = FullSimplify[bk1 + bk2, k ∈ Integers]

a0 = (1/2529) Integrate[f[x_], {x, 0, 2529}];
ak1 =
Integrate[
(1/2)(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9) Cos[k π x],
{x, 0, 1265},
Assumptions -> k ∈ Integers];
ak2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Cos[k π x],
{x, 1266, 2592},
Assumptions -> k ∈ Integers];
bk1 =
Integrate[
(1/2) Sin[k π x]*(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9),
{x, 0, 1265},
Assumptions -> k ∈ Integers]];
bk2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Sin[k π x],
{x, 1266, 2529},
Assumptions -> k ∈ Integers];
ak = FullSimplify[ak1 + ak2, k ∈ Integers]
bk = FullSimplify[bk1 + bk2, k ∈ Integers]

a0 = (1/2529) Integrate[f[x], {x, 0, 2529}];
ak1 =
Integrate[
(1/2)(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9) Cos[k π x/2],
{x, 0, 1265},
Assumptions -> k ∈ Integers];
ak2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Cos[k π x/2],
{x, 1266, 2592},
Assumptions -> k ∈ Integers];
bk1 =
Integrate[
(1/2) Sin[k π x/2]*(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9),
{x, 0, 1265},
Assumptions -> k ∈ Integers]];
bk2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Sin[k π x/2],
{x, 1266, 2529},
Assumptions -> k ∈ Integers];
ak = FullSimplify[ak1 + ak2, k ∈ Integers]
bk = FullSimplify[bk1 + bk2, k ∈ Integers]

5 Major clean-up

# Problem plotting partial sum of a Fourier Seriesseries

I'm new to using Mathematica. I, and I have somea problem inwith plotting Fourier series partial sumsums. In

In particular, my target is 1) to plot the fourier series of my piecewise function and my function (in an overlay chart), and 2) to compare the genereted trigonometric polynomial with the original function to find point of minimum between them. I

1. to plot the Fourier series of my piecewise function and with that function on a single plot

2. and then to compare the genereted trigonometric polynomial with the original function to find point where they are closest to each other.

I started fromwith this piecewise function (I founded it troughwhich was generated by a previous fitting of my data).

f[x_] :=
Piecewise[
f[x_]:=Piecewise[{{1595.6662770406633 - 4.968000370422044*x968000370422044 x + 0.012672971318651484*x^2012672971318651484 x^2 -
0.00001183377889695339*x^300001183377889695339 x^3 + 3.841543896820609*^-9*x^49 x^4,
Inequality[0, LessEqual,     0 <= x, Less,< 1266.]},
{140884.53677307916 - 397.17060928335155*x17060928335155 x + 0.44179779820265586*x^244179779820265586 x^2 -
0.0002399273183663781*x^30002399273183663781 +x^3
+ 6.376241982891033*^-8*x^48 x^4 -
6.639720201538734*^-12*x^512 x^5, Inequality[1266
1266., LessEqual,<= x, Less,< 2530.]}},
0]


SoBy hand, I calulated by handcalculated the Fourier coefficients (I hope they're correct) in this way:

    a0 = (1/2529)*Integrate[f[x_] Integrate[f[x_], {x, 0, 2529}];
ak1 =
ak1Integrate[
= Integrate[  (1/2)*(1595.67 - 4.968*x968 x + 0.012673*x^2 -012673
x^2 - 0.0000118338*x^30000118338 x^3 + (3.84154*x^484154 x^4)/10^9)*Cos[k*Pi*x] Cos[k π x],
{x, 0,
1265},
Assumptions -> Element[k,k Integers]];∈ Integers];
ak2 =
ak2Integrate[
= Integrate[  (1/2)*(140885. - 397.171*x171 x + 0.441798*x^2 -441798
x^2 - 0.000239927*x^3000239927 x^3 + (6.37624*x^437624 x^4)/10^8 - (6.63972*x^563972 x^5)/10^12)*
Cos[k*Pi*x] Cos[k π x],
{x, 1266, 2592},
Assumptions -> Element[k, Integers]];
k ∈ Integers];
bk1 =
Integrate[
(1/2)*
Sin[k*Pi*x]* Sin[k π x]*(1595.67 - 4.968*x968 x + 0.012673*x^2 -012673
x^2 - 0.0000118338*x^30000118338 x^3 + (3.84154*x^484154 x^4)/10^9),
{x, 0, 1265},
Assumptions -> Element[k,k ∈ Integers]];
bk2 =
Integrate[
(1/2)*(140885. - 397.171*x171 x + 0.441798*x^2 -441798
x^2 - 0.000239927*x^3000239927 x^3 + (6.37624*x^437624 x^4)/10^8 - (6.63972*x^563972 x^5)/10^12)*
Sin[k*Pi*x] Sin[k π x],
{x, 1266, 2529},
Assumptions -> Element[k, Integers]];k
∈ Integers];
ak = FullSimplify[ak1 + ak2, Element[k, Integers]]
k ∈ Integers]
bk = FullSimplify[bk1 + bk2, Element[k,k Integers]]∈ Integers]

   s[n_, x_] := a0/2+(1/2530)*Sum[ak*Cos[ Sum[ak Cos[(k*Pi*xk π x)/2]+k*Sin + k Sin[(k*Pi*xk π x)/2], {k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]

Plot[Evaluate[partialsums], {x, 0, 20}]


But, this the following is the result I see when I try to Plotplot my partial sums and the functionf together in a same range, for example, of 20.

   Plot[{Evaluate[partialsums], f[x_]}, {x, 0, 20}]


In other words, Mathematica doesn't plot the partial sums together with my original function I wouldwant to compare with it with.

I also tried with:

But the problem stil remainsthat doesn't work either. Moreover Moreover the generated function generated by the partialsumspartial sums doesn't seemsseem to follow the curvature of the previous function,f and hisits amplitude. I

I think, I have probably made some mistakes in the calculus of coefficientthe coefficients or in definition of variables, and sountil this is corrected, I can't go on with my analisys. Someone could

Could someone please help me?? Thanks a lot for this.

# Problem plotting partial sum Fourier Series

I'm new using Mathematica. I have some problem in plotting Fourier series partial sum. In particular, my target is 1) to plot the fourier series of my piecewise function and my function (in an overlay chart), and 2) to compare the genereted trigonometric polynomial with the original function to find point of minimum between them. I started from this piecewise function (I founded it trough a previous fitting of data).

       f[x_]:=Piecewise[{{1595.6662770406633 - 4.968000370422044*x + 0.012672971318651484*x^2 - 0.00001183377889695339*x^3 + 3.841543896820609*^-9*x^4,
Inequality[0, LessEqual, x, Less, 1266.]}, {140884.53677307916 - 397.17060928335155*x + 0.44179779820265586*x^2 - 0.0002399273183663781*x^3 +
6.376241982891033*^-8*x^4 - 6.639720201538734*^-12*x^5, Inequality[1266., LessEqual, x, Less, 2530.]}}, 0]


So, I calulated by hand the Fourier coefficients (I hope they're correct) in this way:

    a0 = (1/2529)*Integrate[f[x_], {x, 0,2529}];
ak1 = Integrate[(1/2)*(1595.67 - 4.968*x + 0.012673*x^2 -
0.0000118338*x^3 + (3.84154*x^4)/10^9)*Cos[k*Pi*x], {x, 0,
1265},
Assumptions -> Element[k, Integers]];
ak2 = Integrate[(1/2)*(140885. - 397.171*x + 0.441798*x^2 -
0.000239927*x^3 + (6.37624*x^4)/10^8 - (6.63972*x^5)/10^12)*
Cos[k*Pi*x],
{x, 1266, 2592}, Assumptions -> Element[k, Integers]];
bk1 = Integrate[(1/2)*
Sin[k*Pi*x]*(1595.67 - 4.968*x + 0.012673*x^2 -
0.0000118338*x^3 + (3.84154*x^4)/10^9), {x, 0, 1265},
Assumptions -> Element[k, Integers]];
bk2 = Integrate[(1/2)*(140885. - 397.171*x + 0.441798*x^2 -
0.000239927*x^3 + (6.37624*x^4)/10^8 - (6.63972*x^5)/10^12)*
Sin[k*Pi*x],
{x, 1266, 2529}, Assumptions -> Element[k, Integers]];
ak = FullSimplify[ak1 + ak2, Element[k, Integers]]
bk = FullSimplify[bk1 + bk2, Element[k, Integers]]

   s[n_, x_] := a0/2+(1/2530)*Sum[ak*Cos[(k*Pi*x)/2]+k*Sin[(k*Pi*x)/2],{k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]
Plot[Evaluate[partialsums], {x, 0, 20}]


But, this is the result when I try to Plot my partial sums and the function together in a same range, for example, of 20.

   Plot[{Evaluate[partialsums], f[x_]}, {x, 0, 20}]


In other words, Mathematica doesn't plot the partial sums together with my original function I would to compare with it.

I also tried with:

But the problem stil remains. Moreover the generated function by the partialsums doesn't seems to follow the curvature of the previous function, and his amplitude. I think, I probably made some mistakes in the calculus of coefficient or in definition of variables, and so I can't go on with my analisys. Someone could help me?? Thanks a lot for this.

# Problem plotting partial sum of a Fourier series

I'm new to using Mathematica, and I have a problem with plotting Fourier series partial sums.

In particular, my target is

1. to plot the Fourier series of my piecewise function and with that function on a single plot

2. and then to compare the genereted trigonometric polynomial with the original function to find point where they are closest to each other.

I started with this piecewise function which was generated by a previous fitting of my data.

f[x_] :=
Piecewise[
{{1595.6662770406633 - 4.968000370422044 x + 0.012672971318651484 x^2 -
0.00001183377889695339 x^3 + 3.841543896820609*^-9 x^4,
0 <= x < 1266.},
{140884.53677307916 - 397.17060928335155 x + 0.44179779820265586 x^2 -
0.0002399273183663781 x^3 + 6.376241982891033*^-8 x^4 -
6.639720201538734*^-12 x^5,
1266. <= x < 2530.}},
0]


By hand, I calculated the Fourier coefficients (I hope they're correct) in this way:

a0 = (1/2529) Integrate[f[x_], {x, 0, 2529}];
ak1 =
Integrate[
(1/2)(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9) Cos[k π x],
{x, 0, 1265},
Assumptions -> k ∈ Integers];
ak2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Cos[k π x],
{x, 1266, 2592},
Assumptions -> k ∈ Integers];
bk1 =
Integrate[
(1/2) Sin[k π x]*(1595.67 - 4.968 x + 0.012673 x^2 - 0.0000118338 x^3 + (3.84154 x^4)/10^9),
{x, 0, 1265},
Assumptions -> k ∈ Integers]];
bk2 =
Integrate[
(1/2)(140885. - 397.171 x + 0.441798 x^2 - 0.000239927 x^3 + (6.37624 x^4)/10^8 - (6.63972 x^5)/10^12) Sin[k π x],
{x, 1266, 2529},
Assumptions -> k ∈ Integers];
ak = FullSimplify[ak1 + ak2, k ∈ Integers]
bk = FullSimplify[bk1 + bk2, k ∈ Integers]

s[n_, x_] := a0/2+(1/2530) Sum[ak Cos[(k π x)/2] + k Sin[(k π x)/2], {k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]

Plot[Evaluate[partialsums], {x, 0, 20}]


But the following is the result I see when I try to plot my partial sums and f together in a same range, for example, of 20.

Plot[{Evaluate[partialsums], f[x_]}, {x, 0, 20}]


In other words, Mathematica doesn't plot the partial sums together with my original function I want to compare it with.

I also tried with

But that doesn't work either. Moreover the function generated by the partial sums doesn't seem to follow the curvature of f and its amplitude.

I think I have probably made some mistakes in the calculus of the coefficients or in definition of variables, and until this is corrected, I can't go on with my analisys.

Could someone please help me?

4 added 7 characters in body
       Piecewise[f[x_]:=Piecewise[{{1595.6662770406633 - 4.968000370422044*x + 0.012672971318651484*x^2 - 0.00001183377889695339*x^3 + 3.841543896820609*^-9*x^4,
Inequality[0, LessEqual, x, Less, 1266.]}, {140884.53677307916 - 397.17060928335155*x + 0.44179779820265586*x^2 - 0.0002399273183663781*x^3 +
6.376241982891033*^-8*x^4 - 6.639720201538734*^-12*x^5, Inequality[1266., LessEqual, x, Less, 2530.]}}, 0]

   s[n_, x_] := a0+a0/2+(1/Length[price]2530)*Sum[ak*Cos[(k*Pi*x)/2]+k*Sin[(k*Pi*x)/2],{k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]
Plot[Evaluate[partialsums], {x, 0, 20}]

       Piecewise[{{1595.6662770406633 - 4.968000370422044*x + 0.012672971318651484*x^2 - 0.00001183377889695339*x^3 + 3.841543896820609*^-9*x^4,
Inequality[0, LessEqual, x, Less, 1266.]}, {140884.53677307916 - 397.17060928335155*x + 0.44179779820265586*x^2 - 0.0002399273183663781*x^3 +
6.376241982891033*^-8*x^4 - 6.639720201538734*^-12*x^5, Inequality[1266., LessEqual, x, Less, 2530.]}}, 0]

   s[n_, x_] := a0+(1/Length[price])*Sum[ak*Cos[(k*Pi*x)/2]+k*Sin[(k*Pi*x)/2],{k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]
Plot[Evaluate[partialsums], {x, 0, 20}]

       f[x_]:=Piecewise[{{1595.6662770406633 - 4.968000370422044*x + 0.012672971318651484*x^2 - 0.00001183377889695339*x^3 + 3.841543896820609*^-9*x^4,
Inequality[0, LessEqual, x, Less, 1266.]}, {140884.53677307916 - 397.17060928335155*x + 0.44179779820265586*x^2 - 0.0002399273183663781*x^3 +
6.376241982891033*^-8*x^4 - 6.639720201538734*^-12*x^5, Inequality[1266., LessEqual, x, Less, 2530.]}}, 0]

   s[n_, x_] := a0/2+(1/2530)*Sum[ak*Cos[(k*Pi*x)/2]+k*Sin[(k*Pi*x)/2],{k, 1, n}]

partialsums = Table[s[n, x], {n, 1, 5}]
Plot[Evaluate[partialsums], {x, 0, 20}]

3 added 207 characters in body
2 edited title
1