Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Plotting a curve with trigonometric functions

    I need to plot a curve as y = f(t), where t is the time, in the following way.
    y= 0 at t=-5
    - first interval t=-5 to t=0: y slightly increasing
    -second interval t=0 to t=5: y slightly increasing
    -third interval t=5 to t= 10: y increasing at an increasing rate
    -forth interval t=10 to t=15: y increasing at a decreasing rate
    -fifth interval (smaller) t=15 to t=18: y unchanged
    -sixth interval(smaller) t=18 to t=21: y decreasing at an increasing rate
    -seventh interval (smaller) t=21 to t=24: y decreasing at a decreasing rate

    I am unable to get the exact required shape.
    Please note that the t values for intervals are just to explain the shape so they can be different. I think this can better be done by using trigonometric function. Any help in this regard!

  • #2
    I doubt that a trigonometric function will help since - as best as I recall 50+ years out - you're not going to get a flat portion to deal with the 15-to-18 range.

    The output of help graph twoway lists a number of graph types, one of which may help you.
    Code:
        mspline               spline line plot
        lowess                LOWESS line plot
        lfit                  linear prediction plot
        qfit                  quadratic prediction plot
        fpfit                 fractional polynomial plot
    If you were to provide y values corresponding to the x values you give, someone on Statalist might be interested in providing example code. As your question now stands, many readers will be reluctant to spend much time on a problem so poorly specified.

    Comment


    • #3
      ThanksWilliam.
      I tried but maybe the question was clear enough. Here is the type of curve I need but I seek a better way to perform this task.

      Code:
      clear
      input t y
      -5 0
      -4 1
      -3 2
      -2 3
      -1 4
      0 5
      1 7
      2 9
      3 11
      4 13
      5 15
      6 18
      7 22
      8 27
      9 33
      10 40
      11 44
      12 47
      13 49
      14 50
      15 50
      16 50
      17 50
      18 49
      19 47
      20 44
      21 40
      22 35
      23 33
      24 31
      25 30
      
      end
      tsset t
      tssmooth ma y_ = y , window(2 1 2)
      line y_ t, ylabel("") yti("") xti("") xlabel("") ///
          graphregion(fcolor(white) lcolor(white)) aspect(0.3)

      Comment


      • #4
        I think you can improve on what you have by adjusting things a little more.

        Below is the code I used to play with the data, ultimately creating the plot points given by z. By overlaying the plot points on the line, I could better see the relationship of the line to the points. By including vertical lines at each of the x values where the curve changes, to help judge how the line is meeting expectations. I found that window(1,1,1) gave better results. Given that you said you wanted the curve unchanged between 15 and 18 I was surprised to see your data unchanging between 14 and 17.

        Code:
        clear all
        clear
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(t y z)
        -5  0  0
        -4  1  1
        -3  2  2
        -2  3  3
        -1  4  4
         0  5  5
         1  7  7
         2  9  9
         3 11 11
         4 13 13
         5 15 15
         6 18 18
         7 22 22
         8 27 27
         9 33 33
        10 40 40
        11 44 46
        12 47 51
        13 49 55
        14 50 58
        15 50 60
        16 50 60
        17 50 60
        18 49 60
        19 47 59
        20 44 56
        21 40 50
        22 35 44
        23 33 40
        24 31 37
        25 30 36
        end
        
        cls
        tsset t
        tssmooth ma y_ = y , window(1 1 1)
        line y_ t || scatter y t, xline(0 5 10 15 18 21 24) name(tss1) 
        
        tssmooth ma z_ = z , window(1 1 1)
        line z_ t || scatter z t, xline(0 5 10 15 18 21 24) name(tss2) 
        
        line y_ z_ t, xline(0 5 10 15 18 21 24) ///
            ylabel("") yti("") xti("") xlabel("") ///
            graphregion(fcolor(white) lcolor(white)) aspect(0.3)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	67.6 KB
ID:	1566301

        Comment

        Working...
        X