Integrate a function depending on two arrays - python

Initially, I have two arrays that correspond to the values of x and y in a function, but I don't know that function, I just know that the values of y depend on x. Then, I calculate a function that depends on both arrays.
I need to calculate in python the integral of that last function to obtain the total area under the curve between the first value of x and the last. Any idea of how to do that?
x = [array]
y(x) = [array]
a = 2.839*10**25
b = 4*math.pi
alpha = 0.5
z = 0.003642
def L(x,y,a,b,alpha,z):
return x*((y*b*a)/(1+z)**(1+alpha))

Your function is a function of x (in that given a value of x it spits out a value), so first you should repackage it as such (introduce a function yy which, given x, produces the requisite y), then write LL(x) = L(x, yy[x]), then use scipy.integrate to integrate it.

Related

How to implement linear interpolation in python?

I want to implement a function interpolate(x, y, X_new) that computes the linear interpolation of the unknown function f at a new point x_new. The sample is given in the form of two sequences x and y. Both sequences have the same length, and their elements are numbers. The x sequence contains the points where the function has been sampled, and the y sequence contains the function value at the corresponding point. (without using import statement).
As I understand your question, you want to write some function y = interpolate(x_values, y_values, x), which will give you the y value at some x? The basic idea then follows these steps:
Find the indices of the values in x_values which define an interval containing x. For instance, for x=3 with your example lists, the containing interval would be [x1,x2]=[2.5,3.4], and the indices would be i1=1, i2=2
Calculate the slope on this interval by (y_values[i2]-y_values[i1])/(x_values[i2]-x_values[i1]) (ie dy/dx).
The value at x is now the value at x1 plus the slope multiplied by the distance from x1.
You will additionally need to decide what happens if x is outside the interval of x_values, either it's an error, or you could interpolate "backwards", assuming the slope is the same as the first/last interval.
Did this help, or did you need more specific advice?

Why is the output of linspace and interp1d always the same?

So I was doing my assignment and we are required to use interpolation (linear interpolation) for the same. We have been asked to use the interp1d package from scipy.interpolate and use it to generate new y values given new x values and old coordinates (x1,y1) and (x2,y2).
To get new x coordinates (lets call this x_new) I used np.linspace between (x1,x2) and the new y coordinates (lets call this y_new) I found out using interp1d function on x_new.
However, I also noticed that applying np.linspace on (y1,y2) generates the exact same values of y_new which we got from interp1d on x_new.
Can anyone please explain to me why this is so? And if this is true, is it always true?
And if this is always true why do we at all need to use the interp1d function when we can use the np.linspace in it's place?
Here is the code I wrote:
import scipy.interpolate as ip
import numpy as np
x = [-1.5, 2.23]
y = [0.1, -11]
x_new = np.linspace(start=x[0], stop=x[-1], num=10)
print(x_new)
y_new = np.linspace(start=y[0], stop=y[-1], num=10)
print(y_new)
f = ip.interp1d(x, y)
y_new2 = f(x_new)
print(y_new2) # y_new2 values always the same as y_new
The reason why you stumbled upon this is that you only use two points for an interpolation of a linear function. You have as an input two different x values with corresponding y values. You then ask interp1d to find a linear function f(x)=m*x +b that fits best your input data. As you only have two points as input data, there is an exact solution, because a linear function is exactly defined by two points. To see this: take piece of paper, draw two dots an then think about how many straight lines you can draw to connect these dots.
The linear function that you get from two input points is defined by the parameters m=(y1-y2)/(x1-x2) and b=y1-m*x1, where (x1,y1),(x2,y2) are your two inputs points (or elements in your x and y arrays in your code snippet.
So, now what does np.linspace(start, stop, num,...) do? It gives you num evenly spaced points between start and stop. These points are start, start + delta, ..., end. The step width delta is given by delta=(end-start)/(num - 1). The -1 comes from the fact that you want to include your endpoint. So the nth point in your interval will lie at xn=x1+n*(x2-x1)/(num-1). At what y values will these points end up after we apply our linear function from interp1d? Lets plug it in:
f(xn)=m*xn+b=(y1-y2)/(x1-x2)*(x1+n/(num-1)*(x2-x1)) + y1-(y1-y1)/(x1-x2)*x1. Simplifying this results in f(xn)=(y2-y1)*n/(num - 1) + y1. And this is exactly what you get from np.linspace(y1,y2,num), i.e. f(xn)=yn!
Now, does this always work? No! We made use of the fact that our linear function is defined by the two endpoints of the intervals we use in np.linspace. So this will not work in general. Try to add one more x value and one more y value in your input list and then compare the results.

Implementing minimization method

I have a function in 2 variables x1,x2
f = 3*x1^2 + 4*x2^2 + 5*x1 + 6*x2 + 10
Consider x is a row vector such that x = [x5,x6], where x5,x6 are components of the vector. If the notation is confusing, let us consider x = [x1,x1] but x1,x2 can be any arbitrary components. The same argument holds for y.
Then I want to find a from (x + ay) such that it will minimize the f. a is real constant, x and y are vectors. This is explained above.
If this does not make sense, then let us consider x,y as a 1-dimensional arrays with 2 locations. So, x(1),x(2),y(1),y(2) be their components. Then I want to multiply array y by a symbolic variable a.
For example, x=[4,5], y=[-2,3] then, (x + ay) = (4,5) + a(-2,3) = (4-2a,5+3a). a is symbolic variable that is unknown here.
Substituting in f1 (To be more clear, first argument in the definition of f x1 = 4-2a, second argument x2=5+3a)
f1 = 3*(4-2a)^2 + 4*(5+3a)^2 + 5*(4-2a) + 6*(5+3a) + 10 ............(eq. 1)
Then function f1 becomes unknown in one variable, a and can be minimized using 1D minimization algorithm, such as golden section search, given an interval [x_lower,x_upper].
My question is:
Given different x,y,
How to evaluate (x+ay) and pass (or substitute ?) it into function f (eq1)?
How to create 'dynamic' function f1, as in eq. 1, to pass it to 1D minimization algorithm? By dynamic, I mean here is function f1 will change every time for x,y.
I am interested in a low-level implementation of this problem (sticking to the basic features of a language as much as possible and without using language specific features or object oriented features) in python, MATLAB, C or any other language, but again in 'low level.' Can you suggest something?
UPDATE: I don't want to use symbolics from python, MATLAB or from any other language.
I'm rephrasing your question in my own words, because the question in its current form is confusing:
You have a function f(x1,x2) = 3*x1^2 + 4*x2^2 + 5*x1 + 6*x2 + 10. x1 and x2 are the components of a 2D vector obtained from summing x with the product of a and y, where x and y are given vectors, and a is a scalar. You want to obtain the function that results from substituting this relation into f.
Note that the notation is a bit confusing, so I will use instead x = z+a*y, where z (replacing the x you used) and y are the given vectors.
Let's define f as an anonymous function in Matlab (you could easily use a function file as well):
f = #(x) 3*x(1)^2 + 4*x(2)^2 + 5*x(1) + 6*x(2) + 10;
Note that I'm writing this differently than you did, i.e. x(1) and x(2) instead of x1 and x2. This means that I am using components of a vector instead of two unrelated variables.
Then, let's write your equation involving a as a function as well:
g = #(a) z + a*y;
The function g(a) returns a vector for each value a, obeying g(a) = z+a*y.
Now you can do the substitution:
h = #(a) f(g(a))
h is the desired function that takes a as input and returns the value of a applied at the vector obtained from z+a*y.
you can use eval convert string to function
f = 'x+a*y'
x = 4
y = 3
for a in xrange(3):
print eval(f)
output:
4
7
10

Interpolation without specifying indices in Python

I have two arrays of the same length, say array x and array y. I want to find the value of y corresponding to x=0.56. This is not a value present in array x.
I would like python to find by itself the closest value larger than 0.56 (and its corresponding y value) and the closest value smaller than 0.56 (and its corresponding y value). Then simply interpolate to find the value of y when x 0.56.
This is easily done when I find the indices of the two x values and corresponding y values by myself and input them into Python (see following bit of code).
But is there any way for python to find the indices by itself?
#interpolation:
def effective_height(h1,h2,g1,g2):
return (h1 + (((0.56-g1)/(g2-g1))*(h2-h1)))
eff_alt1 = effective_height(x[12],x[13],y[12],y[13])
In this bit of code, I had to find the indices [12] and [13] corresponding to the closest smaller value to 0.56 and the closest larger value to 0.56.
Now I am looking for a similar technique where I would just tell python to interpolate between the two values of x for x=0.56 and print the corresponding value of y when x=0.56.
I have looked at scipy's interpolate but don't think it would help in this case, although further clarification on how I can use it in my case would be helpful too.
Does Numpy interp do what you want?:
import numpy as np
x = [0,1,2]
y = [2,3,4]
np.interp(0.56, x, y)
Out[81]: 2.56
Given your two arrays, x and y, you can do something like the following using SciPy.
from scipy.interpolate import InterpolatedUnivariateSpline
spline = InterpolatedUnivariateSpline(x, y, k=5)
spline(0.56)
The keyword k must be between 1 and 5, and controls the degree of the spline.
Example:
>>> x = range(10)
>>> y = range(0, 100, 10)
>>> spline = InterpolatedUnivariateSpline(x, y, k=5)
>>> spline(0.56)
array(5.6000000000000017)

random values from specific function python

let's say I want to pull out random values from a linear distribution function, I'm not sure how I would do that..
say I have a function y = 3x then I want to be able to pull out a random value from that line.
this is what I've tried:
x,y = [],[]
for i in range(10):
a = random.uniform(0,3)
x.append(a)
b = 3*a
y.append(b)
This gives me y values that are taken from this linear function (distribution per say). Now if this is correct, how would I do the same for a distribution that looks like a horizontal line?
that is what if I had a horizontal line function y = 3, how can I get random values pulled out from there?
Just define your function, using a lambda or explicit definition, and then call it to get the y-value:
def func(x):
return 3
points = []
for i in range(10):
x = random.uniform(0, 3)
points.append((x, func(x))
A linear function with a slope of 0 in this case is fairly trivial.
EDIT: I think I understand that question a little more clearly now. You are looking to randomly generate a point that lies under the curve? That is quite tricky to directly calculate for an arbitrary function, and you probably will want a bound to your function (i.e a < x < b). Supposing we have a bound, one simple method would be to generate a random number in a box containing the curve, and simply discard it if it isn't under the curve. This will be perfectly random.
def linearfunc(x):
return 3 * x
def getRandom(func, maxi, a, b):
while True:
x = random.uniform(a, b)
y = random.uniform(0, maxi)
if y < func(x):
return (x, y)
points = [getRandom(linearFunc, 9, 0, 3) for i in range(10)]
This method requires knowing an upper bound (maxi) to the function on the specified interval, and the tighter the upper bound, the less sampling misses will occur.

Categories

Resources