MATLAB Script for Simpson's Rule Integration, Assignments of Numerical Methods in Engineering

This matlab script implements simpson's rule for numerical integration. The script takes an array of x and y values, calculates the derivative of a function using the rule, and prints the value of the derivative at a given point x0. It also plots the function using fplot.

Typology: Assignments

2019/2020

Uploaded on 05/29/2020

unknown user
unknown user 🇳🇱

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
function simpsonsIntegration
x=[2.5, 2.0, 3.0, 1.5, 3.5, 1.0, 4.0, 0.5, 4.5, 0.0, 5.0];
y=[2.14, 1.73, 2.59, 1.60, 2.66, 1.82, 1.80, 2.27, 0.26, 2.67, 0.45];
x0 = 2.5;
n = size(x, 1);
if n == 1
n = size(x,2);
end
for i = 1:n
D(i,1) = y(i);
end
for i = 2:n
for j = 2:i
D(i,j)=(D(i,j-1)-D(i-1,j-1))/(x(i)-x(i-j+1));
end
end
fx0 = D(n,n);
for i = n-1:-1:1
fx0 = fx0*(x0-x(i)) + D(i,i);
end
formula =@(z) D(1,1)+(D(2,2)*(z-x(1)))+(D(3,3)*(z-x(1))*(z-x(2)))+(D(4,4)*(z-
x(1))*(z-x(2))*(z-x(3)))+(D(5,5)*(z-x(1))*(z-x(2))*(z-x(3))*(z-x(4)))+(D(6,6)*(z-
x(1))*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5)))+(D(7,7)*(z-x(1))*(z-x(2))*(z-x(3))*(z-
x(4))*(z-x(5))*(z-x(6)))+(D(8,8)*(z-x(1))*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-
x(6))*(z-x(7)))+(D(9,9)*(z-x(1))*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-x(6))*(z-
x(7))*(z-x(8)))+(D(10,10)* (z-x(1))*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-
x(6))*(z-x(7))*(z-x(8))*(z-x(9)))+(D(11,11)*(z-x(1))*(z-x(2))*(z-x(3))*(z-x(4))*(z-
x(5))*(z-x(6))*(z-x(7))*(z-x(8))*(z-x(9))*(z-x(10)));
derivativeFormula =@(z) (D(2,2)+(D(3,3)*(z-x(2)))+(D(4,4)*(z-x(2))*(z-x(3)))+
(D(5,5)*(z-x(2))*(z-x(3))*(z-x(4)))+(D(6,6)*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5)))+
(D(7,7)*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-x(6)))+(D(8,8)*(z-x(2))*(z-x(3))*(z-
x(4))*(z-x(5))*(z-x(6))*(z-x(7)))+(D(9,9)*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-
x(6))*(z-x(7))*(z-x(8)))+(D(10,10)*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-x(6))*(z-
x(7))*(z-x(8))*(z-x(9)))+(D(11,11)*(z-x(2))*(z-x(3))*(z-x(4))*(z-x(5))*(z-x(6))*(z-
x(7))*(z-x(8))*(z-x(9))*(z-x(10))));
deriv=derivativeFormula(x0);
D
fprintf('The value of the derivative of x = %f is %f', x0 , deriv);
fplot(formula, [-0.1, 5.1])
end

Partial preview of the text

Download MATLAB Script for Simpson's Rule Integration and more Assignments Numerical Methods in Engineering in PDF only on Docsity!

function simpsonsIntegration x=[2.5, 2.0, 3.0, 1.5, 3.5, 1.0, 4.0, 0.5, 4.5, 0.0, 5.0]; y=[2.14, 1.73, 2.59, 1.60, 2.66, 1.82, 1.80, 2.27, 0.26, 2.67, 0.45]; x0 = 2.5; n = size(x, 1); if n == 1 n = size(x,2); end for i = 1:n D(i,1) = y(i); end for i = 2:n for j = 2:i D(i,j)=(D(i,j-1)-D(i-1,j-1))/(x(i)-x(i-j+1)); end end fx0 = D(n,n); for i = n-1:-1: fx0 = fx0(x0-x(i)) + D(i,i); end formula =@(z) D(1,1)+(D(2,2)(z-x(1)))+(D(3,3)(z-x(1))(z-x(2)))+(D(4,4)(z- x(1))(z-x(2))(z-x(3)))+(D(5,5)(z-x(1))(z-x(2))(z-x(3))(z-x(4)))+(D(6,6)(z- x(1))(z-x(2))(z-x(3))(z-x(4))(z-x(5)))+(D(7,7)(z-x(1))(z-x(2))(z-x(3))(z- x(4))(z-x(5))(z-x(6)))+(D(8,8)(z-x(1))(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z- x(6))(z-x(7)))+(D(9,9)(z-x(1))(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z-x(6))(z- x(7))(z-x(8)))+(D(10,10) (z-x(1))(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z- x(6))(z-x(7))(z-x(8))(z-x(9)))+(D(11,11)(z-x(1))(z-x(2))(z-x(3))(z-x(4))(z- x(5))(z-x(6))(z-x(7))(z-x(8))(z-x(9))(z-x(10))); derivativeFormula =@(z) (D(2,2)+(D(3,3)(z-x(2)))+(D(4,4)(z-x(2))(z-x(3)))+ (D(5,5)(z-x(2))(z-x(3))(z-x(4)))+(D(6,6)(z-x(2))(z-x(3))(z-x(4))(z-x(5)))+ (D(7,7)(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z-x(6)))+(D(8,8)(z-x(2))(z-x(3))(z- x(4))(z-x(5))(z-x(6))(z-x(7)))+(D(9,9)(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z- x(6))(z-x(7))(z-x(8)))+(D(10,10)(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z-x(6))(z- x(7))(z-x(8))(z-x(9)))+(D(11,11)(z-x(2))(z-x(3))(z-x(4))(z-x(5))(z-x(6))(z- x(7))(z-x(8))(z-x(9))*(z-x(10)))); deriv=derivativeFormula(x0); D fprintf('The value of the derivative of x = %f is %f', x0 , deriv); fplot(formula, [-0.1, 5.1]) end