0
$\begingroup$

I use a NonlinearFitModel operation for fitting the piecewised data, and then want to use the result of operation as a function. However, it doesn't work. Could anyone please tell me where can be a problem?

Example:

data = {{0.1, 1}, {0.2, 2}, {0.3, 34}, {0.4, 38}, {0.5, 42}, {0.6, 
   41}, {0.7, 98}, {0.8, 3}, {0.9, 32}, {1.0, 2}}
fit = Piecewise[{{NonlinearModelFit[Select[data, #[[1]] <= 0.5 &], 
     a + b*M + c*M^0.5, {a, b, c}, M], 
    M <= 0.5}, {NonlinearModelFit[Select[data, #[[1]] > 0.5 &], 
     a + b*M + c*M^0.5, {a, b, c}, M], M > 0.5}}]
fit[2]
$\endgroup$

1 Answer 1

2
$\begingroup$

You forgot to add M as argument to fit and you need to write Normal[NonlinearModelFit[...]] to extract the fitted formula.

fit[M_] = Piecewise[{{Normal@NonlinearModelFit[Select[data, #[[1]] <= 0.5 &], 
       a + b*M + c*M^0.5, {a, b, c}, M], M <= 0.5}, {Normal@NonlinearModelFit[
    Select[data, #[[1]] > 0.5 &], a + b*M + c*M^0.5, {a, b, c}, M], M > 0.5}}]
fit[2]

-487.93843

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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