ECE3340 Numerical Methods

Class blog 2/14

ECE 3340 - Han Q. Le (c)

Questions from the class and things for next week.

1. Questions

1.1  How did you figure out the numbers in the array coef that will ...create the heart-shape?

Start from drawing the heart shape with an analytic formula (see CW)

In[1]:=

Class_blog_2_14_v2_1.gif

In[3]:=

Class_blog_2_14_v2_2.png

Out[3]=

Class_blog_2_14_v2_3.gif

Then, the coefficients are simply obtained by the Fourier theorem:         
    Class_blog_2_14_v2_4.png
where Λ is the period.

The code was given explicitly:

In[8]:=

Class_blog_2_14_v2_5.gif

Out[10]=

Class_blog_2_14_v2_6.png

The above are the coefficients (trimmed to 4 digits) and given in the classwork assignment.

1. 2 How did you know that using Cos function will create the heart-shape, instead of using Sin function, or Tan function, or Sec function?

ONLY SIN or COS functions form the complete ORTHOGONAL basis for the Fourier theorem. TAN and SEC are NOT the basis functions (see more in Section 2). This is critical.

Why only using Cos and not Sine?

Fourier theorem states:
  Class_blog_2_14_v2_7.gif

Then, take the complex conjugate:
Class_blog_2_14_v2_8.gif
Since f[x] is real:            Class_blog_2_14_v2_9.png    
Hence:                                            Class_blog_2_14_v2_10.png

Thus we can add pair-wise:
                                Class_blog_2_14_v2_11.png

This is the reason we write:
                                               Re[ coef . fExpn ] in the code.

Special case: Even or symmetric function:        f[-x] = f[x]
then:                                                         Class_blog_2_14_v2_12.png
Hence:           Class_blog_2_14_v2_13.png
Thus, we can use only Cos and no need for Sine.



The other special case: Odd or anti-symmetric function:   f[-x] = -f[x]
Then:                                                Class_blog_2_14_v2_14.png
It follows:
                 Class_blog_2_14_v2_15.gif
Since:                                             Class_blog_2_14_v2_16.png
Class_blog_2_14_v2_17.png must be purely imaginary:             Class_blog_2_14_v2_18.png
and the product         Class_blog_2_14_v2_19.png  is real as expected.

Example of this case of odd function was when we did in the class for an odd function:

In[35]:=

Class_blog_2_14_v2_20.png

Out[35]=

Class_blog_2_14_v2_21.png

Substitute

In[36]:=

Class_blog_2_14_v2_22.png

Substitute again:

In[37]:=

Class_blog_2_14_v2_23.png

Out[37]=

Class_blog_2_14_v2_24.png

                      Class_blog_2_14_v2_25.png   is a purely imaginary quantity.

The answer: because heart-shape function is an even function, hence, we can use only Cos functions, and they are sufficient. But never ever think of Tan or Sec as the basis functions for the Fourier series - please see Section 2.

1.3 How did you know that taking the dot product of coef and fcosn will create 2 arrays that when you plot, you will get the heart-shape?

See answer in 1.1 above. The coefficients were taken by the formula for Fourier series coefficients, hence what we do is simply reconstruct the original, up to the frequency that we choose (32 in this case).

Here is something to look at again to understand:

To draw a penguin, we need 12 single-value functions (12 drawing lines) as shown below. Beside 12 functions, we also need two constant lines for coloring. Execute to understand:

In[11]:=

Class_blog_2_14_v2_26.gif

This is the plot of penguin function:

In[18]:=

Class_blog_2_14_v2_27.gif

Out[20]=

Class_blog_2_14_v2_28.gif

We can get the Fourier coefficients by simply apply:
      Class_blog_2_14_v2_29.png

These lines of code do it:
imgFourier=fouriergraphics2[{funcname,σ, styleparm},nchan,nintv];

Here is the explanation how it is done inside Block function  fouriergraphics2

Class_blog_2_14_v2_30.gif

Read through the code below to understand the steps involved.

In[25]:=

Class_blog_2_14_v2_31.png

The output of the above function is to reconstruct - or to do a Fourier synthesis of the function that we input, which, in this case, is penguin function.

In[30]:=

Class_blog_2_14_v2_32.gif

Out[33]=

Class_blog_2_14_v2_33.gif Class_blog_2_14_v2_34.gif

Out[34]=

time taken: 16.024110.656318859285346 sec.

If we use only 3 Fourier components: m=1, 2, 3, this is how it looks:

In[40]:=

Class_blog_2_14_v2_35.gif

Out[43]=

Class_blog_2_14_v2_36.gif Class_blog_2_14_v2_37.gif

Out[44]=

time taken: 1.234696.5431048789768385 sec.

If we use only 4 Fourier component: m=1, 2, 3, 4, this is how it looks:

In[35]:=

Class_blog_2_14_v2_38.gif

Out[38]=

