|
From: Alexandre F. <o.a...@gm...> - 2011-06-17 13:01:56
|
The Pieter-Tjerk is fairly better :D redefining it this make the same thing but you ask for a maximum integration distance instead a maximum intf(x0,a,h) = (h<=delta) ? f(x0+a/2)*a : ( intf(x0,a/2,delta) + intf(x0+a/2,a/2,delta) ) when you use gnuplot i think you want to plot something, this version if you say plot intf(x0, x - x0, delta) ensures that any integration interval will be larger than delta, and that the recurrence steep will be the minimum (with this function structure) if you plot with 1024 samples the integration function intf(x0, a, delta) is called 1397077 times, and the function f(x) will be evaluated 350548 times and the max recurrence depth will be 10 to integrate with the same acuracy the Daniel's scheme will call the intf* functions and f(x) are called about 523 thounsand times. and the max recurrence deepth is 1024 my scheme calls f(x) 1024 times and intf(x) 1024 times, but use some global variables. excuse-me i accidentally replied only to 3snoW here goes the my scheme. It's not encouraged the use of global variables, but in such case i think here we a better solution, not THE better solution # Characteristic for the integration n_samps = 100; a = 0.0; b = 5.0; # initialization aux = 0.0; h = (b-a)/n_samps; set samples n_samps set xrange [a to b] # the function f(x) = sin(5*x)/(0.1+x) #the integrator scheme intf(x) = (aux = (aux + f(x)*h)) # plot it plot f(x), intf(x) # another function f(x) = sin(x)*x replot for the 2D is also possible. the only thing you need is to reinit the aux variable when x=a this scheme is as poor as the former, therefore it's O(nsamples) and why to use recurrence if you can use memorization? intf can also have other integration schemes, with the advantage that you have to implement only once for example simpson's rule or any other method intf(x) = (aux = (aux + (f(x-h)+4*f(x-0.5*h)+f(x))*h/6)) this example make the h function to be called three times more. the ideal to generate plots is to derive integration schemes open in f(x-h) and closed in f(x), but i will not make this now :D |