Help with trapezoidal rule while incrementing step size please help!

3 次查看(过去 30 天)
I have my function for the trapezoidal rule written and works fine, but I want to add a while loop to increment the step size in order to find out how many terms it takes to find an acceptable answer. here is my code so far:
function answer = trapIntegration(a,b)
x = a;
fun = @(x) exp(x).*sin(x);
s = fun(a);
realAns = integral(fun,a,b);
errMax = 0.00001;
answer = 0;
n = 2;
while abs(realAns-answer) >= errMax
stepsize = (b-a)/n;
for i=1:n-1
x = x + stepsize;
s = s + 2*fun(x);
end
s = s + fun(b);
answer = (b-a)*s/(2*n);
n = n+1;
end
terms = n
This is the output I am getting:
>> trapIntegration(0,pi)
terms =
233
ans =
NaN
Please help!!

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by