<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>MaplePrimes - answers and comments on Question, speed up calculation using the interpolant option of `dsolve/numeric`</title>
    <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant</link>
    <language>en-us</language>
    <copyright>2026 Maplesoft, A Division of Waterloo Maple Inc.</copyright>
    <generator>Maplesoft Document System</generator>
    <lastBuildDate>Wed, 10 Jun 2026 20:06:28 GMT</lastBuildDate>
    <pubDate>Wed, 10 Jun 2026 20:06:28 GMT</pubDate>
    <itunes:subtitle />
    <itunes:summary />
    <description>The latest answers and comments added to the Question, speed up calculation using the interpolant option of `dsolve/numeric`</description>
    <image>
      <url>http://www.mapleprimes.com/images/mapleprimeswhite.jpg</url>
      <title>MaplePrimes - answers and comments on Question, speed up calculation using the interpolant option of `dsolve/numeric`</title>
      <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant</link>
    </image>
    <item>
      <title>Suggestion</title>
      <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant?ref=Feed:MaplePrimes:speed up calculation using the interpolant option of `dsolve/numeric`:Comments#answer80679</link>
      <itunes:summary>Hello Alejandro,

In the setup in the worksheet, the timing measurements for dsolve are actually setting up and applying the numerical problem every time you request the solution.
Given that, it's actually quite fast ;-)

So basically at every requested point the worksheet calls the top-level dsolve/numeric which has to:
1) Process the input problem
2) Build the numerical evaluator
3) Compute the interpolant over 0..2
4) Evaluate the interpolant for the requested value

Ideally you want to set up the problem once for the values of beta,lambda,sigma,P,B (steps 1-3), then use this for multiple 'z' values (step 4).

This can be accomplished by replacing your definition for DLn2:

DLn2:=(z,beta,lambda,sigma,P,B)-&gt;
   (1+z)*sqrt((1+sigma)^P+B)*rhs(op(2,rr(beta,lambda,sigma,P,B)))(z);

With the following:

DLn2:=proc(z,beta,lambda,sigma,P,B) global DLn2state;
   if not assigned(DLn2state) or DLn2state[1]&lt;&gt;[beta,lambda,sigma,P,B] then
      DLn2state := [[beta,lambda,sigma,P,B],
                     rhs(op(2,rr(beta,lambda,sigma,P,B)))];
   end if;
   (1+z)*sqrt((1+sigma)^P+B)*DLn2state[2](z);
end proc:

Now in this I am using the global variable DLn2state to store the dsolve/solution once computed, then using it until the parameters change, at which point the solution is rebuilt.
This is not the preferred method, as when you set this up in a main program you probably want to obtain the solution procedure once, then use it multiple times, not using a global as I have done above. 

With this change the timings become much better, and the dsolve approach is a clear winner.

You may also want to consider the 'output=piecewise' option of dsolve/numeric for this problem, as then you can get a piecewise solution.

And finally, if the z values at which you want to compute the solution are known in advance, the output=Array option (see ?dsolve,numeric) is by far the fastest.

- Allan Wittkopf</itunes:summary>
      <description>Hello Alejandro,

In the setup in the worksheet, the timing measurements for dsolve are actually setting up and applying the numerical problem every time you request the solution.
Given that, it's actually quite fast ;-)

So basically at every requested point the worksheet calls the top-level dsolve/numeric which has to:
1) Process the input problem
2) Build the numerical evaluator
3) Compute the interpolant over 0..2
4) Evaluate the interpolant for the requested value

Ideally you want to set up the problem once for the values of beta,lambda,sigma,P,B (steps 1-3), then use this for multiple 'z' values (step 4).

This can be accomplished by replacing your definition for DLn2:

DLn2:=(z,beta,lambda,sigma,P,B)-&gt;
   (1+z)*sqrt((1+sigma)^P+B)*rhs(op(2,rr(beta,lambda,sigma,P,B)))(z);

With the following:

DLn2:=proc(z,beta,lambda,sigma,P,B) global DLn2state;
   if not assigned(DLn2state) or DLn2state[1]&lt;&gt;[beta,lambda,sigma,P,B] then
      DLn2state := [[beta,lambda,sigma,P,B],
                     rhs(op(2,rr(beta,lambda,sigma,P,B)))];
   end if;
   (1+z)*sqrt((1+sigma)^P+B)*DLn2state[2](z);
end proc:

Now in this I am using the global variable DLn2state to store the dsolve/solution once computed, then using it until the parameters change, at which point the solution is rebuilt.
This is not the preferred method, as when you set this up in a main program you probably want to obtain the solution procedure once, then use it multiple times, not using a global as I have done above. 

With this change the timings become much better, and the dsolve approach is a clear winner.

You may also want to consider the 'output=piecewise' option of dsolve/numeric for this problem, as then you can get a piecewise solution.

And finally, if the z values at which you want to compute the solution are known in advance, the output=Array option (see ?dsolve,numeric) is by far the fastest.

- Allan Wittkopf</description>
      <guid>80679</guid>
      <pubDate>Tue, 23 Aug 2005 02:50:15 Z</pubDate>
      <itunes:author>Allan Wittkopf</itunes:author>
      <author>Allan Wittkopf</author>
    </item>
    <item>
      <title>Some further details</title>
      <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant?ref=Feed:MaplePrimes:speed up calculation using the interpolant option of `dsolve/numeric`:Comments#comment86873</link>
      <itunes:summary>Yes, the purpose of this calculation is to minimize this chi^2 function with GOT,
as in the worksheet that I have attached in the thread
&lt;a href="http://beta.mapleprimes.com/forum/generate_code_for_numerical_integral""&gt;
generate code for numerical integral&lt;/a&gt;

So, the idea is that GOT uses an approximation for chi^2, and its derivatives wrt the parameters, using the interpolating function instead of the integral. Derivatives may be used in the local phase of the search of the minimum, as far as I understand. Hence I guess that some properties of the approximation to the derivatives are also important, eg continuity. 

For a given dataset, the range of z is fixed by (z_min, z_max), z=2 was just for testing. So, if I understand correctly, I should use the output=Array option, but not the global variable. What do you suggest instead?


</itunes:summary>
      <description>Yes, the purpose of this calculation is to minimize this chi^2 function with GOT,
as in the worksheet that I have attached in the thread
&lt;a href="http://beta.mapleprimes.com/forum/generate_code_for_numerical_integral""&gt;
generate code for numerical integral&lt;/a&gt;

So, the idea is that GOT uses an approximation for chi^2, and its derivatives wrt the parameters, using the interpolating function instead of the integral. Derivatives may be used in the local phase of the search of the minimum, as far as I understand. Hence I guess that some properties of the approximation to the derivatives are also important, eg continuity. 

For a given dataset, the range of z is fixed by (z_min, z_max), z=2 was just for testing. So, if I understand correctly, I should use the output=Array option, but not the global variable. What do you suggest instead?


</description>
      <guid>86873</guid>
      <pubDate>Tue, 23 Aug 2005 04:27:42 Z</pubDate>
      <itunes:author>jakubi</itunes:author>
      <author>jakubi</author>
    </item>
    <item>
      <title>Piecewise</title>
      <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant?ref=Feed:MaplePrimes:speed up calculation using the interpolant option of `dsolve/numeric`:Comments#comment90187</link>
      <itunes:summary>It may then be more suitable to use a piecewise approximation to the solution - this allows it to be used more flexbly later on.

So for your problem, generate the piecewise solution for a given set of parameters, and z=0..2.

As a simple example:

&gt; sol := op([2,2],dsolve({diff(y(x),x)=-y(x),y(0)=1},numeric,
&gt;        range=0..2,output=piecewise));                      
sol := {
       { undefined , x &lt; 0.

    0.999621690653975 - 0.972619698891573 x

                                                 2
     + 0.486310338843125 (x - 0.0277621231571284)

                                                 3
     - 0.162115462190059 (x - 0.0277621231571284)

                                                  4
     + 0.0402113832410770 (x - 0.0277621231571284)  ,

     ...

Is this suitable?</itunes:summary>
      <description>It may then be more suitable to use a piecewise approximation to the solution - this allows it to be used more flexbly later on.

So for your problem, generate the piecewise solution for a given set of parameters, and z=0..2.

As a simple example:

&gt; sol := op([2,2],dsolve({diff(y(x),x)=-y(x),y(0)=1},numeric,
&gt;        range=0..2,output=piecewise));                      
sol := {
       { undefined , x &lt; 0.

    0.999621690653975 - 0.972619698891573 x

                                                 2
     + 0.486310338843125 (x - 0.0277621231571284)

                                                 3
     - 0.162115462190059 (x - 0.0277621231571284)

                                                  4
     + 0.0402113832410770 (x - 0.0277621231571284)  ,

     ...

Is this suitable?</description>
      <guid>90187</guid>
      <pubDate>Wed, 24 Aug 2005 01:48:40 Z</pubDate>
      <itunes:author>Allan Wittkopf</itunes:author>
      <author>Allan Wittkopf</author>
    </item>
    <item>
      <title>missing trick</title>
      <link>http://www.mapleprimes.com/questions/43703-Speed-Up-Calculation-Using-The-Interpolant?ref=Feed:MaplePrimes:speed up calculation using the interpolant option of `dsolve/numeric`:Comments#comment90199</link>
      <itunes:summary>I have written a new numeric version for chi^2 using piecewise option (chin4,
in the new attached worksheet on top). But I need to find yet the trick to use
this function once instead of N times, as timings show.

</itunes:summary>
      <description>I have written a new numeric version for chi^2 using piecewise option (chin4,
in the new attached worksheet on top). But I need to find yet the trick to use
this function once instead of N times, as timings show.

</description>
      <guid>90199</guid>
      <pubDate>Wed, 24 Aug 2005 23:36:25 Z</pubDate>
      <itunes:author>jakubi</itunes:author>
      <author>jakubi</author>
    </item>
  </channel>
</rss>