Absolute error for Intergration

4 views (last 30 days)
j.roc
j.roc on 14 Nov 2022
Edited: Chandler Hall on 14 Nov 2022
doing trapezoid intergration. then asked to find the error of this and double the number of trapezoids used if the error is > 0.0006
I've tried to do this but it doesnt like my arrays and i dont quite know why? new to MatLab and struggling :/
"Index exceeds the number of array elements. Index must not exceed 3.
Error in wrksheet3 (line 21)
R= -0.073*t(i+1).^2 + 6.1802*t(i+1);"
oh and we can't use the built in int function.

Answers (1)

Chandler Hall
Chandler Hall on 14 Nov 2022
Edited: Chandler Hall on 14 Nov 2022
It is much more convenient if you paste the actual text of the script instead of an image, or attach the entire .m file with the paperclip icon.
dx = (b-a)/n;
t = a:dx:b;
h = zeros(1, n);
These need to be placed inside of the main loop so that they are updated everytime n is changed. You should also move the doubling of n to the end of the loop so that you don't skip n=2, which theoretically could be accurate enough.
% for i = 1:b will always iterate from 1-60
for i = 1:n % will iterate over the number of traps
Are you given the actual value of the integral to compare against or are you expected to use the built-in function at least for that purpose? Your current error calculation is not appropriate You'll need to move the integral approximation calculation inside of the loop as well, and compare that to the true value. Check the condition on the while loop again. What happens if the trapezoid integral is smaller and produces an error of, say, negative 2?
Anonymous functions are a very useful tool to eliminate repetition and the potential for hard-to-spot typos. For example:
f = @(x) -0.073*x.^2 + 6.1802*x;
L = f(t(i));
R = f(t(i+1));

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!