cosRoot

cosRoot - //than tolerance. //Arguments //xStart--left end...

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

View Full Document Right Arrow Icon
//============================================ //cosRoot.cpp //Finds the root of cosine between 1.5 and 1.6 radians. //============================================ #include  #include  using namespace std; //Declare functions double bisect(double xStart, double yStart, double tolerance, double f(double)); //==============>>main<<====================== int main() { cout.precision(14); // show 14 decimal places of resolution cout<<"The root of cosine between 1.5 and 1.6 is "; cout<<bisect(1.5,1.6,1e-6,cos)<<endl; }</bisect(1.5,1.6,1e-6,cos)<<endl; #include  using namespace std; //====================>> bisect << ============================ //Seeks root of function f(double r) using bisection //in the interval [xStart,yStart]. A root must be verified //in this intervalor the code will abort. Bisection proceeds //until the next root is bracketed by an interval of length of less
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //than tolerance. //Arguments //xStart--left end of initial interval //yStart--right end of initial interval //tolerance--maximum length of final interval //f--function whose root will be found //============================================================ = double bisect(double xStart, double yStart, double tolerance, double f(double)) { assert(tolerance>0.0); assert(xStart<ystart); assert(f(xStart)*f(yStart)<=0.0); double x=xStart;//point to left of root double y=yStart;//point to right of root while(y-x>tolerance) { //f(x)*f(y)<=0.0 double midPoint=0.5*(x+y); if(f(x)*f(midPoint)<=0.0) { y=midPoint; //shorten interval,maintain invariant } else { x=midPoint; //shorten interval , maintain invariant } } return 0.5*(x+y); }</ystart);...
View Full Document

This note was uploaded on 12/14/2011 for the course EE ??? taught by Professor Maybe during the Spring '10 term at USC.

Page1 / 2

cosRoot - //than tolerance. //Arguments //xStart--left end...

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

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