16
Functions and an Introduction to Recursion

Chapter 06 (Part II)

  • Upload
    qiana

  • View
    29

  • Download
    1

Embed Size (px)

DESCRIPTION

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 . - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 06 (Part II)

Functions and an Introduction to Recursion

Page 2: Chapter 06 (Part II)

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.

Page 3: Chapter 06 (Part II)

To compute the slope S between (x1, y1) and (x2, y2):

12

12

xx

yyS

x

y

x2x1

(x1, y1)

(x2, y2)

y2- y1

x2- x1

Page 4: Chapter 06 (Part II)

To compute the slope of the tangent line at x0:

x0 x

y

x0+dxx0+dxx0+dxx0+dx

f(x)

Page 5: Chapter 06 (Part II)

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.

dx

xfdxxf

xdxx

xfdxxfxf

dxdx

)()(lim

)(

)()(lim)(' 00

000

00

00

Page 6: Chapter 06 (Part II)

Program framework

prototype

implementation

Page 7: Chapter 06 (Part II)

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

Page 8: Chapter 06 (Part II)

Remember the only way for a sub-function to communication with outside is through its parameters and return value!

Copy value Outputx

Parameter

int int

value

Return Value

S

argument

int

x3 int value = x*x + 2*x + 5; 20

Page 9: Chapter 06 (Part II)
Page 10: Chapter 06 (Part II)

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.

Page 11: Chapter 06 (Part II)

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

x

y

x1x2

Page 12: Chapter 06 (Part II)

Use rectangles of the same width to cover the enclosed field and then sum up their area

x

y

x1x2

dx dx dx dx dx dx

f(x1+dx)

f(x1

)f(x1+2dx

) f(x1+3dx)f(x1+4dx

)

f(x1+5dx)

Page 13: Chapter 06 (Part II)

The measure of area is

◦ The accuracy of area measure depends on how small dx is.

x

y

x1x2

2111 ,...,2,, where)( xxdxxdxxxxdxxfAx

Page 14: Chapter 06 (Part II)

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.

xdx

x

xdxxfdxxf )(lim)(

0

2

1

Page 15: Chapter 06 (Part II)

Program framework

Page 16: Chapter 06 (Part II)

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){

……}