Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm working on a sales projection, trying to create some flexibility in handle the forecast using locators.

The toy code is something like this:

DynamicModule[{pts={{1,1},{2,1}}}
    ,LocatorPane[
        Dynamic[pts]
       ,Dynamic@Plot[Piecewise[{{InterpolatingPolynomial[{{0,1},Sequence@@pts,{3,5}},x],x<3},{5,x>3}}],{x,0,5},PlotRange->{{0,5},{0,6}}]
    ]
]

plot

Where I can change the locators position in order to change the curve.

The question is, assuming my plot functions as f[x]

  1. How to define f'[3]=0 to don't get the slop break in {3,5}?
  2. How to define f'[0]>0 to don't get a initial down slope?

So I could control the polinomial order to get these conditions met, still using locators.

Maybe using NDSolve together with LinearModelFit? Or is there a simpler way?

Update

The first condition was solved by @kguler and @belisarius. Tks to both.

Some clue for the second?

share|improve this question
1  
For (1) you can use {3, 5, 0} instead of {3,5}. For (20, changing the first point {0,1} to {0,1,1} controls the slope at x=0 but, it seems, patterns like {0,1,_Positive} don't work.. –  kguler Jan 24 '13 at 1:47
    
see my edited answer –  belisarius Jan 24 '13 at 1:59
add comment

3 Answers 3

up vote 3 down vote accepted

add 0 valued derivatives at {3,5} and a slider for controlling the derivative at 0:

DynamicModule[{pts = {{1, 1}, {2, 1}}, d},
 Column@{
   Slider[Dynamic[d], {0, 3}],
   LocatorPane[Dynamic[pts],
    Dynamic@
     Plot[Piecewise[{{InterpolatingPolynomial[{{0, 1, d, 0}, 
           Sequence @@ pts, {3, 5, 0, 0, 0}}, x], x < 3}, {5, x > 3}}], 
         {x, 0, 5}, PlotRange -> {{0, 5}, {0, 6}}]]}]

Mathematica graphics

Edit

To get a positive derivative at x=0, you may also dynamically add more locators like this:

DynamicModule[{pts = {{1, 1}, {2, 1}}}, 
 LocatorPane[Dynamic[pts], 
  Dynamic@Plot[Piecewise[{{InterpolatingPolynomial[{{0, 1}, 
      Sequence @@ Sort@pts, {3, 5, 0, 0, 0}}, x], x < 3}, {5, x > 3}}], {x, 0, 5}, 
          PlotRange -> {{0, 5}, {0, 6}}],  LocatorAutoCreate -> True]]

Mathematica graphics

share|improve this answer
    
Very interesting and simple!.. I get a even better effect with {3, 5, 0, 0} tks +1 –  Murta Jan 24 '13 at 1:52
add comment

A possible way by using one curve to get the derivative, then substituting into another one:

DynamicModule[{pts = {{1, 1}, {2, 1}}, func},
 LocatorPane[Dynamic[pts],
  Dynamic[
   Plot[{
     Piecewise[{{(func = Interpolation[{
             {{0}, 1},
             {{pts[[1, 1]]}, pts[[1, 2]]},
             {{pts[[2, 1]]}, pts[[2, 2]]},
             {{3}, 5, 0}
             }])[x], x < 3}, {5, x > 3}}],
     Piecewise[{{Interpolation[{
           {{0}, 1, If[# < 0, 0, #] &[func'[0]]},
           {{pts[[1, 1]]}, pts[[1, 2]], func'[pts[[1, 1]]]},
           {{pts[[2, 1]]}, pts[[2, 2]], func'[pts[[2, 1]]]},
           {{3}, 5, 0}
           }][x], x < 3}, {5, x > 3}}]
     }, {x, 0, 5}, PlotRange -> {{0, 5}, {0, 6}}, 
    PlotStyle -> {GrayLevel[.5], Red}]
   ]]]

Mathematica graphics

For real applications, only plot the curve needed:

DynamicModule[{pts = {{1, 1}, {2, 1}}, func},
 LocatorPane[Dynamic[pts],
  DynamicWrapper[
   Dynamic@Plot[Piecewise[{{Interpolation[{
           {{0}, 1, If[# < 0, 0, #] &[func'[0]]},
           {{pts[[1, 1]]}, pts[[1, 2]], func'[pts[[1, 1]]]},
           {{pts[[2, 1]]}, pts[[2, 2]], func'[pts[[2, 1]]]},
           {{3}, 5, 0}
           }][x], x < 3}, {5, x > 3}}], {x, 0, 5}, 
     PlotRange -> {{0, 5}, {0, 6}} ],
   func = Interpolation[{
      {{0}, 1},
      {{pts[[1, 1]]}, pts[[1, 2]]},
      {{pts[[2, 1]]}, pts[[2, 2]]},
      {{3}, 5, 0}
      }]
   ]]]

Mathematica graphics

share|improve this answer
add comment

Adding values for the first, second, third... derivatives at x=0 and x=3:

 DynamicModule[{pts = {{1, 1}, {2, 1}}}, 
  LocatorPane[Dynamic[pts], Dynamic@Plot[
   Piecewise[{{InterpolatingPolynomial[{{0, 1, 0, 1}, 
     Sequence @@ pts, {3, 5, 0, 1}}, x], x < 3}, {5, x > 3}}],
     {x, 0, 5}, PlotRange -> {{0, 5}, {0, 6}}]]]

enter image description here
ref/Interpolating Ploynomial

Update: Tie the first/second/... derivatives at x=0 and x=3 to the locator values; for example:

DynamicModule[{pts = {{1, 1}, {2, 1}}}, 
LocatorPane[Dynamic[pts],  Dynamic@Plot[
Piecewise[{{InterpolatingPolynomial[{{0, 1, 0, Abs@pts[[1, 2]]}, 
     Sequence @@ pts, {3, 5, 0, -Abs[pts[[2, 2]]]}}, x],   x < 3},
   {5, x > 3}}], {x, 0, 5}, PlotRange -> {{0, 5}, {0, 6}}]]] 

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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