Document 11586316

advertisement
1.00 Tutorial 7
Agenda
• Administrative Issues (Quiz 2)
• Packaging Function in Objects
• Root Finding
Bisection, Secant, Newton
• Numerical Integration
Rectangular, Trapezoid, Simpson
Quiz 2
• Date: Class Time
• Quiz 2 Review: 1.5 hours
• Topics:
Lecture 11- Lecture 23 Steps in solving numerical problems
• Packaging functions as objects
write the function as a method that takes an
argument (the x) and return the mathematical
representation of the function inside of a class that
implements certain Function interface.
• In your main, call the root finding or integration
methods by passing your function objects and
other parameters.
Packaging Function in Objects
How do you represent functions such as:
1. Sin(x)+ Cos(x2)-0.78
2. x3+5x-9
3. x2+ 5y+z
?
Answer (1): Sin(x)+ Cos(x2)-0.78
public interface MathFunction {
public double f(double x);
}
class Func1 implements MathFunction {
public double f(double x) {
return Math.sin(x)+Math.cos(x*x)-0.78;}
}
Answer (2): x3+5x-9
//You will still be able to use the same interface
public interface MathFunction {
public double f(double x);
}
//You just need to create a new class with a different
“f method”
class Func2 implements MathFunction {
public double f(double x) {
return x*x*x+5x*x-9 ; }
}
Answer (3): x2+ 5y+z
• What are you going to do?
• Change the interface’s function so that it
can take 3 arguments
• Create a new class that implements the
interface…
Question ?
• Why do you need interfaces? Why not just directly
write your classes?
• How do you represent functions that will be used
for newton root finding?
• For instance: x3+5x-9 ?
• You need to change interface so there’s another
method to represent the derivative of the function
• Then just implements the new interface.
Answer (4)
public interface MathFunction2 {
public double fn(double x);
public double fd(double x);
}
class Func4 implements MathFunction2 {
public double fn(double x) {
return x*x*x +5*x- 9;
}
public double fd(double x) {
return 3*x*x + 5;
}
}
Root Finding – Exercise (1)
• Write a class using both bisection and newton
methods to find the root for the function:
x*x*x +5*x- 9
• Think about the structure first
– Packaging the function as object (as we talked about
before)
– Prepare your root finding methods (usually you want to
have a class that has all your root finding methods so
you can directly use them without any change)
– Call them in your main and print the result
Function preparation for root finding (1)
public interface MathFunction {
public double f(double x);
}
class Func1 implements MathFunction {
public double f(double x) {
return x*x*x +5*x- 9;}
}
Function Preparation for Root Finding Using Newton
public interface MathFunction2 {
public double fn(double x);
public double fd(double x);
}
class Func4 implements MathFunction2 {
public double fn(double x) {
return x*x*x +5*x- 9;
}
public double fd(double x) {
return 3*x*x + 5;
}
}
Wrap root finding methods in one class
class RootFinder {
public static final int JMAX= 40; // Maximum #of iterations
public static final double ERR_VAL= -10E10;
public static double rtbis(MathFunction func, double x1, double x2,
double xacc) { …}
public static double newt(MathFunction2 func, double a, double b,
double epsilon) { …}
}
Write your main class to use the methods
public class MyRootFinder {
public static void main(String[] args) {
double root= RootFinder.rtbis(new Func1(), -1.0, 8.0,
0.0001);
System.out.println("Root: (using bisection) " + root);
root= RootFinder.newt(new Func4(), -1.0, 8.0,
0.0001);
System.out.println("Root: (using newton) " + root);
System.exit(0);
}
}
Integration - Exercise (2)
(Same steps as root finding)
• Write a class using all three integration methods
for the function between the range 1 and 20,
interval 50:
x*x*x +5*x- 9
• Think about the structure first
– Packaging the function as object (as we talked about
before)
– Prepare your integration methods (usually you want to
have a class that has all your integration methods so
you can directly use them)
– Call them in your main and print the result
Wrap all your integration methods
class Integration {
public static double rect(MathFunction func, double
a, double b, int n) {…}
public static double trap(MathFunction func, double
a, double b, int n) {…}
public static double simp(MathFunction func, double
a, double b, int n) {…}
}
Write your main class
public class MyIntegration {
public static void main(String[] args) {
double r= Integration.rect(new Func1(), 1.0, 20.0, 50);
System.out.println("Rectangle: " + r);
double t= Integration.trap(new Func1(), 1.0, 20.0, 50);
System.out.println("Trapezoid: " + t);
double s=Integration.simp(new Func1(), 1.0, 20.0, 50);
System.out.println("Simpson: " + s);
System.exit(0);
}
}
Pset 7 – Problem 1
• What is the function? How do you pass
information such as L, N, M, S, P into your
function? (Can you use constructor to take
all these info?)
• What root finding method will you use?
• How do you call them?
Pset 7 - Problem 2
• What is the function? How do you represent
them in your program?
• What is your integration method?
• How do you call them?
Download