1 / 16

Chapter 06 (Part II)

Chapter 06 (Part II). Functions and an Introduction to Recursion. Objective. Write a program for learn C++ subfunction. Exercise: Please implement the following functions: double derivative(double a, double n, double x0); receives three parameters and returns the slope of ax n at x 0 .

qiana
Download Presentation

Chapter 06 (Part II)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 06 (Part II) Functions and an Introduction to Recursion

  2. Objective • Write a program for learn C++ subfunction. • Exercise: • Please implement the following functions: • double derivative(double a, double n, double x0); • receives three parameters and returns the slope of axn at x0. • double integral(double a, double n, double x1, double x2); • receives four parameters and returns the area enclosed by axn , y=0, x=x1 and x=x2.

  3. Derivative • To compute the slope S between (x1, y1) and (x2, y2): y (x2, y2) y2- y1 (x1, y1) x x2- x1 x1 x2

  4. Derivative • To compute the slope of the tangent line at x0: y f(x) x0 x0+dx x0+dx x0+dx x0+dx x

  5. Derivative • When dx approaches 0, then the slope of the tangent line at x0 is called the derivative of f(x) at x0. • We would use a very small dx to approximate the derivative.

  6. Implementation of derivative() • Program framework prototype implementation

  7. The #define Directive • You can use the #define directive to give a meaningful name to a constant in your program. • Example: • DX will be replaced by 0.0001 #include <iostream> #define DX 0.0001 int main () { cout << DX << endl; return 0; } #include <iostream> #define DX 0.0001 int main () { cout << DX << endl; return 0; } 0.0001

  8. int S Copy value Output argument Implementation of Subfunction • Remember the only way for a sub-function to communication with outside is through its parameters and return value! int int value 20 x 3 int value = x*x + 2*x + 5; x Parameter Return Value

  9. Implementation of derivative()

  10. Implementation of derivative() • What the sub-function can only access are its parameters and variables. • Note: do not declare a variable of the same variable name with parameters.

  11. Integral • How do we compute the area enclosed by axn , y=0, x=x1 and x=x2? y x x2 x1

  12. Integral • Use rectangles of the same width to cover the enclosed field and then sum up their area y f(x1+dx) f(x1) f(x1+2dx) f(x1+5dx) f(x1+3dx) f(x1+4dx) dx dx dx dx dx dx x x2 x1

  13. Integral • The measure of area is • The accuracy of area measure depends on how small dx is. y x x2 x1

  14. Integral • When dx approaches 0, we can approximate the area below f(x) between x1 and x2. • We denote as the integral of f(x) from x1 to x2.

  15. Implementation of integral() • Program framework

  16. Implementation of integral() • Use a very small value to represent dx. • Compute the area of rectangle by multiplying f(x) and dx. • Accumulate the area of rectangles. for (double i = x1; i <= x2; i += DX) { …… }

More Related