# Fit a Piecewise function.

**URL:** <https://community.wolfram.com/t/fit-a-piecewise-function/16022>\
**Category:** Questions\
**Tags:** equation-solving, graphics-and-visualization, modeling\
**Created:** [May 18, 2020, 3:46pm UTC](https://community.wolfram.com/t/fit-a-piecewise-function/16022 "2020-05-18T15:46:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomal](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/tomal/32/16254_2.png) [@tomal](https://community.wolfram.com/u/tomal)\
**Post date:** [May 18, 2020, 3:46pm UTC](https://community.wolfram.com/t/fit-a-piecewise-function/16022/1 "2020-05-18T15:46:06Z")

</div>

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

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/cfac31a8-fe63-492e-9072-cf940b25224a)

[Tomal-help.nb](https://community.wolfram.com/uploads/short-url/oNgL9XIr5dZ0JSEmKoOAAxfel1I.nb) (13.1 KB)

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [May 18, 2020, 7:59pm UTC](https://community.wolfram.com/t/fit-a-piecewise-function/16022/2 "2020-05-18T19:59:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomal](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/tomal/32/16254_2.png) [@tomal](https://community.wolfram.com/u/tomal)\
**Post date:** [May 22, 2020, 4:06pm UTC](https://community.wolfram.com/t/fit-a-piecewise-function/16022/3 "2020-05-22T16:06:54Z")

</div>

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/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  
and had a good fitting value. Same code, same initial values. Just added the piecewise function. Please have a look.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/39ed1323-7e1a-46dd-bfa3-f028259e1070)

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [May 22, 2020, 5:27pm UTC](https://community.wolfram.com/t/fit-a-piecewise-function/16022/4 "2020-05-22T17:27:18Z")

</div>

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`.
