Vector operations III

5. Vector operations III#

In addition to applying operations on vectors, we can apply functions to them. Let’s see how this works.

Summary of commands#

In this exercise, we will demonstrate the following:

Applying functions#

Evaluate the function \(y\) given below between \(x = 0\) and \(x = \pi\).

\[ y = e^{x} \sin (2x) + \dfrac{x^2}{3} \]

To do so, first create a vector \(\vec{x}\) containing equally spaced elements in the domain of the function. Then compute the corresponding vector \(\vec{y}\). Each element of \(\vec{y}\) will be equal to the value of the function evaluated at the corresponding elements of \(\vec{x}\).

import numpy as np

x = np.linspace(0, np.pi)   # default is 50 elements, you may want more sometimes
y = np.exp(x) * np.sin(2*x) + x ** 2 / 3
y
array([ 0.        ,  0.13771464,  0.29383853,  0.46718755,  0.65609852,
        0.85839999,  1.07139032,  1.29182422,  1.51590945,  1.73931503,
        1.95719228,  2.1642102 ,  2.35460615,  2.52225297,  2.66074322,
        2.76349114,  2.82385236,  2.83526142,  2.79138623,  2.68629865,
        2.5146595 ,  2.27191606,  1.95450931,  1.56008778,  1.08772428,
        0.538131  , -0.08613175, -0.78045877, -1.53802098, -2.34961072,
       -3.20351786, -4.0854438 , -4.97846034, -5.8630205 , -6.71702818,
       -7.51597311, -8.23313709, -8.83987689, -9.30598788, -9.60015163,
       -9.69046916, -9.54507982, -9.13286404, -8.42422589, -7.39194955,
       -6.01212063, -4.26510143, -2.13654593,  0.38156221,  3.28986813])