I am attempting to integrate an altered sine wave. I am not getting a correct integral

Viewed 32

import numpy as np
import scipy.integrate as integrate
import scipy.special as special
import matplotlib.pyplot as plt

omega = 5
omega *= 2*np.pi
time = np.arange(0.0, 1, 0.0001)
amp = np.sin(omega * time)

Here I set upa time and create an omega for the needed sine wave. In this case the needed sine wave is sin(5 * 2 * PI * time elapsed).

Here I plot the sine wave sine where I get the correct results for said sine wave:


hello = plt.plot(amp)
plt.xlabel('time 10^-3')
plt.ylabel('Voltage')
plt.grid(True, which='both')
plt.axhline(y=0, color='k')

plt.show()

Next I attempt to integrate using special.sici which I believe allows you to integrate sin functions although I'm a little dubious on that notion


test = special.sici(amp)

Then I attempt to plot said newly created integrated function and I get a very strange and very wrong result


hello = plt.plot(test)
plt.xlabel('time 10^-3')
plt.ylabel('Voltage')
plt.grid(True, which='both')
plt.axhline(y=0, color='k')

plt.show()

I'm unsure if I am even beginning to go down the correct path of integrating a non standard sine wave (what I mean by non standard sine wave sin(2x),sin(2PIx)....etc) and if this is not the way to do it I would love to be pointed in the correct direction. Most of what I can find online only refers to integrating already provided standard and unaltered sine/cosin waves.

Here is the script unbroken and whole just in case you want to copy and paste it:


omega = 5
omega *= 2*np.pi
time = np.arange(0.0, 1, 0.0001)
amp = np.sin(omega * time)

hello = plt.plot(amp)  # plot avethe sin w
plt.xlabel('time 10^-3')
plt.ylabel('Voltage')
plt.grid(True, which='both')
plt.axhline(y=0, color='k')

plt.show()

test = special.sici(amp)
hello = plt.plot(test)
plt.xlabel('time 10^-3')
plt.ylabel('Voltage')
plt.grid(True, which='both')
plt.axhline(y=0, color='k')

plt.show()

1 Answers

This is not really a "non-standard sine wave"; I would just call it a sine wave that's been scaled horizontally. The integral of sin(omega*t) from 0 to t is just 1/omega*(1-cos(omega*t)) (see WolframAlpha for nicer formatting).

If you want to integrate it numerically, you can do scipy.integrate.cumulative_trapezoid(amp, time, initial=0).

analytic = 1 / omega * (1 - np.cos(omega * time))
numerical = integrate.cumulative_trapezoid(amp, time, initial=0)
plt.plot(time, analytic, label="analytic")
plt.plot(time, numerical, linestyle="dashed", label="numerical")
plt.xlabel("time")
plt.ylabel("integrated voltage")
plt.grid(True, which="both")
plt.legend()
plt.show()

The sine integral is an entirely different function: the integral of sin(t)/t from 0 to t, as stated in the scipy documentation.

Related