numerical-integration

how to use scipy.integrate to get the volume of a truncated sphere?

和自甴很熟 提交于 2019-12-04 10:03:37
I'm struggling with using scipy.integrate, I used tplquad, but how can I used integrate to get the volume of (truncated)sphere? Thanks import scipy from scipy.integrate import quad, dblquad, tplquad from math import* from numpy import * R = 0.025235 #radius theta0 = acos(0.023895) #the angle from the edge of truncated plane to the center of sphere def f_1(phi,theta,r): return r**2*sin(theta)*phi**0 Volume = tplquad(f_1, 0.0,R, lambda y: theta0, lambda y: pi, lambda y,z: 0.0,lambda y,z: 2*pi) print Volume To truncate by angle it is convenient to use a spherical coordinate systems. Assuming the

Using odeint function definition

限于喜欢 提交于 2019-12-04 01:44:50
问题 Pretty noob question so please bear with me. I am following the example given here--> http://www.codeproject.com/Articles/268589/odeint-v2-Solving-ordinary-differential-equations In particular, I am looking at this function: void lorenz( state_type &x , state_type &dxdt , double t ) { dxdt[0] = sigma * ( x[1] - x[0] ); dxdt[1] = R * x[0] - x[1] - x[0] * x[2]; dxdt[2] = x[0]*x[1] - b * x[2]; } In my case, R takes on a series of values (vector with 100 doubles). odeint is called as: integrate

Solve an implicit ODE (differential algebraic equation DAE)

倖福魔咒の 提交于 2019-12-03 13:42:51
问题 I'm trying to solve a second order ODE using odeint from scipy. The issue I'm having is the function is implicitly coupled to the second order term, as seen in the simplified snippet (please ignore the pretend physics of the example): import numpy as np from scipy.integrate import odeint def integral(y,t,F_l,mass): dydt = np.zeros_like(y) x, v = y F_r = (((1-a)/3)**2 + (2*(1+a)/3)**2) * v # 'a' implicit a = (F_l - F_r)/mass dydt = [v, a] return dydt y0 = [0,5] time = np.linspace(0.,10.,21) F

Numerical Quadrature of scalar valued function with vector input using scipy

谁说胖子不能爱 提交于 2019-12-02 15:54:17
问题 I have been trying to use scipy.integrate.quadrature for integrating a scalar-valued function that takes as input a vector of fixed dimension. However, despite the claim of scipy.integrate.quadrature that it can take functions with vector inputs, I cannot for the life of me understand how to make this work. It is possible that I am misunderstanding the documentation for scipy.integrate.quadrature and that when it says the function can take vector inputs it simply means that it is able to

Custom periodic function without counter

风流意气都作罢 提交于 2019-12-02 04:04:58
问题 I am using ode45 to solve a simple ODE: function dCdt=u_vent(t,C) if t> 600 && t<= 720 Q=Q2; elseif t> 1320 && t<= 1440 Q=Q2; elseif t> 2040 && t<= 2160 Q=Q2; elseif t> 2760 && t<= 2880 Q=Q2; elseif t> 3480 && t<= 3600 Q=Q2; else Q=Q1; end V=100; C_i=400; S=100; dCdt=Q/V*C_i+S/V-Q/V*C(1); return I use then to solve: [t,C]=ode45(@u_vent, [0 1*3600], 400); I would like to create a periodic function such as the one in the picture for Q , 0<t<3600 , without using those if statements... any

Trapezoidal rule in Python

徘徊边缘 提交于 2019-12-01 23:54:05
问题 I'm trying to implement the trapezoidal rule in Python 2.7.2. I've written the following function: def trapezoidal(f, a, b, n): h = float(b - a) / n s = 0.0 s += h * f(a) for i in range(1, n): s += 2.0 * h * f(a + i*h) s += h * f(b) return s However, f(lambda x:x**2, 5, 10, 100) returns 583.333 (it's supposed to return 291.667), so clearly there is something wrong with my script. I can't spot it though. 回答1: You are off by a factor of two. Indeed, the Trapezoidal Rule as taught in math class

Numerical Triple Integration in R

吃可爱长大的小学妹 提交于 2019-12-01 23:04:08
Is it possible to do triple integration in R without using the cubature package? based on the answer in this post InnerFunc = function(x) { x + 0.805 } InnerIntegral = function(y) { sapply(y, function(z) { integrate(InnerFunc, 15, z)$value }) } integrate(InnerIntegral , 15, 50) 16826.4 with absolute error < 1.9e-10 For example, to code this triple integral : I tried InnerMostFunc = function(v) { v + y^2 } InnerMostIntegral = function(w) { sapply(w, function(x) { integrate(InnerMostFunc, 1, 2)$value }) } InnerIntegral = function(y) { sapply(y, function(z){integrate(InnerMostIntegral, 1, 2)

Trapezoidal rule in Python

坚强是说给别人听的谎言 提交于 2019-12-01 21:55:00
I'm trying to implement the trapezoidal rule in Python 2.7.2. I've written the following function: def trapezoidal(f, a, b, n): h = float(b - a) / n s = 0.0 s += h * f(a) for i in range(1, n): s += 2.0 * h * f(a + i*h) s += h * f(b) return s However, f(lambda x:x**2, 5, 10, 100) returns 583.333 (it's supposed to return 291.667), so clearly there is something wrong with my script. I can't spot it though. You are off by a factor of two. Indeed, the Trapezoidal Rule as taught in math class would use an increment like s += h * (f(a + i*h) + f(a + (i-1)*h))/2.0 (f(a + i*h) + f(a + (i-1)*h))/2.0 is

Python numerical integration with Simpson's rule

人盡茶涼 提交于 2019-12-01 20:42:21
I have started to work through this book ( Computational Physics Exercise 5.4 ) and its exercises and I got stuck with the following question: Write a Python function J(m,x) that calculates the value of Jm(x) using Simpson’s rule with N = 1000 points. Use your function in a program to make a plot, on a single graph, of the Bessel functions J0, J1, and J2 as a function of x from x = 0 to x = 20. I have created the following code to evaluate the first part of the question but not sure if even this is correct: def f(x, t): return 1 / pi * (math.cos(x - t * math.sin(x))) def float_range(a, b, c):

Numerical Integration over a Matrix of Functions, SymPy and SciPy

大兔子大兔子 提交于 2019-12-01 16:57:54
问题 From my SymPy output I have the matrix shown below, which I must integrate in 2D. Currently I am doing it element-wise as shown below. This method works but it gets too slow (for both sympy.mpmath.quad and scipy.integrate.dblquad ) for my real case (in which A and its functions are much bigger (see edit below): from sympy import Matrix, sin, cos import sympy import scipy sympy.var( 'x, t' ) A = Matrix([[(sin(2-0.1*x)*sin(t)*x+cos(2-0.1*x)*cos(t)*x)*cos(3-0.1*x)*cos(t)], [(cos(2-0.1*x)*sin(t)