CS 137 lecture oct 12

# CS 137 lecture oct - CS 137 lecture oct 12#include (section 23.3 gcc-1m double sin(double x double cos(double x ^takes radians double

This preview shows pages 1–3. Sign up to view the full content.

CS 137 lecture oct 12 #include <math.h> (section 23.3) gcc-1m double sin(double x); double cos(double x); ^takes radians double acos(double x); double asin(double x); double exp(double x); (e^x) double log(double x) ; (lnx) double log10(double x); log base 10 double pow(double x, double y);( x^y) double sqrt (double x); double fabs(double x); returns absolute value of the double. M_PI – constant pi M_E constant E f(x) = x-cosx f(0)=-1 f(5)=4.72… >0 f(2.5)=3.30. .>0 f(1 ¼)=0.935. .>0 f(5/8)=-0.186 <0 f(15/16)=-0.06 <0 rough idea -start with a, where f(a)>0

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

View Full Document
b, where f(b)>0 -compute the midpoint m=(a+b)/2 if (f(m)<0) a=m; else b=m; repeat until b-a<epsilon Bisection algorithm always works if f(x) is continuous #include <stdio.h> #include <math.h> #include <assert.h> double f(double x) { return x-cosx; } double bisection(double a, double b, double epsilon, int iterations) <-keep from running forever { int i; double m,fm; assert (f(a)<0 & f(b)>0 && epsilon >0 && iterations >0); see far board
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/10/2011 for the course CS 137 taught by Professor Clarke during the Fall '10 term at Waterloo.

### Page1 / 4

CS 137 lecture oct - CS 137 lecture oct 12#include (section 23.3 gcc-1m double sin(double x double cos(double x ^takes radians double

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

View Full Document
Ask a homework question - tutors are online