Azzera filtri
Azzera filtri

Help with trapezoidal rule while incrementing step size please help!

3 visualizzazioni (ultimi 30 giorni)
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!!

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by