How can I integrate a vector that varies as a function of length of time?

3 次查看(过去 30 天)
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!

回答(1 个)

Rushil
Rushil 2025-5-7
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

类别

Help CenterFile Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by