Announcement

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

  • Truncate Y-axis with twoway function graph

    Hi, folks. I've been using Stata for data management and analysis for quite a few years, but I've recently been exploring how I can use it (i.e., Stata 16.1) as an aid to teach some basic calculus concepts to my son. Toward that end, I've written the following code:

    Code:
    input float (x1 y1 x2 y2)
     1 1 3 9
     1 1 2 4
     1 1 1.5 2.25
     1 1 1.2 1.44
     1 1 1 1
     end
    
    * graph function, points, secant lines & tangent line
    graph twoway ///
        (scatter y2 x2) /// plot points
        (function y = x^2, range(0 3) aspectratio(1)) /// function f(x) = x^2
        (function y = -3 + 4*x, range (0 3)) /// secant line for (1,1) and (3,9)
        (function y =  -2 + 3*x, range(0 3))  /// secant line (1,1) and (2,4)
        (function y = -1 + 2*x, range(0 3)) /// tangent line for (1,1)
        , legend(off)
    Here's the resulting graph:
    function tangent and secant lines.gph

    My question is whether it's possible to truncate the range of the y variable in the functions, such that the resulting graph only display the various lines to the extent that they are above the y axis, so that it's easier to see what's going on. Were I graphing only variables, this would be a simple matter of using appropriate if statements, but so far as I've been able to figure out, this doesn't work for the "function" components of the graph twoway command (though, as shown, the range option allows this for the x axis). I'd very much appreciate any guidance that anyone is able to offer.

    Best,
    Marcus Britton
    Attached Files

  • #2
    I don't know any less than pedestrian method here That's just invent a dataset directly. The apparently little used (little known?) command range does help.

    Code:
    clear 
    set obs 101 
    range x 0 3 
    gen y1 = x^2 
    label var y1 "x{sup:2}"
    gen y2 = -3 * 4*x 
    label var y2 "-3 + 4x"
    
    line y1 x if y1 < 5 || line y2 x if y2 < 5

    Comment

    Working...
    X