Class_blog_2_14_v2_39.gif Class_blog_2_14_v2_40.gif

Out[39]=

time taken: 1.523922.634507783521069 sec.

1.4 For example, if I want to plot a chair-shape, where should I begin with? How can I figure out the method?

Write a multi-value function to describe the chair the same way as penguin function describes a penguin, then do this: change funcname to chair with appropriate filling color for the chair.

Class_blog_2_14_v2_41.png

Class_blog_2_14_v2_42.gif

That’s it.

Here, this is a simple example of the half-moon bridge:

In[45]:=

Class_blog_2_14_v2_43.png

This is how it looks:

In[48]:=

Class_blog_2_14_v2_44.gif

Out[49]=

Class_blog_2_14_v2_45.gif

Now, see this:

Class_blog_2_14_v2_46.gif

Out[63]=

Class_blog_2_14_v2_47.gif Class_blog_2_14_v2_48.gif

Have fun! I suggest drawing a recliner. But here are more interesting artworks that had been tried by other students in the past. They didn’t look exactly the same. I only recalled the ideas.

Class_blog_2_14_v2_49.gif ,  Class_blog_2_14_v2_50.gif Class_blog_2_14_v2_51.gif Class_blog_2_14_v2_52.gif
Just a few as examples. It was given as HW, but too labor-intensive for most, only a fraction of the class could do it. Triangle function (for pine tree) and Ellipse function (for penguin) were given. Students just combined them such as the witch hat, or ice cream cone.

2. Dot product in Fourier series theorem

2.1 Formal review (if you forgot linear algebra)

In an N-dimensional space with basis vectors:
                           Class_blog_2_14_v2_53.png

Any point (or element) in that space can be represented as a DOT product:
             Class_blog_2_14_v2_54.gif
One can use the transpose format of the above, which is fine.

In Fourier series theorem, the functions:
     Class_blog_2_14_v2_55.png
form a complete orthonormal basis system: (infinite-dimension space) like the u basis vectors above.

For those who care about formal mathematics, the vector space in this case is the space of piece-wise continuous, square-integrable functions, defined in a finite interval. For convenience, we can let that interval be [0, Λ).

Then. the inner product of two vectors (i. e. two functions) f[x] and g[x] is defined to be:
                               Class_blog_2_14_v2_56.png
(Notation bra < |  and ket | > are very useful in physics).

Denoting the function      Class_blog_2_14_v2_57.png
Then, the set    
          Class_blog_2_14_v2_58.png for  Class_blog_2_14_v2_59.png

forms a complete orthonormal basis system:                             
          Class_blog_2_14_v2_60.gif   
The above is equivalent with:     Class_blog_2_14_v2_61.png that are often used for the linear space of  Class_blog_2_14_v2_62.png

Hence, any piece-wise continuous, square-integrable function f[x] in that interval can be represented as:
Class_blog_2_14_v2_63.png
Or:
Class_blog_2_14_v2_64.png

which is (in conjugate-pair-wise arrangement)

Class_blog_2_14_v2_65.gif
For a real-value function, there is no need to add the negative index part, we only need the positive index part and take the real part of the whole expression.

2.2 Formalism vs. numerical implementation with coding

The first commandment (or professional imperative) of numerical methods:
Thou shall do correct calculations.

The proof-of-the-pudding of understanding and practical skill is NOT about reproducing formulas or proofs. It’s about the capability to code to generate correct numerical results.

In our computer coding, it is:

Class_blog_2_14_v2_66.png

Class_blog_2_14_v2_67.png

Hence: outFS=Re[coef . fexpn ] ;
and that is why we use dot product.

Remember: Fourier functions are not only the basis functions of the space of piece-wise continuous, square-integrable functions, of course. We can arbitrarily generate an infinite number of basis systems simply using a linear transformation:

          Class_blog_2_14_v2_68.png

where R transformation martrix must be invertible or has non-zero determinant.

Numerous systems of basis functions are important in physics and engineering. Legendre, Laguerre, Hermite, Chevbyshev... polynomials are all well known. Other functions such as Bessels with Fourier azimuthal Class_blog_2_14_v2_69.png, and Associated Laguerre polynomilas with spherical harmonics are important for differential equations of 2nd order in cylindrical and spherical coordinate.

2.3 True/proof-in-the-pudding skills: correct coding.

2.3.1 Example 1:

If you don’t feel comfortable with formal mathematics above, it is OK, just learn how to express in code correctly for numerical calculation. Follow this recipe:

1- IF THE COEF ARE GIVEN, then:

Class_blog_2_14_v2_70.gif

2- IF YOU WANT TO FIND THE COEF, then:

Class_blog_2_14_v2_71.gif

Example: half circle:

In[32]:=

Class_blog_2_14_v2_72.gif

Out[33]=

Class_blog_2_14_v2_73.gif

In[64]:=

Class_blog_2_14_v2_74.gif

Out[66]=

