Using C to Approximate Simpson's 1/3 Rule

8
KEA090035 SAZNIZAM SAZMEE SINOH STUDENT NAME : SAZNIZAM SAZMEE SINOH MATRIC NO. : KEA090035 COURSE CODE/COURSE NAME : KAEA3202 ENGINEERING MATHEMATICS 2 SEMESTER/ACADEMIC YEAR : 2 2011/2012 LECTURER : DR. NORJIDAH BINTI ANJANG ABD HAMID INSTRUCTION Given: I= 0 2 x 2 e x dx , Write a program to carry out Simpson’s 1 3 rule to approximate the integral using the sequence of mesh sizes: h= 1 2 ( ba ) , 1 4 ( ba ) , 1 8 ( ba) , 1 16 ( ba ) , , 1 2048 ( ba ) , 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. 1

description

This was an assignment to use computer programming to approximate a solution using Simpson's 1/3 Rule

Transcript of Using C to Approximate Simpson's 1/3 Rule

Page 1: Using C to Approximate Simpson's 1/3 Rule

KEA090035 SAZNIZAM SAZMEE SINOH

STUDENT NAME : SAZNIZAM SAZMEE SINOHMATRIC NO. : KEA090035COURSE CODE/COURSE NAME : KAEA3202 ENGINEERING MATHEMATICS 2SEMESTER/ACADEMIC YEAR : 2 2011/2012LECTURER : DR. NORJIDAH BINTI ANJANG ABD HAMID

INSTRUCTION

Given:

I=∫0

2

x2 e−x dx,

Write a program to carry out Simpson’s 13 rule to approximate the integral using the

sequence of mesh sizes:

h=12

(b−a ) , 14

(b−a ) , 18

(b−a ) , 116

(b−a ) ,⋯, 12048

(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.

1

Page 2: Using C to Approximate Simpson's 1/3 Rule

KEA090035 SAZNIZAM SAZMEE SINOH

The following code is written using C programming language and was compiled using Dev-Cpp 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");

2

Page 3: Using C to Approximate Simpson's 1/3 Rule

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;}

3

Page 4: Using C to Approximate Simpson's 1/3 Rule

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;}

4

Page 5: Using C to Approximate Simpson's 1/3 Rule

KEA090035 SAZNIZAM SAZMEE SINOH

OUTPUT

5

Page 6: Using C to Approximate Simpson's 1/3 Rule

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 Simpson’s 1/3 Rule.

Figure 1.1: Graphical representation of Simpson’s 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.

6