Question

8. Write a MATLAB function that will take as inputs a set of data (x,y) and...

8. Write a MATLAB function that will take as inputs a set of data (x,y) and output the best estimate of the integral using a Romberg Array. The function should perform two trapezoidal integrations using the full interval and half the interval, and then combine these integrations in a Romberg Array. Your function should also output the relative error of the integral. Please note that the data entered may not be evenly spaced and your function should make sure that at least 3 data points were entered and that the x- and yvectors containing the data are the same length. Hint: Use interpolation to obtain the required points for the trapezoidal integrations function [integral, err] = Romberg(x,y)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: The MATLAB code is shown below: Define a function romberg () function r romberg (f, a, b,n) %Compute width %Define r %Loop until length for j = 2:n Declare variables subtotal = 0; %Loop for i until length = 1:2^ (j-2) %compute total subtotal = subtotal + f (a + (2?1-1)?h (j)); %End end

Code:

%Define a function romberg()

function r = romberg(f,a,b,n)

%Compute width

h = (b - a) ./ (2.^(0:n-1));

%Define r

r(1,1) = (b - a) * (f(a) + f(b)) / 2;

%Loop until length

for j = 2:n

%Declare variables

subtotal = 0;

%Loop until length

for i = 1:2^(j-2)

%Compute total

subtotal = subtotal + f(a + (2 * i - 1) * h(j));

%End

end

%Compute value

r(j,1) = r(j-1,1) / 2 + h(j) * subtotal;

%Loop until length

for k = 2:j

%Compute r

r(j,k) = (4^(k-1) * r(j,k-1) - r(j-1,k-1)) / (4^(k-1) - 1);

%End  

end

%End

end;

Add a comment
Know the answer?
Add Answer to:
8. Write a MATLAB function that will take as inputs a set of data (x,y) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT