numerical-integration

R plotting integral

痴心易碎 提交于 2019-11-29 15:34:30
I'm having some problems with integration function in R. I'm trying to plot the integral vo but it seems I'm not doing correctly. t <- seq(0, 0.04, 0.0001) vi <- function(x) {5 * sin(2 * pi * 50 * x)} vo <- function(x) {integrate(vi, lower=0, upper=x)$value} test_vect = Vectorize(vo, vectorize.args='x') plot(t, vo(t)) # should be a cosine wave plot(t, vi(t)) # sine wave vo should be a sine wave but using test_vect gives me wrong plot and using vo directly gives error 'x' and 'y' lengths differ . Can anyone, please, help me on this matter? You are already there. Just use plot(t, test_vect(t)) .

How do I do numerical integration of a vector in MATLAB?

耗尽温柔 提交于 2019-11-29 04:44:25
I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one. I found that we can use trapz or quad, but i don't really understand how to integrate without the function. Doresoom If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections, x=0:pi/357:pi; y=sin(x); area=trapz(x,y); If you just use trapz(y) , you'll get a much larger number, since the default distance between points is assumed to be 1. This

using SciPy to integrate a function that returns a matrix or array

邮差的信 提交于 2019-11-29 03:38:49
I have a symbolic array that can be expressed as: from sympy import lambdify, Matrix g_sympy = Matrix([[ x, 2*x, 3*x, 4*x, 5*x, 6*x, 7*x, 8*x, 9*x, 10*x], [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]]) g = lambdify( (x), g_sympy ) So that for each x I get a different matrix: g(1.) # matrix([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], # [ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]) g(2.) # matrix([[ 2.00e+00, 4.00e+00, 6.00e+00, 8.00e+00, 1.00e+01, 1.20e+01, 1.40e+01, 1.60e+01, 1.80e+01, 2.00e+01], # [ 4.00e+00, 8.00e+00, 1.60e+01, 3.20e+01, 6.40e+01, 1.28e+02, 2.56e+02, 5.12e

f2py, Python function that returns an array (vector-valued function)

醉酒当歌 提交于 2019-11-28 13:43:42
In the following Python I have five functions contained in the array returned by func which I have to integrate. The code calls an external Fortran module generated using f2py : import numpy as np from numpy import cos, sin , exp from trapzdv import trapzdv def func(x): return np.array([x**2, x**3, cos(x), sin(x), exp(x)]) if __name__ == '__main__': xs = np.linspace(0.,20.,100) ans = trapzdv(func,xs,5) print 'from Fortran:', ans print 'exact:', np.array([20**3/3., 20**4/4., sin(20.), -cos(20.), exp(20.)]) The Fortran routine is: subroutine trapzdv(f,xs,nf,nxs,result) integer :: I double

R plotting integral

杀马特。学长 韩版系。学妹 提交于 2019-11-28 09:37:38
问题 I'm having some problems with integration function in R. I'm trying to plot the integral vo but it seems I'm not doing correctly. t <- seq(0, 0.04, 0.0001) vi <- function(x) {5 * sin(2 * pi * 50 * x)} vo <- function(x) {integrate(vi, lower=0, upper=x)$value} test_vect = Vectorize(vo, vectorize.args='x') plot(t, vo(t)) # should be a cosine wave plot(t, vi(t)) # sine wave vo should be a sine wave but using test_vect gives me wrong plot and using vo directly gives error 'x' and 'y' lengths

using SciPy to integrate a function that returns a matrix or array

限于喜欢 提交于 2019-11-27 22:09:30
问题 I have a symbolic array that can be expressed as: from sympy import lambdify, Matrix g_sympy = Matrix([[ x, 2*x, 3*x, 4*x, 5*x, 6*x, 7*x, 8*x, 9*x, 10*x], [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]]) g = lambdify( (x), g_sympy ) So that for each x I get a different matrix: g(1.) # matrix([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], # [ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]) g(2.) # matrix([[ 2.00e+00, 4.00e+00, 6.00e+00, 8.00e+00, 1.00e+01, 1.20e+01, 1.40e+01, 1.60e+01,

Absolute error of ODE45 and Runge-Kutta methods compared with analytical solution

梦想与她 提交于 2019-11-27 08:29:25
I would appreciate if someone can help with the following issue. I have the following ODE: dr/dt = 4*exp(0.8*t) - 0.5*r ,r(0)=2, t[0,1] (1) I have solved (1) in two different ways. By means of the Runge-Kutta method (4th order) and by means of ode45 in Matlab. I have compared the both results with the analytic solution, which is given by: r(t) = 4/1.3 (exp(0.8*t) - exp(-0.5*t)) + 2*exp(-0.5*t) When I plot the absolute error of each method with respect to the exact solution, I get the following: For RK-method, my code is: h=1/50; x = 0:h:1; y = zeros(1,length(x)); y(1) = 2; F_xy = @(t,r) 4.*exp

Quadruple Integral Using Nested Integral2 in Matlab

别说谁变了你拦得住时间么 提交于 2019-11-27 07:32:35
问题 I am trying to solve a problem of the following form: f=@(x,y,z,w) x.*y.*z.*w; % A complicated black box function a=1;b=1;c=1;d=1; % Integration limits I=integral2(@(x,y)integral2(@(z,w)f(x,y,z,w),c,-c,d,-d),a,-a,b,-b); Using this implementation I get the following error: Error using .* Matrix dimensions must agree. The problem is that x, y, z, and w are not the same size. For the first function evaluation all inputs are the same size but then on the second function evaluation x and y are not

Solving an ODE when the function is given as discrete values -matlab-

元气小坏坏 提交于 2019-11-27 05:13:32
I have the following ODE: x_dot = 3*x.^0.5-2*x.^1.5 % (Equation 1) I am using ode45 to solve it. My solution is given as a vector of dim(k x 1) (usually k = 41, which is given by the tspan ). On the other hand, I have made a model that approximates the model from (1), but in order to compare how accurate this second model is, I want to solve it (solve the second ODE) by means of ode45 . My problem is that this second ode is given discrete: x_dot = f(x) % (Equation 2) f is discrete and not a continuous function like in (1). The values I have for f are: 0.5644 0.6473 0.7258 0.7999 0.8697 0.9353

MATLAB: Is it possible to have two event values whilst using ode45?

这一生的挚爱 提交于 2019-11-26 23:18:51
I want two limitations to my ode45 calculation of a movement equation: position and time. I have already got the time event to work but I am not sure if and how I can add another event for limiting the position. EDIT: I also have many different particles coupled together in one ODE equation and need them to stop individually once they reach a 'roof' as they all travel at different speeds... would I be able to achieve this through events? I have an idea on how I would do this but its very complicated and would probably be very slow... I'm not sure if you can do exactly what you want, but it is