2
$\begingroup$

I have the following code:

       u[0, t_, h_] := 0; For[k = 1, k < 11,u[k, t_, h_] = 
       u[k - 1, t, h] +h Integrate[(D[u[k - 1, \[Tau], h], \[Tau]] + 
       u[k - 1, \[Tau], h]^2 - 1), {\[Tau], 0, t}]; 
       Print["k=", k, "____", 0 Expand[u[k, t, h]]]; k++]
           

I want to calculate the integral numerically as it is time consuming by increasing the loop.

$\endgroup$
4
  • $\begingroup$ Have you tried NIntegrate, Mathematica's numerical integration command? $\endgroup$
    – Michael E2
    Commented Oct 5 at 21:13
  • $\begingroup$ No sir, could please help me to do it? $\endgroup$
    – user62716
    Commented Oct 5 at 21:15
  • 1
    $\begingroup$ Your recent edit has significantly changed the question, pleasse avoid that. If you have a new question, consider posting a separate question. And, we already have quite a few posts about Simpson's rule: mathematica.stackexchange.com/search?q=simpson+rule Please have a look. $\endgroup$
    – xzczd
    Commented Nov 17 at 3:25
  • 1
    $\begingroup$ @user62716 Which original question led you to the code? Do you try to solve an integral-equation? If yes please show this equation too. Thanks! $\endgroup$ Commented Nov 18 at 9:00

2 Answers 2

2
$\begingroup$

Use Memoization, it become much faster.

u[0, t_, h_] := 0;
u[k_, t_, h_] := u[k, t, h] = u[k - 1, t, h] + 
      h* Integrate[(D[u[k - 1, τ, h], τ] + u[k - 1, τ, h]^2 - 1), {τ, 0, t}]

data = Table[u[k, t, h], {k, 1, 5}]

To speed it up more, you can use numerical values for t,h like this

u[0,t_,h_]:=0;
u[k_,t_,h_]:=u[k,t,h]=u[k-1,t,h]+h* Integrate[(D[u[k-1,τ,h],τ]+u[k-1,τ,h]^2-1),{τ,0,t}];
t=1;h=3;
data=Table[u[k,t,h],{k,1,11}]//N

enter image description here

Which took less than one minute. For k=9 it took little over one second.

u[0, t_, h_] := 0;
u[k_, t_, h_] := 
  u[k, t, h] = 
   u[k - 1, t, h] + 
    h* Integrate[(D[u[k - 1, \[Tau], h], \[Tau]] + 
        u[k - 1, \[Tau], h]^2 - 1), {\[Tau], 0, t}];
t = 1; h = 3;
Timing[ Table[N@u[k, t, h], {k, 1, 9}]]

enter image description here

$\endgroup$
7
  • $\begingroup$ Dear Nasser, still time consuming when k,1,10 $\endgroup$
    – user62716
    Commented Oct 5 at 21:20
  • $\begingroup$ @user62716 plugin values for t and h. You have to do this any way if you want to do numerical integration. $\endgroup$
    – Nasser
    Commented Oct 5 at 21:21
  • $\begingroup$ I need it depend on h, later I will evaluate h. $\endgroup$
    – user62716
    Commented Oct 5 at 21:22
  • $\begingroup$ @user62716 but then you can not do numerical integration if h remain symbol. Will not work. $\endgroup$
    – Nasser
    Commented Oct 5 at 21:23
  • $\begingroup$ is there any way to make faster depend on t, h? $\endgroup$
    – user62716
    Commented Oct 5 at 21:23
1
$\begingroup$

If you split up your integral, and make some observations about Integrate[u[-1 + k, t, h]^2, t] always being zero at t=0 you can simplify your expression to:

u[0, t_, h_] := 0;
u[k_, t_, h_] := u[k, t, h] =
  (1 + h) u[-1 + k, t, h] + 
   h ((Integrate[u[-1 + k, τ, h]^2, τ] /. τ -> t) - t)

Which takes a little over a minute to calculate to kMax = 9: (k = 10 seem to be a lot harder, the LeafCount of the DownValues stored in u is starting to get huge):

kMax = 9;
(*preload values*)
u[kMax, t, h]; // AbsoluteTiming

uDat = u[#, t, h] & /@ Range[0, kMax];

{72.2118, Null}

Compare this to the original:

uOrig[0, t_, h_] := 0; 
For[k = 1, k <= kMax, 
  uOrig[k, t_, h_] = 
   uOrig[k - 1, t, h] + 
    h Integrate[(D[uOrig[k - 1, \[Tau], h], \[Tau]] + 
        uOrig[k - 1, \[Tau], h]^2 - 1), {\[Tau], 0, t}];
  Print["k=", k, "____", 0 Expand[uOrig[k, t, h]]]; 
  k++] // AbsoluteTiming

{145.233, Null}

And of course verify our simplification is correct (I only verify up to k=8, because the k=9 case was taking too long to simplify):

origDat = uOrig[#, t, h] & /@ Range[0, kMax];
FullSimplify[Most@origDat == Most@uDat]

True

$\endgroup$
1
  • $\begingroup$ Can we evaluate it using say simpson...etc? $\endgroup$
    – user62716
    Commented Nov 16 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.