how do I get the cumulative sum of integrals after for loop?

2 ビュー (過去 30 日間)
Deen Halis
Deen Halis 2015 年 10 月 3 日
編集済み: Triveni 2016 年 1 月 3 日
Hello,
I’m having two expressions with many variables. I have simplified it here in order to easily check the answers. ‘K’in eqn 1 and ‘t’ in eqn 2 vary from a set of numbers(maybe non-integers and non-logicals). Eqn 2 is to be integrated with values of ‘t’ being the limits.
I got the values from the integration. These values are lumped together, but I want to separate them for each value of K (ie 2 and 5 in this example) Also, I want get the cumulative sum of each jth element from ‘sa(i,j)’, something like (0.1667,0.1667+1.1667, 0.1667+1.1667 +3.1667,+……till +21.1667) for the first set of results, then the same operation for the second set ie 0.4167,0.4167+2.9167,…..+52.9167). The cumulative command (cumsum) isn’t working for me.
I would appreciate any help.
% N and K could have any number of elements
K = [2 5];
% K = [2 5 6.2 1.5];
% K = [2 5 6.2 1.5 2.5];
% K = [2 5 6.2 1.5 2.5 1.8 8.6 4.3];
n = length(K);
N = 3;
L = [0:0.5:N];
syms A t;
for i = 1:n;
B(i) = 2*K(i);%eqn 1 %this is a dummy expression; not what I really %$have
M(i) = B(i)*t^2; %eqn 2 %this is a dummy expression; not what I really %$have
for j = 1:length(L)
s1(i) = int(M(i),t,A,A+0.5);
sa(i,j) = double(subs(s1(i), A, L(j)));
% disp(sa(i,j));
% sb(i,j) = cumsum(sa(i,j))%%not working
end
end
Result:
0.1667
1.1667
3.1667
6.1667
10.1667
15.1667
21.1667
0.4167
2.9167
7.9167
15.4167
25.4167
37.9167
52.9167

採用された回答

Triveni
Triveni 2016 年 1 月 3 日
編集済み: Triveni 2016 年 1 月 3 日
sum(sum(sa(:,:,:)))
or you could find sb by
% N and K could have any number of elements
K = [2 5];
% K = [2 5 6.2 1.5];
% K = [2 5 6.2 1.5 2.5];
% K = [2 5 6.2 1.5 2.5 1.8 8.6 4.3];
n = length(K);
N = 3;
L = [0:0.5:N];
syms A t;
for i = 1:n;
B(i) = 2*K(i);%eqn 1 %this is a dummy expression; not what I really %$have
M(i) = B(i)*t^2; %eqn 2 %this is a dummy expression; not what I really %$have
for j = 1:length(L)
s1(i) = int(M(i),t,A,A+0.5);
sa(i,j) = double(subs(s1(i), A, L(j)));
% disp(sa(i,j));
sb = sum(sa,2);
end
end
find all sum use
sum(sb)

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by