Lecture061 while loops - How do Calculators Computer Square...

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

View Full Document Right Arrow Icon
Lecture 1 -- 1 Computer Science I - Martin Hardwick How do Calculators Computer Square Roots? When you enter 29 into your calculator and push the square root button, how does the calculator compute ? Consider the function x 2 – 29 = 0. it crosses the x-axis when  x  =  so, to compute the        we need to  compute exactly where the function x 2  – 29 crosses the x-axis We will use an iterative method we know the function crosses the  x -axis somewhere between 0 and 29 compute the midpoint of the interval 0 to 29 decide whether the function crosses the  x -axis before the midpoint or  after the midpoint repeat for the half of the interval in which the function crosses the  x - axis do this until the interval is very small (0.00000001) – the midpoint of  the final interval is an approximation to the square root of 29 29 x x 2 – 29 -29 29 29 - 29 29
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture 1 -- 2 Computer Science I - Martin Hardwick Computing Square Roots (1) #include <iostream> using namespace std; double f(double x, double num) { return (x*x - num); } Goal – find the square root of a number using the iterative Bisection Method. root is where x 2  - number = 0 We need a function that computes x 2 – number every time that we need it this is the job of function f(x) Function f(x): has two arguments, which are  type double produces a result which is  type double
Background image of page 2
Lecture 1 -- 3 Computer Science I - Martin Hardwick Computing Square Roots (2) int main () { // number to find root of double num; // end points for search double left, right;  // mid point of interval double midpoint;  // error bound double epsilon; // size of interval double interval; // flag to terminate iteration bool done; // Get number to find root of
Background image of page 3

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

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

This note was uploaded on 03/30/2012 for the course CSCI 101 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 12

Lecture061 while loops - How do Calculators Computer Square...

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

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