5
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Thanks J.M, is there any related tag for my questions? $\endgroup$ – Michle Niaye Jul 24 '16 at 12:55
  • $\begingroup$ You can query WolframAlpha in Mathematica by starting a new cell with = $\endgroup$ – Feyre Jul 24 '16 at 13:17
  • $\begingroup$ any calculus textbook. (seems off topic here) $\endgroup$ – george2079 Jul 24 '16 at 13:18
  • $\begingroup$ I'm sorry, why angry :) thanks, I need to do it with Wolfram Site. thanks @C.E. $\endgroup$ – Michle Niaye Jul 24 '16 at 13:57
  • $\begingroup$ 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. $\endgroup$ – Nasser Jul 24 '16 at 14:12
8
$\begingroup$

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....

$\endgroup$
  • 1
    $\begingroup$ @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 ... $\endgroup$ – Nasser Jul 24 '16 at 20:48
  • 1
  • 1
    $\begingroup$ @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 $\endgroup$ – Nasser Jul 25 '16 at 0:42
  • 1
    $\begingroup$ ... 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. $\endgroup$ – Nasser Jul 25 '16 at 0:49
  • 1
    $\begingroup$ @MichleNiaye added $b_1$ calculations. Same for other $b_n$ values. $\endgroup$ – Nasser Jul 25 '16 at 14:01

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.