Code for romberg integration using recursion

조회 수: 13 (최근 30일)
Prajakta Borse
Prajakta Borse 2021년 10월 26일
댓글: Jan 2021년 10월 28일
format long
eps_step = 1e-5;
N = 11;
a = 0;
b = 1;
R = zeros( N + 1, N + 1 );
h = b - a;
pow=(1/3);
R(0 + 1, 0 + 1) = 0.5*(cos(a) + cos(b))*h;
for i=1:N
h = h/2;
R( i + 1, 1 ) = 0.5*(cos(a) + 2*sum( cos( (a + h):h:(b - h) ) ) + cos(b))*h;
for j = 1:i
R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
end
if abs( R(0 + 1, 0 + 1) - R( i + 1, 1 ) ) < eps_step
break;
elseif fprintf( 'The composite trapezoidal rule did not converge' );
continue;
end
%R(0 + 1, 0 + 1) = R( i + 1, 1 );
if abs( R(i + 1, i + 1) - R(i, i) ) < eps_step
R(i + 1, i + 1)
break;
elseif i == N + 1
error( 'Romberg integration did not converge' );
end
end
I don't understand what's wrong with this code. I want to find what 'N' value is required for both romberg and trapezoidal and see which is greater. But the code does not run if the N value does not work for trapezoidal rule. Can anyone figure out the error in the code?

답변 (1개)

Jan
Jan 2021년 10월 26일
The posted code runs without an error in R2021b.
eps_step = 1e-5;
N = 11;
a = 0;
b = 1;
R = zeros( N + 1, N + 1 );
h = b - a;
pow=(1/3);
R(0 + 1, 0 + 1) = 0.5*(cos(a) + cos(b))*h;
for i=1:N
h = h/2;
R( i + 1, 1 ) = 0.5*(cos(a) + 2*sum( cos( (a + h):h:(b - h) ) ) + cos(b))*h;
for j = 1:i
R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
end
if abs( R(0 + 1, 0 + 1) - R( i + 1, 1 ) ) < eps_step
break;
elseif fprintf( 'The composite trapezoidal rule did not converge\n' );
continue;
end
%R(0 + 1, 0 + 1) = R( i + 1, 1 );
if abs( R(i + 1, i + 1) - R(i, i) ) < eps_step
R(i + 1, i + 1)
break;
elseif i == N + 1
error( 'Romberg integration did not converge' );
end
end
The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge The composite trapezoidal rule did not converge
"I don't understand what's wrong with this code" - please post why you assume, that there is a problem.
  댓글 수: 4
Prajakta Borse
Prajakta Borse 2021년 10월 27일
It does not give an error, I want the code to compute value using Trapezoidal rule and romberg integration in the same code. But I'm not able to do that.
Jan
Jan 2021년 10월 28일
Split your problem into parts. Start with the trapezoidal rule and modify it until it works. Afterwards do the same with the Romberg integration. I do not see, that this is working currently. You find many examples in the net for working versions. Finally join the two methods.
The description "I'm not able to do that" is not usefeul. You do see a problem. Then explain, what you see. Which values do not match which expectations?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by