{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 137 lecture oct 12

CS 137 lecture oct 12 - CS 137 lecture oct 12#include...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full Document Right Arrow Icon
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 for (i=0;i<iterations;i++){ m=(a+b)/2.0; fm=f(m); if(fabs(fm)<=epsilon) return m; if(fm>0.0) b=m; else a=m; int main(void) { printf(“%g/n”,bisection(-10,10,0.001,100000)); } takes 13 iterations, prints 0.738525
Background image of page 2
Background image of page 3

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

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

{[ snackBarMessage ]}