Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : creating an array, weighed by sine function

I can create an array of 100 evenly spaced numbers from 0 to 30 by doing -

theta = linspace(0,30,100)

Is it possible to get an array of 100 numbers from 0 to 30, which is not evenly spaced, but weighed by their sine function??

EXPLANATION: Here is a picture to give you a vague idea of what I want. The theta that I have defined, selects the points evenly (like the first picture). I want the array to be weighed with their sine function. As the numbers increase from 0 to 30, their sine(value) increases, so in my array I want to pick the higher numbers more frequently (like the second picture) -

enter image description here

like image 348
Panchi Avatar asked Jul 19 '26 07:07

Panchi


1 Answers

I think I've got your solution. It takes an originally linearly spaced array and takes the sine of it, it then returns the sines scaled to have the same end as the linear array.

import numpy as np
import matplotlib.pyplot as plt

def sinespace(m=90, n=10):
    x = np.linspace(0, m, n)
    s = np.sin(np.deg2rad(x))
    return (s/s[-1])*m

s = sinespace()
print(s)

plt.plot(s, np.ones_like(s), 'ro')
plt.show()

Note that, for a max of 30, the spacing is pretty similar to linearly spaced. This is because theta is linear with sin(theta) for small theta (and when theta is in radians, theta ~= sin(theta)). To show the "sinespacing" I've plotted it up to 90 degrees to make it more obvious.

enter image description here

like image 63
Ffisegydd Avatar answered Jul 21 '26 21:07

Ffisegydd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!