Summing a series within a For Loop

1 回表示 (過去 30 日間)
Tony Stianchie
Tony Stianchie 2023 年 3 月 7 日
コメント済み: VBBV 2023 年 3 月 7 日
H = 0.1;
I = 5;
Y = 0;
BI = zeros(I,1);
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),pi*i);
BI(i) = b ;
end
X = linspace(0,1,I);
Theta = zeros(I,1);
for k = 1:length(X)
for m = 1:BI
Ts = sum(((((1/BI).*(1-cos(BI)))/(0.5-(1/(4.*BI)))).*sin(BI.*Y).*(cosh(2.*BI.*(X))-tanh(2.*BI).*sinh(2.*BI.*(X)))));
end
end
I'd like to:
  • Start at X =0
  • Sum Ts for all values of BI
  • Step to next value of X
  • Sum Ts for all values of BI
  • etc.

採用された回答

Edoardo_a
Edoardo_a 2023 年 3 月 7 日
編集済み: Edoardo_a 2023 年 3 月 7 日
Hi, do you mean something like that?
In this case I sum all the values in the same Ts variable for all the X entry.
If you want to save a different Ts sum for each X entry then you should preallocate and store each Ts from the for loop.
H = 0.1;
I = 5;
Y = 1;
BI = zeros(I,1);
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),pi*i);
BI(i) = b ;
end
X = linspace(0,1,I);
Theta = zeros(I,1);
Ts = zeros(1);
for k = 1:length(X)
for m = 1:length(BI)
Ts =Ts + ((((1/BI(m)).*(1-cos(BI(m))))/(0.5-(1/(4.*BI(m))))).*sin(BI(m).*Y).*(cosh(2.*BI(m).*(X(k)))-tanh(2.*BI(m)).*sinh(2.*BI(m).*(X(k)))));
end
end
  1 件のコメント
VBBV
VBBV 2023 年 3 月 7 日
H = 0.1;
I = 5;
Y = 1;
BI = zeros(I,1);
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),pi*i);
BI(i) = b ;
end
X = linspace(0,1,I);
Theta = zeros(I,1);
Ts = zeros(1);
for k = 1:length(X)
for m = 1:length(BI)
Ts(m) = ((((1/BI(m)).*(1-cos(BI(m)*pi/180)))/(0.5-(1/(4.*BI(m))))).*sin(BI(m)*(pi/180).*Y).*(cosh(2.*BI(m)*(pi/180).*(X(k)))-tanh(2.*BI(m)*pi/180).*sinh(2.*BI(m)*(pi/180).*(X(k)))));
%->>
end
TS(k) = sum(Ts);
end
TS
TS = 1×5
0.0030 0.0029 0.0028 0.0027 0.0027
You can do store the Ts values for each BI iteration using its index, and sum the Ts variable later. Also, input values to trigonometric functions, need to be in radians, for which you can multply with pi/180

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by