Filter löschen
Filter löschen

Help with trapezoidal rule while incrementing step size please help!

3 Ansichten (letzte 30 Tage)
Thomas MacDowell
Thomas MacDowell am 16 Jul. 2018
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!!

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by