lecture19

lecture19 - 1.00 Lecture 19 October 24, 2005 Numerical...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1.00 Lecture 19 October 24, 2005 Numerical Methods: Root Finding Remember Java Data Types Size Type (bits) Range byte 8 -128 to 127 short 16 -32,768 to 32,767 int 32 -2,147,483,648 to 2,147,483,647 long 64 -9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L float 32 +/- 3.4E+38F (6-7 significant digits) double 64 +/- 1.8E+308 (15 significant digits) char 16 ISO Unicode character set boolean 1 true or false 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Remember Numerical Problems Problem Integer Float, double Zero divide (overflow) Exception thrown. POSITIVE_INFINITY, Program crashes NEGATIVE_INFINITY unless caught. 0/0 Exception thrown. NaN (not a number) Program crashes unless caught Overflow No warning. Program POSITIVE_INFINITY, gives wrong results. NEGATIVE_INFINITY Underflow Not possible No warning, set to 0 Rounding, Not possible No warning. Program accumulation errors gives wrong results. Common, “bad news” cases Packaging Functions in Objects Consider writing method that finds root of function or evaluates a function, e.g., f(x)= 0 on some interval [a, b], or find f(c) General method that does this should have f(x) as an argument Can’t pass functions in Java Wrap the function in an object instead Then pass the object to the root finding or evaluation method as an argument Define an interface that describes the object that will be passed to the numerical method 2
Background image of page 2
“Function Passing” Example // Scope is public, so it must be in its own file public interface MathFunction { public double f(double x); } // Place this class its own file // FuncA implements the interface class funcA implements MathFunction { public double f(double x) { return x*x - 2; } } Exercise: Passing Functions Write interface MathFunction2 in its own file public interface MathFunction2 { public double f(double x1, double x2); } In a second file, write a class that implements the interface for the 3 function 5x 1 2 + 2x 2 class FuncB implements MathFunction2 { … } 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exercise (continued) Write a method to evaluate functions that takes a MathFunction2 object and two doubles as arguments evalFunc( ) returns true if f >= 0, false otherwise It’s convenient to make evalFunc( ) static class Evaluator { boolean evalFunc(MathFunction2 func, double d1,double d2){…} Write a main(), in Evaluator or outside it, that: Invokes Evaluator, passing a FuncB object to it, and Outputs the value of the function at x 1 =2 and x 2 =-3 General Interface for multivariate functions public interface MathFunctionN { public double f(double xArray[]); //xArray.length provides the number of // variables in f(x) } 4
Background image of page 4
New in Java 5.0 – Variable argument lists public interface MathFunctionN { public double f(double… xval); } This allows calls of the form f(3.0, 2.0, 1.0) that the complier converts in to array with as many members as there are parameters public class TestMathFunctions {
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 23

lecture19 - 1.00 Lecture 19 October 24, 2005 Numerical...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online