Intergration of expotential function using simpsons rule

3 vues (au cours des 30 derniers jours)
Brian Makuza
Brian Makuza le 15 Nov 2019
I try to do intergration using simpsons rule. Each and every time l use the function simprule it gives me error, "undefined function or variable". How could l define the function more than what l did. Below is my code
%SIMPRULE Simpsons rule integration.
% I = SIMPRULE(F, A, B, N) returns Simpsons rule approximation
% for the integral of f(x) from x=A to x=B, using N subintervals,
% where F is a function handle.
f=@exp(x)
a=0
b=1
n=2
I = simprule(f, a, b, n)
h = (b-a) / n;
x = a:h:b; % an array of length n+1
S = 0;
L = 0;
for l = 1:2:n %generates the odd number array
S = S + 4*f(x(l));
end
for j = 2:2:n % generates the even number array
L = L + 2*f(x(j));
end
I = (h/3)*(f(a)+ S + L + f(x(n)));

Réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by