Message Boards Message Boards

Fit a Piecewise function.

I am trying NonlinearModelFit on a piecewise function but it is showing error, I don't know why. Can anyone help?

Attachments:
3 Replies
Posted 4 years ago

Your model is overparameterized. You are trying to fit 5 parameters when the model only requires just 3 parameters. Why do I say that?

Here's your basic function:

f = gamma*(Abs[H0] Cos[Pi/180*(Phi - PhiH)] +
      4 Pi Ms + 1/2 H1*(3 + Cos[Pi/180*4 Phi]) -
      2 Hu (Cos[Pi/180*(Phi - Phiu)])^2)^0.5*
   (Abs[H0] Cos[Pi/180*(Phi - PhiH)] + 2 H1 Cos[Pi/180*4 Phi] -
      2 Hu Cos[Pi/180*2*(Phi - Phiu)])^0.5;

Now consider some substitutions:

constants = {Cos[Pi/180*(Phi - PhiH)] -> c1,
    4 Ms \[Pi] + 1/2 H1 (3 + Cos[(Phi \[Pi])/45]) - 2 Hu Cos[1/180 (Phi - Phiu) \[Pi]]^2 -> c2,
   2 H1 Cos[(Phi \[Pi])/45] - 2 Hu Cos[1/90 (Phi - Phiu) \[Pi]] -> c3};

Make those substitutions:

f//.constants
(* 0.002802 (c2 + c1 Abs[H0])^0.5 (c3 + c1 Abs[H0])^0.5 *)

We see that the model can be characterized by just 3 parameters. This has nothing to do with the data or NonlinearModelFit.

POSTED BY: Jim Baldwin
Posted 4 years ago

There are a few "odd" things about your code. The first part of the code up to the ClearAll statement is repeated. That's inconsequential.

The Piecewise function has the same two arguments for H0<0 and H0>=0. (So there's no need for Piecewise.)

The resulting correlation matrix

nlmd["CorrelationMatrix"] // MatrixForm

$$\left( \begin{array}{ccccc} 1. & 0.949382 & -0.780545 & -0.278255 & 0.806957 \\ 0.949382 & 1. & -0.937394 & 0.0375483 & 0.951635 \\ -0.780545 & -0.937394 & 1. & -0.383222 & -0.999056 \\ -0.278255 & 0.0375483 & -0.383222 & 1. & 0.342746 \\ 0.806957 & 0.951635 & -0.999056 & 0.342746 & 1. \\ \end{array} \right)$$

suggests that the model is not parameterized well in that the estimators for Phi and Phiu are near perfectly correlated. That is also suggested with the estimates of those two parameters being identical to their starting values.

So I think that something if off with the model.

POSTED BY: Jim Baldwin

The functions are the same because I want to first check the applicability for the known result. I already did this fitting using this function: g[H0, H1, Ms, Phi, Hu, Phiu] :=gamma(Abs[H0] Cos[Pi/180(Phi - PhiH)] + 4 Pi Ms + 1/2 H1(3 + Cos[Pi/1804 Phi]) - 2 Hu (Cos[Pi/180(Phi - Phiu)])^2)^0.5(Abs[H0] Cos[ Pi/180(Phi - PhiH)] + 2 H1 Cos[Pi/1804 Phi] - 2 Hu Cos[Pi/1802(Phi - Phiu)])^0.5 and had a good fitting value. Same code, same initial values. Just added the piecewise function. Please have a look.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract