/************************************************************** A.Treibergs Jan. 23, 2006 Solution to problem 1A Sum the series S = 1/e^1 + 2/e^2 + 3/e^3 +... **************************************************************/ /* The nth term is a_n = n exp(-n) = f(n). The terms are all positive and decreasing (i.e. a_n > a_{n+1}) because f' = (1-x)exp(-x) which is nonpositive for x >= 1. Let S_n denote the partial sum S_n = 1/e^1+2/e^2+...+ n/e^n. The nth error is R_n = S-S_n = (n+1)/e^(n+1) + (n+2)/e^(n+2) + ... Because the terms are decreasing and positive, we may apply the integral estimate for the error: Integrating from n to infinity, |R_n| < \int x exp(-x) dx = (n+1)exp(-n) = g(n). Thus S_n approximates S to an accuracy of six decimal places, or within 0.5e-6 if g(n)<0.5e-6. Using a calculator, we find g(18) = 2.89e-7 < 0.5e-6 < g(17) = 7.45e-7. Thus the desired accuracy is achieved for a partial sum with n=18 terms. */ #include #include #include int main( void ) { double sum=0.0; int i, n=18; for ( i=1 ; i <= n ; i=i+1 ) { sum = sum + i/exp(i); } printf (" To six decimal places, the sum of the series\n"); printf (" 1/e + 2/e^2 + 3/e^3 + ... is %lf\n", sum); return EXIT_SUCCESS; } /************************************************************** A.Treibergs Jan. 23, 2006 Solution to problem 1B Sum the series S = 1/e^1 - 2/e^2 + 3/e^3 -... **************************************************************/ /* This is an alternating series S = a_1 - a_2 + a_3 - a_4 + a_5 - ... The nth term is a_n = n exp(-n) = f(n). The terms are all positive and decreasing (i.e. a_n > a_{n+1}) because f' = (1-x)exp(-x) which is nonpositive for x >= 1. Let S_n denote the partial sum S_n = 1/e^1-2/e^2+...-(-1)^n n/e^n. Because the terms a_n are decreasing positive, we may apply Leibnitz's estimate for the error of an alternating series: the error is less than the first term neglected. Thus |R_n| = |S - S_n| <= a_(n+1) = (n+1) exp(-n-1) = h(n). Thus S_n approximates S to an accuracy of six decimal places, or within 0.5e-6 if h(n)<0.5e-6. Using a calculator, we find h(17) = 2.74e-7 < 0.5e-6 < g(16) = 7.04e-7e-7. Thus the desired accuracy is achieved for a partial sum with n=17 terms. Compared to problem 1A, this sum takes fewer terms.*/ #include #include #include int main( void ) { double sum=0.0, termsign=1.0; int i, n=17; for ( i=1 ; i <= n ; i=i+1 ) { sum = sum + termsign*i/exp(i); termsign = -termsign; } printf (" To six decimal places, the sum of the series\n"); printf (" 1/e - 2/e^2 + 3/e^3 - ... is %lf\n", sum); return EXIT_SUCCESS; } /************************************************************** A.Treibergs Jan. 23, 2006 Solution to problem 1C Input k and print a table of n vs y_n= (1+k/n)^n n runs from 1 to 10, then to 100 by steps of 10, then to 1000 by steps of 100, then to 10000 by steps of 1000. **************************************************************/ /* To see the limit, because log is continuous, ln(L) = lim ln(y_n). n->\infty But ln(y_n) = n ln(1 + k/n) = k ln( 1 + k/n)/(k/n). Thus ln(L) = lim k ln( 1 + k/n)/(k/n). n->\infty Let x = k/n. Then x -> 0 as n -> \infty. ln(L) = lim k ln( 1 + x)/x = lim k / (1 + x) = k, x->0 x->0 by l'Hopital's rule. Finally L = exp(k). */ #include #include #include int main( void ) { double k=0.0; int n=1; printf( "Enter the value of k: " ); scanf("%lf", &k); printf( " n \t\t (1+k/n)^n \n"); printf( "-------\t\t\t-----------------\n"); for(n=1; n <= 10 ; n++) { printf("%7d\t\t %21.15lf\n", n, pow(1.0+k/n,n) ); } for(n=20; n <= 100 ; n=n+10) { printf("%7d\t\t %21.15lf\n", n, pow(1.0+k/n,n) ); } for(n=200; n <= 1000 ; n=n+100) { printf("%7d\t\t %21.15lf\n", n, pow(1.0+k/n,n) ); } for(n=2000; n <= 10000 ; n=n+1000) { printf("%7d\t\t %21.15lf\n", n, pow(1.0+k/n,n) ); } printf("Theoretical limit = %21.15lf\n", exp(k)); return EXIT_SUCCESS; }