# Absolute error for Intergration

조회 수: 2 (최근 30일)
j.roc 2022년 11월 14일
편집: Chandler Hall 2022년 11월 14일
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.

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

### 답변 (1개)

Chandler Hall 2022년 11월 14일
편집: Chandler Hall 2022년 11월 14일
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));

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

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by