Group Abstract Group Abstract

Message Boards Message Boards

Add results of NDsolve to each other?

Posted 10 years ago

Hello! I need to average on many runs of the function NDsolve - each one with different initial conditions. I store each run in the variable:

solList = Table[1, {j, 2}, {i, DtNum}];

I am running a Do loop as follows:

Do[
s = NDSolve[{cg'[t] == I \[CapitalOmega] ce[t], 
    ce'[t] == I \[CapitalOmega] cg[t], ce[DeltaT*(n - 1)] == 0, 
    cg[DeltaT*(n - 1)] == 1}, {cg[t], ce[t]}, {t, DeltaT*(n - 1), 
    DeltaT*n}, 
   "ExtrapolationHandler" -> {Indeterminate &, 
     "WarningMessage" -> False}];
sols = {cg[t], ce[t]} = {cg[t], ce[t]} /. s[[1]];
solList[[1]][[n]] = sols[[1]]+solList[[1]][[n]] ;
solList[[2]][[n]] = sols[[2]]+solList[[2]][[n]] ;
,{n, Tmax}]

In the end I want to plot the value in, for example: solList[[1]][[10]] - hoping that it will be the sum of all the functions that were calculated in the Do loop. It seems not to work and I don't know why. Any help will be greatly appreciated!

POSTED BY: shaked rozen

Hi,

your code is incomplete, so I cannot run it as it is. It would be useful if you were to post functional code. Here is an example where I use a simple harmonic oscillator with different initial conditions and then calculate the mean.

sols = Table[NDSolveValue[{x''[t] == -0.2 x[t], x[0] == RandomReal[], x'[0] == RandomReal[]}, x[t], {t, 0, 10}], {k, 1, 10}];
Plot[sols, {t, 0, 10}]

enter image description here

Here is the average over all solutions:

Plot[Mean[sols], {t, 0, 10}]

enter image description here

If you really want the total, you can substitute Mean by Total.

Cheers,

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard