Problem with for loop
Mostra commenti meno recenti
areaneeded=3.75*100.1/100;
%disp(areaneeded) %area needed for a 0.1% error
a=1;
b=2;
for n=1:100;
h=(b-a)/n;
x=a:h:b;
y=x.^3;
ya=a.^3;
yb=b.^3;
area = h/2*(ya+yb+2*(sum(y)-ya-yb));
%disp(area)
tol=1e-12;
if abs(areaneeded-area)<tol
disp(n)
break
end
end
%
In this code i'm trying to find out the number of strips necessary to get an error of 0.1% with the trapezium rule. by cross multiplication i get the area i need to get with the trapezium rule, and then with the for loop i try to run n a hundred times (n=1, n=2, n=3...) and when the result from the trapezium rule is equal to the areaneeded display n. But i don't get any answer and i don't know how to solve it. Some help would be appreciate. Thanks in advance.
2 Commenti
per isakson
il 27 Mag 2015
Modificato: per isakson
il 27 Mag 2015
Here your code runs without throwing any error. What error do you get?
Alvaro García
il 28 Mag 2015
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Get Started with MuPAD in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!