Message Boards Message Boards

0
|
3488 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to generate good looking output?

Posted 10 years ago

Hey,

I've been trying to write my own program to calculate the Fourier-Coefficients and so far the program works but has an awful output.

I would like to have ai and bi written next to each output and also have the results for a and b in the same line, except a0 has to standalone or b0 has to be 0.

This is my code so far:

Clear[n, t, f, i, ak, bk] f[t_] := \[Pi]^2 - t^2; ak[f_, n_, t_] := (1/\[Pi]) Integrate[f[t] Cos[n t], {t, -\[Pi], \[Pi]}]; bk[f_, n_, t_] := (1/\[Pi]) Integrate[f[t] Sin[n t], {t, -\[Pi], \[Pi]}] n = 5; For[i = -1, i < n, Print[i.ak[f, i, t]], i++] For[i = 0, i < n, Print[i.bk[f, i, t]], i++]

Can someome help me and make the ouput more plesseant to look at. I have not done something like this in MATHEMATICA before.

Thanks for your help in advance and have a nice day.

POSTED BY: tt5 kk6
5 Replies

Well, you have to make up your mind as to which t to use for the accuracy. One way would be to maximize Abs[f[t]-fn[t]] over all t. You can use FindMaxValue.

POSTED BY: Gianluca Gorni
Posted 10 years ago

That is almost what i mean, but i meant the Approximation for each point. So what i wanted to do was to use:

el2distance[f_, fn_] := Abs[f[t] - fn[t]]]

But MATHEMATICA now gives me an errormessage saying:

Abs[0. +t$322115-2. Sin[t$322115]+1. Sin[2. t$322115]-0.666667 Sin[3. t$322115]+0.5 Sin[4. t$322115]-0.4 Sin[5. t$322115]] should be a real number or a list of non-negative numbers, which has the same length as {\!\(\*

I guess what i have to do is to numerically calculate the value of that expression? But how?

POSTED BY: tt5 kk6

My guess is that you had given t a numeric value, and this interfered with Integrate. You can protect the integration variable by wrapping it into Module:

Clear[ak, bk];
ak[f_, n_] := 
  ak[f, n] = 
   Module[{t}, (1/\[Pi]) Integrate[f[t] Cos[n t], {t, -\[Pi], \[Pi]}]];
bk[f_, n_] := 
  bk[f, n] = 
   Module[{t}, (1/\[Pi]) Integrate[f[t] Sin[n t], {t, -\[Pi], \[Pi]}]];
f[t_] := \[Pi]^2 - t^2;
ak[f, 2]

Also, it may be prudent to use NIntegrate instead of Integrate, unless you know in advance that your integrals have a closed form. As for the FillingStyle, you may first define your measure of accuracy, for example the L2 distance

el2distance[f_, fn_] := 
 Module[{t}, Sqrt@NIntegrate[Abs[f[t] - fn[t]]^2, {t, -Pi, Pi}]]

and then write something like

FillingStyle -> GrayLevel[el2distance[f, fn]]

This may not work straight away, because you probably have to normalize the distance first, as GrayLevel[x] wants x between 0 and 1. Instead of GrayLevel[x], you may prefer something like Blend[{Blue, Red}, x].

POSTED BY: Gianluca Gorni
Posted 10 years ago

Yes exactly like this. Thank you.

Well if i dont make ak and bk depend on t then MATHEMATICA doesn't evalute the Integrals properly for some reason. I think it's because f depends on t and therefore ak and bk do aswell.

I have continued to work on this little Program and decided to plot the functions and the fourier-series.

Is there a way to change the filling into more and more red (maybe starting at blue) the less accurate the approximation is?

This is my code for ploting: (as u can see i dont know what to put next to FillingStyle)

fourierplot[f_, fn_, n_] := Plot[{f[t], fn[t]}, {t, -\[Pi], \[Pi]}
  , Filling -> {1 -> {2}}
  , FillingStyle -> ColorFunction
  , ImageSize -> Medium
  , AxesLabel -> {t, y}
  , PlotLegends -> {f, "Fourierreihe für n=" Text[n]}]
f[t_] = t;
{f5[t_], f10[t_], f20[t_]} = fourierseries[f[t], {5, 10, 20}];
Row[fourierplot[f, f5, 5]
 , fourierplot[f, f10, 10]
 , fourierplot[f, f20, 20]]
POSTED BY: tt5 kk6

Something like this?

TableForm[Table[{i, ak[f, i, t], bk[f, i, t]}, {i, -1, 10}], 
 TableHeadings -> {None, {k, ak, bk}}]

or else

Grid[Table[{i, ak[f, i, t], bk[f, i, t]}, {i, -1, 10}], Frame -> All]

Why do you make ak and bk depend on t, when they don't?

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract