Why is this giving me an error message when trying to use trapz.

4 vues (au cours des 30 derniers jours)
Saruultugs Batzorig
Saruultugs Batzorig le 23 Août 2019
Réponse apportée : Allen le 24 Août 2019
Hello, I'm trying to integrate for each time step the following. But it keeps giving me an error message.
B=18.6;
t0=0;
step=0.5;
tmax=90;
npas=tmax/step+1;
U=60;
for i= 1:npas
t0=t0+step;
s = (U.*t0)./B;
fun3 = @(s) (0.0081*(exp(-0.0058.*s))-0.0392.*exp(-0.0833.*s));
y3 = fun3(s);
q3(i) = trapz(s,y3);
end
B=18.6;
t0=0;
step=0.5;
tmax=90;
npas=tmax/step+1;
U=60;
for i= 1:npas
t0=t0+step;
s = (U.*t0)./B;
fun3 = @(s) (0.0081*(exp(-0.0058.*s))-0.0392.*exp(-0.0833.*s));
y3 = fun3(s);
q3(i) = trapz(s,y3);
end
  6 commentaires
Walter Roberson
Walter Roberson le 23 Août 2019
fun3 = @(s) (0.0081*(exp(-0.0058.*s))-0.0392.*exp(-0.0833.*s)); %does not change with i
for i= 1:npas
t0=t0+step;
s(i) = (U.*t0)./B;
y3(i) = fun3(s(i));
end
q3 = trapz(s, y3);
Though I suspect you would prefer to use cumtrapz() instead of trapz() here.
Saruultugs Batzorig
Saruultugs Batzorig le 24 Août 2019
It worked! Thank you so much.

Connectez-vous pour commenter.

Réponses (1)

Allen
Allen le 24 Août 2019
A more concise aproach.
B = 18.6;
step = 0.5;
tmax = 90;
npas = tmax/step+1;
U = 60;
fun3 = @(s) (0.0081*(exp(-0.0058.*s))-0.0392.*exp(-0.0833.*s));
s = U/B*step*(1:npas/2/step);
y3 = fun3(s);
q3 = trapz(s,y3);

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by