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

조회 수: 3 (최근 30일)
tewelde Mezgebo
tewelde Mezgebo 2022년 4월 3일
답변: Rushil 2025년 5월 7일
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