
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
Uploaded on 05/29/2020
1 / 1
This page cannot be seen from the preview
Don't miss anything!

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