CSCE 121:509-512 Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 14: Plotting Functions and Data 1 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 2 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 3 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 4 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 5 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 type typedef 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/2 double square(double x) { return x*x;} // f(x) = x*x CSCE 121:509-512 Set 14: Plotting Functions and Data 6 Definition of Function Shape struct 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 */ }; CSCE 121:509-512 Set 14: Plotting Functions and Data 7 Using Function Shape /* ... */ Simple_window win(/* ... */); Function plot(slope, /* ... */); // remember slope was already defined win.attach(plot); win.wait_for_button(); /* ...*/ CSCE 121:509-512 Set 14: Plotting Functions and Data 8 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 9 Default Arguments Example struct 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 10 Standard Mathematical Functions Use #include <cmath> to get access to abs, ceil, floor, sqrt, cos, sin, tan, acos, asin, atan, sinh, cosh, tanh, exp, log, log10, pow, etc. CSCE 121:509-512 Set 14: Plotting Functions and Data 11 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 12 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 CSCE 121:509-512 Set 14: Plotting Functions and Data 13