Develop a Matlab code to perform numerical integration for t
Develop a Matlab code to perform numerical integration for the following data using the combination of trapezodal rule and Simpson\'s 1/3 rule. Matlab code MUST BE BASED ON Pseudocode provided below.
| x | 0 | 0.5 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| f(x) | 0.5 | 3.2 | 5.1 | 9.8 | 10.3 | 9.4 | 8.7 | 6.6 | 5.4 |
Solution
I = simpsons(f,a,b,n)
Where,
f= can either be an anonymous function (e.g. f=@(x) sin(x)) or a vector containing equally spaced values of the function to be integrated
a= Initial point of interval
b= Last point of interval
n= # of sub-intervals (panels), must be integer
Suppose you want to integrate a function f(x) in the interval [-1,1].
You also want 3 integration points (2 panels) evenly distributed through the
domain (you can select more point for better accuracy).
Thus:
f=(x) ((x-1).*x./2).*((x-1).*x./2);
I=simpsons(f,-1,1,2)
Suppose you want to integrate a function f(x) in the interval [-1,1].
You know some values of the function f(x) between the given interval,
I=simpsons(fi,-1,1)
note that there is no need to provide the number of intervals (panels) \"n\"
Integrate Y = SIN(X)
x = 0:0.2:pi;
y = sin(x);
a = sum(y)*0.2; % Rectangle rule
b = trapz(x,y); % Trapezoid rule
c = simpson(x,y,\'1/3\'); % Simpson\'s 1/3 rule
d = simpson(x,y,\'3/8\'); % Simpson\'s 3/8 rule
e = cos(x(1))-cos(x(end)); % Actual integral
fprintf(\'Rectangle Rule: %.15f\ \', a)
fprintf(\'Trapezoid Rule: %.15f\ \', b)
fprintf(\'Simpson\'\'s 1/3 Rule: %.15f\ \', c)
fprintf(\'Simpson\'\'s 3/8 Rule: %.15f\ \', d)
fprintf(\'Actual Integral: %.15f\ \', e)
x1 = linspace(0,2,4);
x2 = linspace(0,2,7);
x4 = linspace(0,2,13);
y = @(x) 2+cos(2*sqrt(x));
format long
y1 = y(x1); res1 = simpson(x1,y1, \'3/8\'); disp(res1)
y2 = y(x2); res2 = simpson(x2,y2, \'3/8\'); disp(res2)
y4 = y(x4); res4 = simpson(x4,y4, \'3/8\'); disp(res4)
Class support for inputs X, Y: float: double, single
