Sie sind auf Seite 1von 6

KEA090035 SAZNIZAM SAZMEE SINOH

STUDENT NAME : MATRIC NO. : COURSE CODE/COURSE NAME : SEMESTER/ACADEMIC YEAR : LECTURER :

SAZNIZAM SAZMEE SINOH KEA090035 KAEA3202 ENGINEERING MATHEMATICS 2 2 2011/2012 DR. NORJIDAH BINTI ANJANG ABD HAMID

INSTRUCTION

I
Given:

x 2 e x dx
,
1

Write a program to carry out Simpsons 3 rule to approximate the integral using the sequence of mesh sizes:

1 2

1 1 1 1 b a, 4 b a, 8 b a, 16 b a, , 2048 b a ,

where b a is the length of the given interval. The exact integral is given as I = 2 - 10e-2 = 0.646647168. Verify that the expected the rate of decrease of the error is observed, Comment on any anomalies observed.

KEA090035 SAZNIZAM SAZMEE SINOH

The following code is written using C programming language and was compiled using DevCpp Integrated Development Environment. SOURCE CODE #include <stdio.h> #include <math.h> float const a=0.0, b=2.0; float x, h, n; double I_approx, error; double step_size(float number); float function(float var); double I_calc(float num, double step); double sum_odd(float num_o, double step_o); double sum_even(float num_e, double step_e); double error_calc(double err); double const I_exact=0.646647168; int main() { printf("ASSIGNMENT - NUMERICAL INTEGRATION\n"); printf("----------------------------------\n"); printf("\nNAME: SAZNIZAM SAZMEE SINOH\n"); printf("MATRIC NO: KEA090035\n\n"); printf("Hello. This program will use Simpson's 1/3 rule to calculate\n"); printf("\nI = integral of (x^2)/exp(x) from 0 to 2 \n"); printf("\nusing the following mesh sizes\n"); printf("\n(b-a)/2, (b-a)/4, (b-a)/8, ..., (b-a)/2048 \n"); printf("\nas well as calculate the error of these values compared to\n"); printf("the true value of %.10f\n", I_exact); printf("\n---------------------------------------------------------------------------- \n"); printf("| Step Size \t |\tApprox Value | Relative True Percentage Error(%%) |\n"); printf("---------------------------------------------------------------------------- \n"); int i; I_approx=0; error=0; for(i=1;i<12;i++) { n=pow(2,i); h=step_size(n); I_approx=I_calc(n,h); error=error_calc(I_approx); printf("| %10.10f |\t %10.10f |\t\t %10.10f\t\t |\n", h, I_approx, error); } printf("---------------------------------------------------------------------------- \n");

KEA090035 SAZNIZAM SAZMEE SINOH

printf("\n Thank You\n"); printf(" ______ \n"); printf(" / \\ \n"); printf(" / ^__^ \\ \n"); printf(" \\ \\/ / \\\\//\n"); printf(" \\ ____ / ( )\n"); printf("\n(C) 2012 SAZNIZAM SAZMEE SINOH");

getchar(); return 0; }

float function(float var) { return pow(var, 2)/exp(var); } double step_size(float number) { return (b-a)/number; } double I_calc(float num, double step) { float first=function(a); double sum_of_odd=sum_odd(num, step); double sum_of_even=sum_even(num, step); float last=function(b); return (step/3.0)*(first+4*sum_of_odd+2*sum_of_even+last); } double sum_odd(float num_o, double step_o) { float p=0, lim_o=0; p=a+step_o; double sum_o=0, odd=0; lim_o=num_o/2; int h=1; while (h<=lim_o) { odd=function(p); sum_o=sum_o+odd; p=p+2*step_o; h++; } return sum_o; }

KEA090035 SAZNIZAM SAZMEE SINOH

double sum_even(float num_e, double step_e) { float q=0, lim_e=0; q=a+2*step_e; double sum_e=0, even=0; lim_e=(num_e/2)-1; int k=1; while (k<=lim_e) { even=function(q); sum_e=sum_e+even; k++; q=q+2*step_e; } return sum_e; } double error_calc(double err) { return 100*fabs(err-I_exact)/I_exact; }

KEA090035 SAZNIZAM SAZMEE SINOH

OUTPUT

KEA090035 SAZNIZAM SAZMEE SINOH

DISCUSSION The results show that as the step size decreases, so too does the true relative percentage error. In other words, as the number of iterations required to calculate the value of I increase, then the error of the approximated value to the exact value decreases. Consider the figure below which shows a graphical representation of multiple applications for Simpsons 1/3 Rule.

Figure 1.1: Graphical representation of Simpsons 1/3 Rule The number of vertical rectangles represents the number of iterations required to calculate I and the width of these rectangles is the step size. If the step size is very big, then the actual shape of the graph is not represented accurately. However, as the step size decreases, then the width of the rectangles decreases which causes the combined shape to resemble the function more accurately as shown in the figure below.

Figure 1.2: As the value of the step size decreases, the shape of the rectangles more accurately mirrors the shape of the curve.