Serial summation within a for() loop

3 次查看(过去 30 天)
Ali Dhukka
Ali Dhukka 2015-10-19
评论: Star Strider 2015-10-20
Summation Equations are a particular Achilles' Heel for me plus I'm a bit rusty on MATLAB these days. How I can algorithmize this equation?
where Δt_sup, q and t are each vector arrays. I was given this expansion as an example for Δt_sup(3):
Now I know that I need to do this within a for loop to iterate values for each corresponding value of Δt_sup(i), q(i) and t(i), but I'm not sure how to go about tackling this beyond the basic setup as such:
t_sup=zeros(length(q),1);
for i=1:length(q)
t_sup(i)= ???
end
Any help would be much appreciated.
  1 个评论
Stefan Raab
Stefan Raab 2015-10-19
You could use another for-loop within the for-loop which runs from 1 to i and adds the actual j-value in each step:
t_sup(i) = t_sup(i) + ((q(j) - q(j-1))/q(i))*log(t(i)-t(j-1)); % Caution: log10 for log to the base 10, log is for base e

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2015-10-19
You might not need any loops. See if this works:
q = randi(99, 10, 1); % Create Data (Column Vector)
t = [1:10]'; % Create Data (Column Vector)
dq = [diff(q); q(1)]/q(end); % ‘q-difference’ Vector
dt = [(t(end)-t(1:end-1)); t(end)]; % ‘t-difference’ Vector
Dt_sup = sum(dq .* log(dt)); % Δt_sup
  2 个评论
Ali Dhukka
Ali Dhukka 2015-10-20
Correct me if my understanding of what your script does is off, but I think I still need a for loop as I need to be able to find a value for Dt_sup at each time t and this script results with a scalar value for Dt_sup.
Star Strider
Star Strider 2015-10-20
My code calculates ‘Dt_sup’ for equal-length vectors of ‘t’ and ‘q’ defined in the code. My understanding of your code is that it is a scalar for given equal-length vectors of ‘t’ and ‘q’. It does not appear to be a function of ‘t’ otherwise. You can certainly loop through it with different vectors for ‘t’ and ‘q’, the only restriction being that the ‘t’ vector must be strictly monotonically increasing.
I just took the equation in your Question and coded it as presented. I have no idea what you’re doing, or the larger context of this equation.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by