0
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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

0.245836

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})]

Etc.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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