Question

In: Computer Science

Give me a MATLAB code which integrates any function using Romberg integration The code must work...

Give me a MATLAB code which integrates any function using Romberg integration

The code must work fine or else I will down vote the answer badly

Dont post if you dont know

Solutions

Expert Solution

The given MATLAB CODE takes any function and integrates it as per the rule of Romberg as per various levels and prints the result. The intermediate trapezoidal rule code is also shown

The sample is shown for f(x)=1/(1+x) for 5 levels from 0 to 1

clc;clear all
format long
f=@(x) 1./(1+x);
a=0;b=1;h=1;n=5;
ans1=RombergIntegration(f,a,b,h,5);
fprintf('Desired answer is %5.6f \n',ans1(1,n+1))
function I=RombergIntegration(f,a,b,s,level)
for(j=1:(level+1))
ni=s*2^(j-1);
I(j,1)=trapezoidal(f,a,b,ni);
end
for(j=2:level+1)
for(k=1:(level-j+2))
I(k,j)=(4^(j-1)*I(k+1,j-1)-I(k,j-1))/(4^(j-1)-1);
end
end
end
function answer=trapezoidal(f,a,b,n)
i(1)=f(a); i(n+1)=f(b);
h=(b-a)/n;
x=a:h:b;
i(2:n)=2*f(x(2:(length(x)-1)));
answer=0.5*h*sum(i);
end


Related Solutions

Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine...
Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine and must break into 2 matrix L and U It must be in MATLAB The code used must use continue statement. If no continue statement is there in code it will be downvoted. Answer only if you know or else i will dislike badly
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Give me a working MATLAB code for the Golden section search method . It should be...
Give me a working MATLAB code for the Golden section search method . It should be working Dont answer if you do not know, the code must work for the golden section method
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank...
This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank in the shape of an inverted cone. The radius of the tank at the top rim is 3 m, and the total height of the tank is 4 m. The volume of the tank in m3 is given by: V = (1/3) R2 H. The volume of liquid in the tank when filled to a height h measured from the bottom vertex is: V...
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
This is a numerical methods question using MATLAB. Which of the following code snippets finds the...
This is a numerical methods question using MATLAB. Which of the following code snippets finds the forward difference estimate of the derivative at each of the x values. Assume x and y have been previously defined, for example as y=[10,20,25, 27.5, 30]; x = [0.3,0.5, 0.8, 0.9, 1]; (d is the derivative variable name) Although not necessarily so, there may be more than one correct answer. a) for k=1:length(y)-1 d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end d(k+1)=NaN b) for k=1:length(y) d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end c) d(1)=NaN; for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT