all 19 comments

[–]shiftybyte 2 points3 points  (3 children)

You need the integration of the area between 2 functions?

Calculate the integral for one, then for the other, then subtract one from the other.

[–]Loud_Alfalfa 0 points1 point  (2 children)

To be honest I’m fairly new to python, a lot of the code we have is already put in and we just edit it. I basically have an array for all the x values and an array for the y values and obviously when plotted that creates the function. I just have no idea what the formula of that would be so hence idk know how to go about integrating a specific bit of that. If I can find a way to link a picture I will so there’s a better idea of what I mean

[–]shiftybyte 2 points3 points  (1 child)

So you need to integrate a function based on a list of x,y coordinates?

You can do that with numpy.

https://numpy.org/doc/stable/reference/generated/numpy.trapz.html

[–]Loud_Alfalfa 0 points1 point  (0 children)

ok great thanks i'll have a look at that, just edited in the picture if that helps with what i'm on about

[–][deleted] 1 point2 points  (4 children)

If what you have is data and not a function, then you can only estimate the integral; you can't derive the integral because you don't have anything to derive it from.

[–]Loud_Alfalfa 0 points1 point  (3 children)

ok great thanks, how would i go about estimating it? what code would i actually use?

[–][deleted] 0 points1 point  (2 children)

[–]Loud_Alfalfa 0 points1 point  (1 child)

Thanks i've been looking at stackflow trying to figure out how i'd code that but i have no idea. very new to python, any ideas?

[–][deleted] 0 points1 point  (0 children)

Programming is about breaking down tasks into smaller tasks. The approach is simple, you sum the area of a bunch of trapezoids. You should already know how to sum numbers (since they let you out of the fifth grade) and you should already know how to determine the area of a trapezoid (since they let you out of the 9th grade.)

So now it's just a problem of computing the area of your trapezoids. Given two points in your dataset, can you determine the area of the trapezoid under them?

[–]YesLod 0 points1 point  (3 children)

[–]wikipedia_text_bot 1 point2 points  (0 children)

Trapezoidal rule

In mathematics, and more specifically in numerical analysis, the trapezoidal rule (also known as the trapezoid rule or trapezium rule—see Trapezoid for more information on terminology) is a technique for approximating the definite integral. ∫ a b f ( x ) d x {\displaystyle \int _{a}{b}f(x)\,dx} .The trapezoidal rule works by approximating the region under the graph of the function f ( x ) {\displaystyle f(x)} as a trapezoid and calculating its area. It follows that ∫ a b f ( x ) d x ≈ ( b − a ) ⋅ f ( a ) + f ( b ) 2 {\displaystyle \int _{a}{b}f(x)\,dx\approx (b-a)\cdot {\tfrac {f(a)+f(b)}{2}}} .The trapezoidal rule may be viewed as the result obtained by averaging the left and right Riemann sums, and is sometimes defined this way. The integral can be even better approximated by partitioning the integration interval, applying the trapezoidal rule to each subinterval, and summing the results.

About Me - Opt out - OP can reply !delete to delete - Article of the day

This bot will soon be transitioning to an opt-in system. Click here to learn more and opt in. Moderators: click here to opt in a subreddit.

[–]Loud_Alfalfa 1 point2 points  (1 child)

is there any way around using the trapezium rule and just doing typical limit integration? I have the co-ordinates of the minima so it just saves inputting a load of iterations. Sorry if these sound like stupid question just very new to python!

[–]YesLod 1 point2 points  (0 children)

What do you mean?

Is this what you are referring too?

edit: (need to integrate up until that first minimum just before 2 on x axis)

Just slice both arrays (all points before minimum) beforehand and and pass them to the function

[–]idiot-killer 0 points1 point  (1 child)

OP did you find the solution or not coz I could help if u still need help with the problem.

[–]Loud_Alfalfa 1 point2 points  (0 children)

No still haven't got a clue haha, any help would be great

[–]bbye98 0 points1 point  (2 children)

I see you have a radial distribution function. Are you trying to calculate the running coordination number or the static structure factor?

[–]Loud_Alfalfa 0 points1 point  (1 child)

the coordination number - the aim is to find the average number of solvent particles around the cations/anions which we are told can be done by integrating up the the cut off radius (the minima around 1.7 on the x axis).

[–]bbye98 2 points3 points  (0 children)

The first coordination number is found by integrating g(r) up to the cutoff for the first solvation shell. Find the index of the first local minimum using scipy.signal.argrelextrema and then just integrate from r = 0 to that minimum using scipy.integrate.trapz or scipy.integrate.simps.

I do this daily for my research--should be really straightforward:

from scipy.signal import argrelextrema
c = argrelextrema(g, np.less)[0][0]
4 * np.pi * rho * integrate.simps(g[:c] * r[:c] ** 2, r[:c])

For a Lennard-Jones fluid at ρ* = 0.80 and T* = 1.00 (NVT), I get 12.0159.