# Optimizing the fitting of Ordinary Differential Equation(NDsolve) involving interpolation functions

I am a newbie to this forum. I am trying to use NDsolve to solve some ODEs involving the interpolation function. Then, I will compare the fitted curve and data to determine the ODE coefficients.

Basically, I measure the Voltage(V) and Current(I) for a circuit. Then I want to use these V and I to determine the value of electric components in the circuit.

First, I make a interpolation function out of the waveform V data:

dataIndex = Range[2, 10001];
myVoltData = Transpose[{rawData[[dataIndex, 1]], rawData[[dataIndex, 4]]}];
myCurrData = Transpose[{rawData[[dataIndex, 1]], rawData[[dataIndex, 2]]}];
voltageFunc = Interpolation[myVoltData, InterpolationOrder -> 2];


Second, use NDSolve to solve the ODE, then use the solution to calculate the chi-sq. In the following code, the ODE parameters to be fit are r0Fit, r1Fit, c1Fit, currOffset, and the function fitFunc will eventually return the chi-sq:

fitFunc[r0Fit_?NumberQ, r1Fit_?NumberQ, c1Fit_?NumberQ, currOffsetFit_?NumberQ] :=
Block[ {sol, curr00, curr11},
sol = NDSolve[ {voltageFunc[t] == curr00[t]*r0Fit + curr11[t]*r1Fit ,
curr00[t] == (curr11[t] + r1Fit*c1Fit/10^6*curr11'[t]),
curr00[t0fit] == curr11[t0fit] == (voltageFunc[t0fit])/(r0Fit + r1Fit)},
{curr00, curr11}, {t, t0fit, t1fit}, AccuracyGoal -> 7][[1]];
Apply[Plus,
((myCurrData[[fitDataRange, 2]] + 50*currOffsetFit/10^6
- 50*Flatten[curr00[t] /. sol /.t -> myCurrData[[fitDataRange, 1]]])
/myCurrDataErr[[fitDataRange, 2]])^2/dataPnts]
]


Finally, use NMinimize find the fitting result with chi-sq minimum:

Timing[fitResult = NMinimize[ {fitFunc[r0f, r1f, c1f,  iOffset],
130 < r0f < 170, 3000 < r1f < 3200, 650 < c1f < 750, 60 < iOffset < 100},
{r0f, r1f, c1f,  iOffset},AccuracyGoal -> 0, Method -> "DifferentialEvolution"]]


My first problem is, since voltageFunc[t] is the interpolation function from data, it severely procrastinates the NDSolve command, as a result, the NMinimize is consuming vast amount of time(>20 mins). I wonder, why does interpolation function of ODE delay NDSolve so much? Is there any optimization method to make the NMinimize and NDSolve faster?

Secondly, NMinimize occasionally fails to find the global minimum. Any suggestions to improve the fitting result's reliability.

Any suggestion is appreciated. Thank you all.

• Hey, anyone interested in this problem. Just a update, I finally solved the problem in IGOR, which has a better solution for fitting ODE. Mar 17, 2014 at 21:20

You can try solving the differential equation once and for all with

solCurr =  DSolve[{voltageFunc[t] == curr00[t]*r0Fit + curr11[t]*r1Fit,
curr00[t] == (curr11[t] + r1Fit*c1Fit/10^6*curr11'[t]),
curr00[t0fit] == curr11[t0fit] == (voltageFunc[t0fit])/(r0Fit + r1Fit)},
{curr00, curr11}, {t}]


which will involve integrals over your voltageFunc. This should improve speed at least.

• Hi, thanks for this idea. It seems to use DSolve to replace NDSolve. However, I tried it in my notebook and it fails to execute， probably due to that DSolve has difficulty to handle interpolation function. Jan 16, 2013 at 15:38
• My suggestion is to use DSolve only once leaving voltageFunc unspecified. The next step is to substitute your voltageFunc into the general solution and then feed this to NMinimize. Jan 16, 2013 at 15:42
• I just tried to use the solution from DSolve. But it take even longer time to do the integral for interpolation function. It seems that the interpolation function is always pain for numerical analysis. I probably will switch to matlab or IGor by manually solving the ODE via Runge-Kutta method. Jan 17, 2013 at 0:43
• Could you post a sample of your data so I can have a try ? Jan 17, 2013 at 7:52
• Hi Firstly, thank you very much for your attention on my problem. Secondly, I feel myself stupid enough being not able to find any file upload button here. Can you leave me you email addr so that I can send data file and my NB file. Thanks Jan 17, 2013 at 19:49