0% found this document useful (0 votes)
152 views

NC Assignment PDF

The document provides MATLAB code to numerically compute integrals using Weddle's rule and Simpson's 1/3 rule. For Weddle's rule, the code defines functions, input limits, number of intervals, calculates the step size, and uses a formula to sum terms and output the result. For Simpson's 1/3 rule, the code similarly defines the function, limits, intervals, step size, and uses variables to sum odd and even terms before applying the Simpson's formula to output the approximation.

Uploaded by

Sabtain Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

NC Assignment PDF

The document provides MATLAB code to numerically compute integrals using Weddle's rule and Simpson's 1/3 rule. For Weddle's rule, the code defines functions, input limits, number of intervals, calculates the step size, and uses a formula to sum terms and output the result. For Simpson's 1/3 rule, the code similarly defines the function, limits, intervals, step size, and uses variables to sum odd and even terms before applying the Simpson's formula to output the approximation.

Uploaded by

Sabtain Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment Solution

Numerical Computation
Submitted To:
Sir Dr. Yasir Faheem
Submitted By:
Sabtain Ali
Registration #:
FA20-BCS-B-076
Topic:
MATLAB Commands
 MATLAB Commands for Weddle’s Rule
 Code:
f=input('\n Enter the function f(x): '); %inline('1/(1+x)')
% f=@ (x) 1/(1+x) or cos(x)-log(x)+exp(x) etc
a=input('Enter lower limit a: '); % exmple a=1
b=input('Enter upper limit b: '); % exmple b=2
n=input('Enter the number of sub-intervals n: '); % exmple n=36
h=(b-a)/n;
if rem(n,6)==0
m=n/6;
else
fprintf('\n Enter valid n!!!');
n=input('\n Enter n as multiple of 6 ');
m=n/6;
end
sum=0;
%Formula: (3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)
%+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h))
for k=1:1:m
sum=sum+((3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)
+f(a+6*h)));
a=a+6*h;
end
fprintf('\n The value of integration is %f',sum);
 MATLAB Commands for Simpson’s 1/3 Rule:
 Code:
syms x
% Lower Limit
a = 4;
% Upper Limit
b = 5.2;
% Number of Segments
n = 6;
% Declare the function
f1 = log(x);
% inline creates a function of string containing in f1
f = inline(f1);
% h is the segment size
h = (b - a)/n;
% X stores the summation of first and last segment
X = f(a)+f(b);
% variables Odd and Even to store
% summation of odd and even
% terms respectively
Odd = 0;
Even = 0;
for i = 1:2:n-1
xi=a+(i*h);
Odd=Odd+f(xi);
end
for i = 2:2:n-2
xi=a+(i*h);
Even=Even+f(xi);
end
% Formula to calculate numerical integration
% using Simpsons 1/3 Rule
I = (h/3)*(X+4*Odd+2*Even);
disp('The approximation of above integral is: ');
disp(I);

You might also like