HW #02 |
Due: 09/13/2023 |
Note: If you have not registered on the web, your hw paper is NOT graded. |
|
|
#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, s=0.0 , x ; n=100; h = (b-a)/n ; for (i= 0;i<n;i++) s = s + f(a + i*h) ; s=s*h ; printf("Result =%lf\n", s) ; return 0; } |
f = @(x) 4/(1+x^2); a=0;b=1;s=0; %n=input('Enter n='); n=100 h=(b-a)/n; for i=0:1:n-1 s=s+f(a+i*h); end; s=s*h; fprintf('%f\n', s); |
/* Trapezoidal 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, s=0.0, x; /* printf("Enter number of partitions = "); scanf("%d", &n) ; */ n=10; h = (b-a)/n ; for (i=1;i<=n-1;i++) s = s + f(a + i*h); s=h/2*(f(a)+f(b))+ h* s; printf("%20.12f\n", s) ; return 0; } |
f = @(x) 4/(1+x^2); a=0;b=1;s=0; n=10; %n=input('Enter n='); h=(b-a)/n; for i=1:1:n-1 s=s+f(a+i*h); end; s=h/2*(f(a)+f(b))+h*s; fprintf('%f\n', s); |