필터 지우기
필터 지우기

I tried to create a mid point formula and trapezoid formula in Matlab and solve a integral using it but I'm not getting the same result as the build in function.

조회 수: 1 (최근 30일)
we are trying to use these two formula to approximate a integral value. instead of using the build in function of
format long
integral(@(x)exp(-x.^4),0,20)
ans =
0.906402477055477
but the result should match the result of build in function.
we are trying to approxiamte this integral
my code is follows
a = 0;
b = 20;
n = 10000;
h = (b-a)/n;
x = linspace(a,b,n+1);
y = exp(-x.^4);
x_mid = a+h/2:h:b;
t_form = h*(sum(y)+((y(1)+y(end))/2))
t_form =
0.908402477055477
my result doesn't match the original result. But i'm not sure where the problem is.

채택된 답변

Torsten
Torsten 2022년 10월 16일
편집: Torsten 2022년 10월 17일
a = 0;
b = 20;
h = 1e-2;
x = a:h:b;
y = exp(-x.^4);
x_mid = a+h/2:h:b-h/2;
y_mid = exp(-x_mid.^4);
format long
trapz_form = h*((y(1)+y(end))/2+sum(y(2:end-1)))
trapz_form =
0.906402477055477
mid_point_form = h*sum(y_mid)
mid_point_form =
0.906402477055477
reference_int = integral(@(x)exp(-x.^4),0,20)
reference_int =
0.906402477055477

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by