3/8 Simpson's Rule

조회 수: 3 (최근 30일)
Hasan Dikka
Hasan Dikka 2020년 9월 3일
답변: Alan Stevens 2020년 9월 3일
I created my code for 3/8 Simpson's Rule also ı want to add relative error. How can I add relative error in code.
function hasan2 (n)
a = 0;
b = 1;
h = (b-a)/n;
sum = 0;
for i = 1:n-1
a = (i-1)*h;
b = i*h;
sum = sum + 3/8*(f(a) + 3*f((2*a+b)/3) + 3*f((a+2*b)/3) + f(b));
end
sum
end
function fx=f(x)
fx=exp(2*x)*sin(5*x);
end

답변 (1개)

Alan Stevens
Alan Stevens 2020년 9월 3일
The result of integrating of your function between 0 and 1 is:
I = (exp(1)^2*(2*sin(5)-5*cos(5)) + 5)/29;
so relative error is
relerror = abs(sum/I - 1);
As an aside, "sum" is a built-in Matlab function, so it's best not to use it as a variable name.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by