# FindMinimum error related to NDSolve

**URL:** <https://community.wolfram.com/t/findminimum-error-related-to-ndsolve/23184>\
**Category:** Questions\
**Tags:** data-science, calculus, equation-solving, numerical-computation, statistics-and-probability\
**Created:** [December 3, 2023, 3:58am UTC](https://community.wolfram.com/t/findminimum-error-related-to-ndsolve/23184 "2023-12-03T03:58:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malvam](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@malvam](https://community.wolfram.com/u/malvam)\
**Post date:** [December 3, 2023, 3:58am UTC](https://community.wolfram.com/t/findminimum-error-related-to-ndsolve/23184/1 "2023-12-03T03:58:01Z")

</div>

I’m trying to fit some data into a model using the standard procedure of minimizing the chi-square function using this code:

```
ClearAll["Global`*"]
Needs["ErrorBarPlots`"];
a0 = 1;
b0 = 2;

sol[a_, b_] := 
NDSolve[{x'[t] == -a*x[t] + Sin[t], y'[t] == a*x[t] - b*y[t]^2, 
z'[t] == b*y[t] - Cos[t], x[0] == 1, y[0] == 0, z[0] == 0}, {x, y,
z}, {t, 0, 10}, MaxSteps -> Infinity];

F[t_] := x[t] + y[t]^2 + z[t]^3;

wsol[a_, 
b_] := (wsol[a, b] = 
NDSolve[{w'[t] - w[t] - ((F[t] /. sol[a, b])[[1]]) == 0, 
w[0] == 0}, w, {t, 0, 10}]);

model[t_, a_, b_] := (w[t] /. wsol[a, b])[[1]] // Chop;

SeedRandom[1264645]; 
rangoT = Range[0, 9, 0.5];
ndat = Length[rangoT];

DATA = Table[{t, 
model[t, a0, b0] + RandomVariate[NormalDistribution[0, .1]], 
RandomReal[{.05, .1}]}, {t, rangoT}];

chi2[a_, b_] := 
Sum[((DATA[[i, 2]] - model[DATA[[i, 1]], a, b])/DATA[[i, 3]])^2, {i, 
1, ndat}]

FindMinimum[chi2[a, b], {w, 0.9}, {b, 1.1}]

```

But when I run it I get error messages from the FindMinimum command related to NDSolve and ReplaceAll. Everything runs just fine until using that command. I was wondering why’s not working and what should be changed.

[fit\_test\_1.nb](https://community.wolfram.com/uploads/short-url/jaQNB5ytBxRc9cYCDllItK6P5VM.nb) (27.1 KB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [December 3, 2023, 12:32pm UTC](https://community.wolfram.com/t/findminimum-error-related-to-ndsolve/23184/2 "2023-12-03T12:32:00Z")

</div>

The solutions to your first differential equation escape at infinity in finite time, and this time depends on the parameters `a,b`. However your functions `rangoT, DATA` and `chi2` seem to assume that all your solutions are defined on the same interval `[0,9]`.
