5

How we can write the following Formula in Wolfram:

The Fourier Series

$$ f(x)=f(x+2\pi), f(x) =\left\{ \begin{array}{rcr} 1 & & -\pi <x<0 \\ \sin x & & 0<x<\pi \\ \end{array} \right. $$

be like as:

$$ f(x)=\frac{a_0}{2}+\Sigma_{n=1}^{\infty} (a_n \cos nx+b_n \sin nx) $$

To calculate the following Coefficient (the final solution is mentioned here)?

$$a_n=0,n=2k+1,b_n=0,n=2k$$

i.e. Is there anyway to wrote above formula with Wolfram Site in free, Or using Mathematia for finding coefficient?

  • Thanks J.M, is there any related tag for my questions? – Michle Niaye Jul 24 '16 at 12:55
  • You can query WolframAlpha in Mathematica by starting a new cell with = – Feyre Jul 24 '16 at 13:17
  • any calculus textbook. (seems off topic here) – george2079 Jul 24 '16 at 13:18
  • I'm sorry, why angry :) thanks, I need to do it with Wolfram Site. thanks @C.E. – Michle Niaye Jul 24 '16 at 13:57
  • May be there is some confusion on Wolfram here. There are three main ways to use Wolfram language: Wolfram Alpha, Wolfram-online, and Wolfram Mathematica. Wolfram Mathematica is what meant by notebook. You run Wolfram Mathematica on the desktop. You can also access Wolfram alpha from Wolfram Mathematica using == method. This way Wolfram language commands are send to Wolfram alpha from Wolfram Mathematica and the answer is send back to the notebook. Most folks here use Wolfram Mathematica. I do not know if Wolfram-online can call Wolfram alpha as well. I myself only use Wolfram Mathematica. – Nasser Jul 24 '16 at 14:12
  • @Nasser thanks now I got it, so I want to used one tools that give me step by step solution to get above coefficient ? – Michle Niaye Jul 24 '16 at 14:14
  • Here's how you can access the Wolfram Language for free without an account. – Greg Hurst Jul 25 '16 at 14:38

1 Answers1

8

I do not use the Wolfram Language at Wolfram Alpha since the syntax is a little different and I have access to Wolfram Mathematica which I prefer to Wolfram Alpha.

If you have Wolfram Mathematica, then you use one of the Wolfram language commands, called FourierCoefficient to generate $a_n$ and $b_n$ as follows. (You can try these commands at Wolfram Alpha, but I do not know if they will work as is)

ClearAll[f, x, n];
T0 = 2 Pi; (*period*)
f[x_] := Piecewise[{{1, -Pi < x <= 0}, {Sin[x], 0 <= x <= Pi}}]
Plot[f[x], {x, -T0/2, T0/2}, Exclusions -> None]

Mathematica graphics

nTerms = 10;
c = Table[FourierCoefficient[f[x], x, n, FourierParameters -> {1, 1}], {n, 0, 
    nTerms}];
b = Table[I*(c[[n]] - Conjugate@c[[n]]), {n, 2, nTerms}];
a = Table[(c[[n]] + Conjugate@c[[n]]), {n, 2, nTerms}];
Grid[{{Grid[Join[{{"n", "a(n)"}}, Table[{n, a[[n]]}, {n, 1, Length@a}]], 
    Frame -> All],
   Grid[Join[{{"n", "b(n)"}}, Table[{n, b[[n]]}, {n, 1, Length@a}]], 
    Frame -> All]}}]

Mathematica graphics

And now you can plot the Fourier Series approximation

fapprox[x_] := (c[[1]] + Sum[a[[n]] Cos[n x], {n, 1, Length@a}] + 
   Sum[b[[n]] Sin[n x], {n, 1, Length@b}])
Plot[{f[x], fapprox[x]}, {x, -T0/2, T0/2}, Evaluated -> True,PlotRange -> All]

Mathematica graphics

By adding more terms, the approximation will improve. This is for 30 terms:

Mathematica graphics

The above uses the standard conversion from complex fourier coefficients to the non-complex ones given by

$$ \begin{align} a_0 &= c_0\\ b_n &= i(c_n - c_n^\ast)\\ a_n &= c_n + c_n^\ast \end{align} $$

In above, $c_n^\ast$ is complex conjugate.

The command FourierCoefficient generates $c_n$ and the above converts them standard $a_n,b_n$.

If you prefer to do this by hand, then you can use the definitions of $a_n$ and $b_n$

T0 = 2 Pi;
f[x_] := Piecewise[{{1, -Pi < x <= 0}, {Sin[x], 0 <= x <= Pi}}]
a0 = 1/(T0/2) Integrate[f[x], {x, -T0/2, T0/2}]
an = 1/(T0/2) Integrate[f[x] Cos[n x], {x, -T0/2, T0/2}];
an = Assuming[n > 0 && Element[n, Integers], Simplify[an]]
bn = 1/(T0/2) Integrate[f[x] Sin[n x], {x, -T0/2, T0/2}]

etc...

Mathematica graphics

But it is better to use the FourierCoefficient command to eliminate making mistakes.


Comment asked to show $b_1$ by hand to verify Mathematica is correct.

$$\begin{align*} b_{n} & =\frac{1}{\pi}\int_{-\pi}^{\pi}f\left( x\right) \sin\left( nx\right) dx\\ & =\frac{1}{\pi}\left( \int_{-\pi}^{0}\sin\left( nx\right) dx+\int_{0}% ^{\pi}\sin\left( x\right) \sin\left( nx\right) dx\right) \\ & =\frac{1}{\pi}\left( I_{1}+I_{2}\right) \end{align*} $$

Let us do $I_{1}$ first

$$\begin{align*} \int_{-\pi}^{0}\sin\left( nx\right) dx & =\frac{-1}{n}\left[ \cos\left( nx\right) \right] _{-\pi}^{0}\\ & =\frac{-1}{n}\left[ \cos\left( 0\right) -\cos\left( -n\pi\right) \right] \\ & =\frac{-1}{n}\left[ 1-\cos\left( n\pi\right) \right] \\ & =\frac{\cos\left( n\pi\right) -1}{n}\\ & =\frac{-1^{n}-1}{n} \end{align*} $$

Now we do $I_{2}=\int_{0}^{\pi}\sin\left( x\right) \sin\left( nx\right) dx$. Using $\sin u\sin v=\frac{1}{2}\left( \cos\left( u-v\right) -\cos\left( u+v\right) \right) $ the integrand becomes

$$ \begin{align*} I_{2} & =\frac{1}{2}\int_{0}^{\pi}\cos\left( x-nx\right) -\cos\left( x+nx\right) dx\\ & =\frac{1}{2}\left( \int_{0}^{\pi}\cos\left( \left( 1-n\right) x\right) dx-\int_{0}^{\pi}\cos\left( \left( 1+n\right) x\right) dx\right) \\ & =\frac{1}{2}\left( \frac{\sin\left( \left( 1-n\right) x\right) }{\left( 1-n\right) }-\frac{\sin\left( \left( n+1\right) x\right) } {n+1}\right) _{0}^{\pi}\\ & =\frac{1}{2}\left( \frac{\sin\left( \left( n-1\right) \pi\right) } {n-1}-\frac{\sin\left( \left( n+1\right) \pi\right) }{n+1}\right) \end{align*} $$

Hence

$$ \begin{align*} b_{n} & =\frac{1}{\pi}\left( I_{1}+I_{2}\right) \\ & =\frac{1}{\pi}\left( \frac{-1^{n}-1}{n}+\frac{1}{2}\left( \frac {\sin\left( \left( n-1\right) \pi\right) }{n-1}-\frac{\sin\left( \left( n+1\right) \pi\right) }{n+1}\right) \right) \end{align*} $$

For integer $n\geq1$, the term $\frac{\sin\left( \left( n+1\right) \pi\right) }{n+1}$ always zero, therefore

$$ b_{n}=\frac{1}{\pi}\left( \frac{-1^{n}-1}{n}+\frac{1}{2}\left( \frac {\sin\left( \left( n-1\right) \pi\right) }{n-1}\right) \right) $$

For $n=1$, and since denominator becomes zero at $n=1$, must take the limit

$$ \begin{align*} b_{1} & =\frac{1}{\pi}\left( \frac{-2}{1}+\frac{1}{2}\left( \lim _{n\rightarrow1}\frac{\sin\left( \left( n-1\right) \pi\right) } {n-1}\right) \right) \end{align*} $$

Using L'Hopital

$$ \lim_{n\rightarrow1}\frac{\sin\left( \left( n-1\right) \pi\right) } {n-1}=\lim_{n\rightarrow1}\frac{\frac{d}{dn}\sin\left( \left( n-1\right) \pi\right) }{\frac{d}{dn}\left( n-1\right) }=\lim_{n\rightarrow1}\frac {\pi\cos\left( \left( n-1\right) \pi\right) }{1}=\frac{\pi\cos\left( 0\right) }{1}=\pi $$

Hence $$ \begin{align*} b_{1} & =\frac{1}{\pi}\left( -2+\frac{1}{2}\pi\right) \\ & =\frac{1}{\pi}\left( \frac{-4+\pi}{2}\right) \\ & =\left( \frac{-4+\pi}{2\pi}\right) \end{align*} $$

Which is the result given by Mathematica above.

For $n>1$ we see that $b_{n}$ simpifies to

$$ \frac{1}{\pi}\left( \frac{-1^{n}-1}{n}\right) $$

Since the second term is zero. Hence for $n=2$, $b_{2}=0$ and for $n=3$, $b_{3}=\frac{1}{\pi}\left( \frac{-1^{3}-1}{3}\right) =\frac{1}{\pi}\left( \frac{-2}{3}\right) $ and so on....

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • It's very nice I couldn't vote up because of my points under 15, but very nice – Michle Niaye Jul 24 '16 at 19:25
  • I ran into a question, is it possible with this software to show the step to step integrals that reach to this answer for $a_n$ ,$b_n$? – Michle Niaye Jul 24 '16 at 19:39
  • 1
    @MichleNiaye if you are using Wolfram Alpha, it has a PRO version (not free, $5 per month) which is supposed to show step by step solutions. I do not know if it will show step-by-step for what you are looking for. For Wolfram Mathematica, you can look at get-a-step-by-step-evaluation-in-mathematica and step-by-step-definite-integration and ... – Nasser Jul 24 '16 at 20:48
  • 1
  • Thanks very useful. one remains question, what is the last formula for $a_n$ and $b_n$ in your answer? thanks so much – Michle Niaye Jul 24 '16 at 21:53
  • Sorry, I used this $b_{n} = \frac{1}{\pi} (\int\limits_{-\pi}^{0}-1sin(nx)dx + \int\limits_{0}^{\pi}sin(x)sin(nx) dx)=\frac{1}{\pi} (\frac{cos(nx)}{n}|_{-\pi}^{0} + \frac{\pi}{2}) = \frac{1}{\pi} (\frac{1-(-1)^n}{n}+\frac{\pi}{2}) $ and for $n=1$ I get $b_1=\frac{4+\pi}{2\pi}$ contradict in "-" with your table !!! would you please clarify me – Michle Niaye Jul 24 '16 at 23:34
  • 1
    @MichleNiaye your integration is not correct. I do not see how $\int_0^\pi \sin(x) \sin(nx) ,dx$ gives you $\frac{\pi}{2}$. You can obtain $c_n$ in Mathematica using command c = FourierCoefficient[f[x], x, n, FourierParameters -> {1, 1}] and now you can find $a_n,b_n$ using those conversion formula. For example c1 = Limit[c, n -> 1];b1 = I*(c1 - Conjugate[c1]) gives (-4 + Pi)/(2 Pi) which is what shown above. It is easier to use complex Fourier series and then convert, since less chance of making errors in tricky integration. Mathematica graphics – Nasser Jul 25 '16 at 00:42
  • 1
    ... be careful, do not plug in numerical n values in the integral before doing the integration. sometimes this does not work. Correct way is to leave n as symbolic in the integral, do the integration, then afterwords, evaluate for different n values. Either way, I am sure Mathematica FourierCoefficient result is correct. If you have math questions about the integration itself, it will be better to ask this at Math forum or in chat. – Nasser Jul 25 '16 at 00:49
  • I know wasting your valuable time, but would you please show me the calculation of $b_n$ with hand? I means the correct of my solution? – Michle Niaye Jul 25 '16 at 09:13
  • 1
    @MichleNiaye added $b_1$ calculations. Same for other $b_n$ values. – Nasser Jul 25 '16 at 14:01
  • thanks very useful. I Love persons that love to learn others, I study it now... – Michle Niaye Jul 25 '16 at 14:04
  • Sorry with your new edit I can sure $b_{n} = \frac{1}{\pi} (\int\limits_{-\pi}^{0}-1sin(nx)dx + \int\limits_{0}^{\pi}sin(x)sin(nx) dx)=\frac{1}{\pi} (\frac{cos(nx)}{n}|_{-\pi}^{0} + \frac{\pi}{2}) = \frac{1}{\pi} (\frac{1-(-1)^n}{n}+\frac{\pi}{2}) $ is wrong, am I Right? – Michle Niaye Jul 25 '16 at 14:45
  • 1
    @MichleNiaye the result you give above, at $n=1$ do not give same result as shown. It gives + term not -. So it is not correct. One way to check, is to use your $b_1$ value and plot the approximation and see if it works well or not. This is most normal way to check if the Fourier coefficient are correct or not. – Nasser Jul 25 '16 at 17:51