NDSolve slow when using generic function definition
- To: mathgroup at smc.vnet.net
- Subject: [mg124998] NDSolve slow when using generic function definition
- From: Panayiotis Georgiou <ps.georgiou at gmail.com>
- Date: Fri, 17 Feb 2012 06:23:33 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Hello, I am using NDSolve to solve a simple 1st order ODE. The problem I have is that when I solve the function with predefined parameters it runs extremely fast. However, if I try to do the same but by defining a generic function it is extremely slow. I have tried placing the evaluate at various places but still no luck. To be more specific, when solving this for example: sol = NDSolve[{z'[t] == \[Alpha]*fmod2[z[t], 2, 0.5, k, 1, d]* I1[t], z[0] == 0.1}, z, {t, 0, 1/f0}, MaxStepSize -> 0.0001, AccuracyGoal -> 6, PrecisionGoal -> 5]; pp4 = Plot[{Evaluate[z[t] /. sol]}, {t, 0, 1/f0}, PlotRange -> All, AxesOrigin -> {0, 0}, GridLines -> {{{0.5, Dashed}, {max[n1], Dashed}}, None}] It runs almost instantly. But then if I do the following: Zt[t_, n_, m_, j_, d_] := z[t] /. NDSolve[{z'[t3] == \[Alpha]*fmod2[z[t3], n, m, K[n], j, d]* I1[t3], z[0] == z0}, z, {t3, 0, 1/f0}, MaxStepSize -> 0.0001, AccuracyGoal -> 6, PrecisionGoal -> 5]; pp4 = Plot[{Evaluate[Zt[t, 2, 0.5, 1, d]]}, {t, 0, 1/f0}, PlotRange -> All, AxesOrigin -> {0, 0}, GridLines -> {{{0.5, Dashed}, {max[n1], Dashed}}, None}] It takes about 5 minutes to evaluate and plot. I have tried enclosing in Evaluate[] various parts of the function definition but it is still extremely slow. Do you have any idea why there is this speed difference between the two approaches? Thanks for your help.