CSCE 121:509-512Introduction to Program Design and Concepts, HonorsJ. Michael MooreSpring 2015Set 14: Plotting Functions and Data
1
CSCE 121:509-512 Set 14: Plotting Functions and Data
Overview of Chapter 15• Discusses graphing of (mathematical) functions and
data– Benefits of visualization– Pitfalls with floating-point approximations
• Introduces programming methodologies that are useful in doing so– typedef– Passing functions as arguments– Default arguments– Standard mathematical functions
• Graphics library as running example
2
CSCE 121:509-512 Set 14: Plotting Functions and Data
Visualization
• It’s very useful to plot graphs of functions and data
• You can learn things from a plot that are not evident in a set of numbers– Think about a sine curve
• Visualization of data is used in most research and business areas
• Can communicate large amounts of data simply
3
CSCE 121:509-512 Set 14: Plotting Functions and Data
Graphing Simple Functions• Suppose we want to display on the screen a plot of a
mathematical function, such as f(x) = 1, or f(x) = 2*x, or f(x) = x*x.
• We’d like to do this in a general way, so that we don’t have to write new code from scratch every time we want to plot a different function
• Try to pass in the (mathematical) function as an argument to a (C++) function!– Represent the mathematical function as (another) C++
function
4
CSCE 121:509-512 Set 14: Plotting Functions and Data
Calling a Function that Takes a Function as an Argument
• Our graphics library has a type of Shape called Function (name of a class)
• It takes the following arguments:– A (C++) function that takes one double argument and
returns a double: this calculates the mathematical function we want to plot on the screen
– Range of values over which the mathematical function is to be plotted
– Some other bookkeeping stuff for displaying the plot in the window
• After constructing a specific Function object, it can be attached to the window and displayed
5
CSCE 121:509-512 Set 14: Plotting Functions and Data
Specifying an Argument that is a Function
• We need a type for the argument that specifies the function to be plotted
• Use typedef keyword, which declares a new name for a typetypedef double Fct(double);
• Now Fct means “a function that takes a double argument and returns a double”
• Examples of objects of type Fct:double one(double x) { return 1;} // f(x) = 1
double slope(double x) { return x/2;} // f(x) = x/2double square(double x) { return x*x;} // f(x) = x*x
6
CSCE 121:509-512 Set 14: Plotting Functions and Data
Definition of Function Shapestruct Function : Shape {// constructor Function( Fct f, // f is a (C++) function that has one // double argument and returns a double /* more arguments for controlling the display of the plot of f in the window */ );/* rest of class description */};
7
CSCE 121:509-512 Set 14: Plotting Functions and Data
Using Function Shape
/* ... */Simple_window win(/* ... */);Function plot(slope, /* ... */); // remember slope was already definedwin.attach(plot);win.wait_for_button();/* ...*/
8
CSCE 121:509-512 Set 14: Plotting Functions and Data
Default Arguments
• The Function Shape constructor has seven arguments!– Too many– Asking for trouble, confusion and error
• Solution: provide default values for some of the arguments– Must be “trailing” arguments
9
CSCE 121:509-512 Set 14: Plotting Functions and Data
Default Arguments Examplestruct Function : Shape { Function( Fct f, double r1, double r2, Point xy, int count = 100, // default value of count is 100 double xscale = 25, // default value of xscale is 25 double yscale = 25); // default value of yscale is 25};/* ... */Function f1(sqrt,0,11,orig,100,25,25);Function f2(sqrt,0,11,orig,100); // same as f1
10
CSCE 121:509-512 Set 14: Plotting Functions and Data
Standard Mathematical Functions
Use #include <cmath> to get access toabs, ceil, floor, sqrt, cos, sin, tan, acos, asin, atan, sinh, cosh, tanh, exp, log, log10, pow, etc.
11
CSCE 121:509-512 Set 14: Plotting Functions and Data
Warning!• Floating point numbers are just
approximations of real numbers– Overflow– Underflow– Not precise enough for your needs– Small inaccuracies (rounding errors) can build up
into huge errors• Advice:
– Be suspicious about calculations– Check your results
12
CSCE 121:509-512 Set 14: Plotting Functions and Data
Acknowledgments
• Photo on slide 1: “Juhan’s 2008 Career Graph” by Juhan Sonin, licensed under CC BY 2.0
• Slides are based on those for the textbook:http://www.stroustrup.com/Programming/15_graphing.ppt
13