help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

definite integral of arbitrary function


From: Robert Durkacz
Subject: definite integral of arbitrary function
Date: Sun, 22 Apr 2012 22:57:59 +1000

I would like to ask, is the following effort of mine the right way to
obtain the definaite integral of
a well-defined function as a well-behaved function? By well-defined
and wel-behaved
I only mean that they would be coded properly in octave; I do not mean
in any mathematical sense.
So any function that is supplied by the caller, here referred to as x_prime_x(),
should return a vector of values corresponding to a vector of
arguments. Likewise the resulting function
should have the same capability.

Here is a use case

c1= @(tau) ... #some arbitrary function
f3= @(tau) adiff (c1, tau); #antiderivate or definite integral of the function

t= [-1:0.10:1];
plot (t, f3(t))  #-should work

What follows is octave code for two variants -they just have different
starting points for the integration.
These starting points are the lower limit of the integration. They
have to be prepended to the argument vector -
that is how lsode works -but this causes huge practical problems: not
having control over the vector of arguments,
I find that prepending a value out of the blue can easily result in a
set of values that lsode cannot handle (and my
code does not work well enough.)

There are posts in the archives that touch on this, but I could not
find an answer there -basically do I have the right approach and
should I be having these implementation difficulties?



#return definite integral of supplied function from tau= 0 to tau= t
#routine will probably blow up if t is negative, but really that is
not good enough if so-
# the integral from t (a negative value) to 0 should be calculated and
the result negated.
# lsode/octave should (?) do this by itself but I do not think it will

#the initial limit of integration could equally validly be
-Inf(inity). Of course it could be
# supplied as an additional argument, but that will increase the
hazard that when that value
# is prepended to the input vector t, lsode will find the resulting
vector to be out of order.

function x= adiff0 (xprime_x, t)
    xprime= @(x_unused, t_used) xprime_x(t_used); #using lsode in a trivial way
    [x_0, x]= lsode (xprime, 0, [0, t]); #integrate. Prepend 0 for
lsode routine
                                         #remove 0 prepended by lsode
routine (x_0 should be zero)
            #lsode returns a column vector, input t can be either row or column
endfunction

function x= adiff (xprime_x, t)
    xprime= @(x_unused, t_used) xprime_x(t_used);
    [x_0, x]= lsode (xprime, 0, [-Inf, t]);
endfunction

Robert Durkacz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]