Summing a series within a For Loop

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by