Group Abstract Group Abstract

Message Boards Message Boards

Nonlinearmodelfit with conditionals - how to.

Posted 10 years ago

Hey guys I am trying to adapt a SAS code to mathematica, but I am experiencing some problems in doing that. I tried to run a model, but wasn't sure how to insert some conditionals.

SAS code:

proc nlin method = newton;
parms a=1 b=10^6 d=2.8;
If x > a then do;
Model S=0;
end;
else
if x < b then do;
model S =1;
end;
else
model S = (x^(d - 3) - b^(d - 3))/(a^(d - 3) - b^(d - 3));
end;
run;

My first mathematica model, without using the conditionals (x > a;x < b)

data = {{0.1 , 1},{0.5 , 0.99245283},{1 , 0.981132075},{2 , 0.957629013},{3 , 0.94647978},{5 , 0.892872862},{8 , 0.825362586},{10 , 0.794884307},{15 , 0.737820069},{30 , 0.688456089},{50 , 0.710852149},{100 , 0.686350949},{300 , 0.630188679},{500 , 0.622243133},{1500 , 0.61197978}};

model = (x^(d - 3) - b^(d - 3))/(a^(d - 3) - b^(d - 3));

nlm = NonlinearModelFit[data,model, {{a, 1}, {b, 10^6}, {d, 2.8}}, x,Method -> "Newton"]
POSTED BY: André Reis
9 Replies
Posted 10 years ago
POSTED BY: Jim Baldwin
Posted 10 years ago

No. Actually not. I got this code in a scientific paper, so that I am trying to contact the author to see what's wrong with the code. I am not sure.

POSTED BY: André Reis
Posted 10 years ago

Your SAS code has several syntax errors. "^" should be "**", "b=10^6" should be "b=10E6", and the last "end;" generates an error as it is not needed. Those errors seem a bit more than cut-and-paste issues. Does the code really run in SAS?

POSTED BY: Jim Baldwin
Posted 10 years ago

I see. Well I was trying to use Piecewise function, but as I started working with mathematica last week, I don't know how to use yet. Thank you again..

POSTED BY: André Reis

I am no expert. It may have something to do with the fact that your model is not differentiable. Sorry I can't help you any further. Here is a code that gives an output, even though with a warning:

data = {{0.1, 1}, {0.5, 0.99245283}, {1, 0.981132075}, {2, 
    0.957629013}, {3, 0.94647978}, {5, 0.892872862}, {8, 
    0.825362586}, {10, 0.794884307}, {15, 0.737820069}, {30, 
    0.688456089}, {50, 0.710852149}, {100, 0.686350949}, {300, 
    0.630188679}, {500, 0.622243133}, {1500, 0.61197978}};
model = Piecewise[{{1, x < a}, {0, 
     x > b}, {(x^(d - 3) - b^(d - 3))/(a^(d - 3) - b^(d - 3)), True}}];
nlm = NonlinearModelFit[data, model, {{a, 1}, {b, 10^6}, {d, 2.8}}, x]
POSTED BY: Gianluca Gorni
Posted 10 years ago

I am not sure if a explained it in an understandable way. Well I tried to use this model by using LevenbergMarquardt method and it worked, wasn't the same when tried Newton method. Some specific reason why it didn't work?

Thank you anyway.

POSTED BY: André Reis
Posted 10 years ago
POSTED BY: André Reis

Are you sure your conditions are x>a or x<b, and not a>b or x<a? That would make more sense.

POSTED BY: Gianluca Gorni

The following gives a result with a warning:

Clear[x, a, b, d];
data = {{0.1, 1}, {0.5, 0.99245283}, {1, 0.981132075}, {2, 
    0.957629013}, {3, 0.94647978}, {5, 0.892872862}, {8, 
    0.825362586}, {10, 0.794884307}, {15, 0.737820069}, {30, 
    0.688456089}, {50, 0.710852149}, {100, 0.686350949}, {300, 
    0.630188679}, {500, 0.622243133}, {1500, 0.61197978}};
model = Piecewise[{{0, x > a}, {1, 
     x < b}, {(x^(d - 3) - b^(d - 3))/(a^(d - 3) - b^(d - 3)), True}}];
nlm = NonlinearModelFit[data, model, {{a, 1}, {b, 10^6}, {d, 2.8}}, x]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard