Homework #11 |
Due: April 21, 2025 |
|
x Euler Exact ---------------------------
0.000000 0.000000 0.000000 0.100000 0.099833 0.091123 ..................... ..................... 1.500000 0.730990 0.712344 1.600000 0.862848 0.793431 1.700000 1.036497 1.012222 1.800000 1.257597 1.257597 1.900000 1.532184 1.532184 2.000000 1.866667 1.866667 |
plot "data.txt" using 1:2, title "Euler", "data.txt" using 1:3 title "Exact" |
/* Simpson's rule */ #include <stdio.h> #include <math.h> double f(double x) {return 4.0/(1.0+x*x);} int main() { int i, n ; double a=0.0, b=1.0 , h, s1=0.0, s2=0.0, s3=0.0, x; printf("Enter number of partitions (must be even) = "); scanf("%d", &n) ; h = (b-a)/(2.0*n) ; s1 = (f(a)+ f(b)); for (i=1; i<2*n; i=i+2) s2 = s2 + f(a + i*h); for (i=2; i<2*n; i=i+2) s3 = s3 + f(a + i*h); printf("%f\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ; return 0; } |
range(start, stop, step)where