# Simpson’s 1/3rd Rule MATLAB Program Examples

## Question

Evaluate the integral x^4 within limits -3 to 3 using Simpson’s 1/3 rd rule.

## Solution

Let y(x)=x^4

here a=-3 and b=3

therefore (b-a)=6

let ‘n’ be the number of intervals. assume n=6 in this case.

also h=(b-a)/n = 6/6 =1

x: -3  -2  -1  0  1  2  3

y: 81  16  1  0  1  16  81

According to Simpson’s 1/3rd  rule:

answer= (h/3)*[(y1+y7)+2*(y3+y5)+4*(y2+y4+y6)]

answer=(1/2)*[(81+81)+2*(1+1)+4*(16+0+16)]

answer=98.

## MATLAB Program For Simpson’s 1/3rd Rule

```%Created by myclassbook.org (Mayuresh)
%Created on 24 May 2013
%Question: Evaluate the integral X^4 within limits 3 to -3

clc;
clear all;
close all;

[email protected](x)x^4; %Change here for different function
a=-3;b=3; %Given limits
n=b-a; %Number of intervals
h=(b-a)/n;
p=0;

for i=a:b
p=p+1;
x(p)=i;
y(p)=i^4; %Change here for different function
end

l=length(x);
x
y
answer=(h/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))```

## Image Format MATLAB code for Simpson’s one-third rule

## Second Example

Evaluate the integral 1/(1+x) within limits 0 to 6 using Simpson’s 1/3 rd rule.

## Solution

Let y(x)=1/(1+x)

here a=0 and b=6

therefore (b-a)=6

let ‘n’ be the number of intervals. assume n=6 in this case.

also h=(b-a)/n = 6/6 =1

x: 0                  1                    2              3                  4               5               6

y: 1.0000   0.5000   0.3333   0.2500   0.2000   0.1667   0.1429

According to Simpson’s 1/3 rd rule.

answer= (h/3)*[(y1+y7)+2*(y3+y5)+4*(y2+y4+y6)]

answer=1.9587.

## MATLAB Code for Simpson’s 1/3rd Rule

```%Created by myclassbook.org (Mayuresh)
%Created on 24 May 2013
%Question: Evaluate the integral 1/(1+x) within limits 0 to 6

clc;
clear all;
close all;

[email protected](x)1/(1+x); %Change here for different function
a=0;b=6; %Given limits
n=b-a; %Number of intervals
h=(b-a)/n;
p=0;

for i=a:b
p=p+1;
x(p)=i;
y(p)=1/(1+i); %Change here for different function
end

l=length(x);
x
y
answer=(h/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))```

## Image Format MATLAB code for Simpson’s one-third rule

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

## 2 thoughts on “Simpson’s 1/3rd Rule MATLAB Program Examples”

1. SAID SAIF says:

% please I need to help how to integral (sin^3 ).x
% Generally sin^3 (x) is equal 1/4[3sinx – sin 3x]
clc;
clear all;
close all;

[email protected](x) (sinX)^3 ; %Change here for different function
a=0;b=pi/2; %Given limits
n=b-a; %Number of intervals
N=6;
h=n/N;
p=0;

for i=a:b
p=p+1;
x(p)=i;
y(p)=(sin i)^3 ; %Change here for different function
end

l=length(x);
x
y
answer=(h*1/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))
plot(x,y);

2. rkreddy says:

Please change the first loop I mean
for i=a:h:b

this solves your problem and you get a nice plot.