0.392699 +0. i 0.0711538 +6.07153*10^^-18 i -0.0265478-2.81893*10^^-18 i 0.0147271 -8.67362*10^^-18 i
-0.00965818+8.67362*10^^-19 i 0.00695125 -4.33681*10^^-18 i -0.0053087-5.20417*10^^-18 i 0.00422459 +0. i
-0.00346506-2.60209*10^^-18 i


See the meaningless residuals in Class_blog_2_14_v2_75.png? To clean them up:

In[71]:=

Class_blog_2_14_v2_76.gif

Out[74]=

0.392699 0.0711538 -0.0265478 0.0147271
-0.00965818 0.00695125 -0.0053087 0.00422459
-0.00346506


There is no need to integrate with so much accuracy and precision:

In[75]:=

Class_blog_2_14_v2_77.gif

Out[78]=

0.392699 0.0711535 -0.0265527 0.0147313
-0.00966308 0.00695548 -0.00531361 0.0042288
-0.00346995


FYI, function like half-circle has exact analytic results:

Class_blog_2_14_v2_78.gif

Out[89]=

Class_blog_2_14_v2_79.png

It involves Bessel function J:              Class_blog_2_14_v2_80.png
Bessel functions are known as the Sine, Cos, and Exp of the cylindrical coordinate.

But here, we use pure numerical method to handle any function (not specific like half-circle). To reconstruct, use the recipe above:

In[100]:=

Class_blog_2_14_v2_81.gif

Out[104]=

Class_blog_2_14_v2_82.gif

2.3.2 Example 2:

Remember that a function can be a mapping from R to Class_blog_2_14_v2_83.png, in other words, a scalar can be mapped to a vector in N-dimensional space. Below, is a 3-values function:

Class_blog_2_14_v2_84.gif

Try to apply the above and see what you get. You will learn the important skill of array handling when having to deal with multi-value function.

Use this for the output:

Class_blog_2_14_v2_85.png

Answer (only if you get stuck)

3. Things for next class(es): Fourier from space to time

The objective in the next two lectures is to get to the concept of Fourier transform, and then learn discrete Fourier transform for numerical Fourier analysis. We also make a major transition: we study Fourier theorem for applications with space, now we will study it for applications with time.

From space:Class_blog_2_14_v2_86.gif     

to time:    Class_blog_2_14_v2_87.gif

3.1 Review basic concepts of time periodic signals

Physical concepts of time periodic signals: see app below

Out[2]=

Review the mathematica object we use to describe the above physical signal: Euler’s complex number notation and for circuit, we call it phasor.

Class_blog_2_14_v2_89.gif
Class_blog_2_14_v2_90.gif


Although the signal can be described with harmonic functions sine or cosine in the Real Number space R, it is more fundamental as well as mathematically more efficient to describe periodic phenomena in the Complex space C

Class_blog_2_14_v2_91.gif

3.2 Fourier synthesis: generate a time signal

We will continue to do Fourier synthesis, but not for drawing. The variable in Section 1 above is x, which is for physical space, used for visual rendering of images. This time we will apply Fourier synthesis to time, variable t.

Class_blog_2_14_v2_93.png

Instead of space period Λ, we deal with time period T, and the most important parameter in this case is:
                    frequency                 Class_blog_2_14_v2_94.png  unit: Class_blog_2_14_v2_95.png,     if Class_blog_2_14_v2_96.png , it is Hz
or:                angular frequency    ω=2 π f     unit: Class_blog_2_14_v2_97.png such as radian Hz.                     

Remember that in the complex plane, Class_blog_2_14_v2_98.png is a rotating point (or vector). It is the mathematical model for virtually all wave phenomena in nature. In circuit, we use this math for phasor. Learn more by downloading apps or visit the part on phasor in other courses.

Note also that we drop Class_blog_2_14_v2_99.png: it is a meaningless constant in time-varying signals that we are interested in. Hence, set it as 0.

3.3 Numerical approach: coding a function using Block or Module to generate a time signal

Suppose we wish to generate a time signal, what do we need to know:
1- the frequency: call it freq
2- the amplitude and phase: this is represented as the complex amplitude of a phasor:
                        Class_blog_2_14_v2_100.png
3- the sampling rate: how often do we sample the signal as output? t is continuous, but there is no such a thing as a continuous numerical array, we have to sample time with a finite set of discrete sampling point. Let’s call it srate
4- duration: how long in time is the signal? The signal may be forever, but numerically, there is a finite size of an array of numbers to be generated.

So, below is an example of such a function, we call it signalgen for signal generator:

In[11]:=

Class_blog_2_14_v2_101.png

Example how to use it: we will generate a random signal:

In[26]:=

Class_blog_2_14_v2_102.gif

Out[32]=

Class_blog_2_14_v2_103.gif

End


You are strongly encouraged to go through all the tutorials up to now. Send in questions if need help.







Created with the Wolfram Language