필터 지우기
필터 지우기

Summing a series within a For Loop

조회 수: 3 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by