Azzera filtri
Azzera filtri

Summing a series within a For Loop

3 visualizzazioni (ultimi 30 giorni)
Tony Stianchie
Tony Stianchie il 7 Mar 2023
Commentato: VBBV il 7 Mar 2023
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.

Risposta accettata

Edoardo_a
Edoardo_a il 7 Mar 2023
Modificato: Edoardo_a il 7 Mar 2023
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 Commento
VBBV
VBBV il 7 Mar 2023
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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by