csc 1051 – finite differences dr. joel hirsh csc 1051 m.a. papalaskari, villanova university...

7
CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

Upload: kory-mason

Post on 02-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 – Finite Differences

Dr. Joel Hirsh

CSC 1051 M.A. Papalaskari, Villanova University

Optional Project

Page 2: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Description• Derivatives or slopes of a function are used in

many numerical simulations:– Numerical weather prediction– Aircraft design – Financial modeling.

• These derivatives need to be computed using some type of approximation. One of the most commonly used approximations is the finite difference. There are many types of finite difference approximations. In this project, we will use two of the simplest:– Forward differences – Central differences.

Page 3: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Definitions• Given a function of a single variable, x, let f(x) be

some single valued function of x, e.g., the trigonometric function sin(x). Let the derivative of f(x) be f’(x). For some small interval, h,

• The forward difference approximation is

• The central difference approximation is

Page 4: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Errors• If f’(x) is well known then the error in the two

methods can be expressed as

 • efd(x) = fd(x) – f’(x) = O(h)

 • ecd(x) = cd(x) – f’(x) = O(h2)• Where O(y) means that the quantity goes to zero in

limit as y goes to zero.

Page 5: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Assignment• Write a Java program using the function f(x) =

sin(x). Input a value of x. Evaluate the function, and both types of finite difference methods over a range of values for h, start with h = 0.1 and half the value of h 8 times. For each values of h, print the function value, the exact derivative (the exact derivative of sin(x) is cos(x)), the two finite differences and the two errors. Your output should be a table and look like

Page 6: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Sample Output

Page 7: CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project

CSC 1051 M.A. Papalaskari, Villanova University

Excel Plot of Sample Output0.0010.010.11

1.00E-06

1.00E-05

1.00E-04

1.00E-03

1.00E-02

1.00E-01

1.00E+00

for err

cen err

h

erro

r