How can I integrate a vector that varies as a function of length of time?
3 views (last 30 days)
Show older comments
t=2:201;
s=1:200;
mu = [10*ones(1,length(t)/4) 15*ones(1,length(t)/4) 20*ones(1,length(t)/4) 10*ones(1,length(t)/4)] ;
for i =1:length(t)
p= integeral(mu,s(i),t(i))
end
I am a beginner to matlab and wanted to find the integeral of the vector mu. can any one help me please? Mu fanction is assigned 10 for the first 50 intervals, 15,20 and 10 for the conscative 50 intervals each.
Thank you!
0 Comments
Answers (1)
Rushil
on 7 May 2025
Hello
I believe that the task of integration over a piecewise constant function can be accomplished using the “trapz” function.
For each interval defined by
and
, you can compute the integral by selecting the corresponding segment from the mu function and applying “trapz”.
In this approach, s and t are selected to remain within the bounds of mu, thereby preventing indexing errors.
Refer to the below given code snippet for better understanding:
t = 2:200;
s = 1:199;
mu = [10*ones(1,50) 15*ones(1,50) 20*ones(1,50) 10*ones(1,50)];
integral_mu = zeros(1, length(t));
for i = 1:length(t)
segment = mu(s(i):t(i));
% trapz for numerical integration
integral_mu(i) = trapz(s(i):t(i), segment);
end
% plot the results
plot(s, integral_mu)
xlabel('s')
ylabel('Integral of mu from s to t')
title('Numerical Integral of mu')
You may read more about “trapz” at the link below:
Hope it helps
0 Comments
See Also
Categories
Find more on Problem-Based Optimization Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!