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.
0 个评论
采纳的回答
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
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
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!