Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a short script to NDSolve a second-order nonlinear BVP, which as its output produces an InterpolatingFunction object. I would like to load in some data, and check how well this data fits the function by performing a Least-Mean-Squares sum.

I don't know how to go about this with a function represented by one of these InterpolatingFunction objects. Minimal(ish) code to produce the function is included below.

{r0, f0} = {1.349, 1.421}; {r1, f1} = {3.913, 0.044};
eqn = 2 H == f''[r]/((1 + f'[r]^2)^(3/2)) + (1/r) (f'[r]/(1 + f'[r]^2)^(1/2));
H = -0.021414;
F = First[f /. NDSolve[{eqn, f'[r0] == -1.127435, f[r1] == f1}, f, {r, r0, r1}]]

Any help would be appreciated.

share|improve this question
up vote 3 down vote accepted

I guess you mean something like this:

{r0, f0} = {1.349, 1.421}; {r1, f1} = {3.913, 0.044};
eqn = 2 h == f''[r]/((1 + f'[r]^2)^(3/2)) + (1/r) (f'[r]/(1 + f'[r]^2)^(1/2));
h = -0.021414;
ff = First[f /. NDSolve[{eqn, f'[r0] == -1.127435, f[r1] == f1}, f, {r, r0, r1}]];
data = {#, ff[#]} + {0, RandomReal[{-.1, .1}]} & /@  RandomReal[{r0, r1}, 30];
Show[Plot[ff[r], {r, r0, r1}, Evaluated -> True], ListPlot[data]]

Tr[(#[[2]] - ff[#[[1]]])^2 & /@ data]

Mathematica graphics


Of course Mathematica provides infinite ways for doing the same:

Norm[Differences /@ MapAt[ff, data, {All, 1}]] (*Sqrt of previous result*)

Norm[Subtract @@@ (data /. {x_, y_} -> {ff[x], y})]


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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