Multiple Summation of Series using For Loops
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello all,
I am trying to sum the following series for various positions, y being the position in the function.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241445/image.png)
The 'r' and 'z' values are known dimensions of a coil, indicating the thickness and height of the coil respectively. The plot that I get from this code is incorrect and I can't figure out why. Is my methodology for executing the series summation for each value of position (y) correct? Any help would be greatly appreciated.
position = -0.1:0.001:0.1;
L = length(position);
Coupl = zeros(1,L);
s = 0;
r = [0.01746 0.0247];
z = [-0.0063 0.0063];
Ac = (r(2) - r(1))*(z(2)-z(1));
Nc = 1500;
Br = 1.31;
Vm = 3.218e-6;
Ff = 0.33;
for y = 1:L
for i=1:2
for j=1:2
s = s + Nc*Br*Vm*Ff/(2*Ac)*((-1)^(i+j)*(log(r(i)+sqrt(r(i)^2+(z(j)-position(y))^2)) - r(i)/sqrt(r(i)^2+(z(j)-position(y))^2)));
end
end
Coupl(y) = s;
end
figure
plot(position,Coupl)
xlabel('Position')
ylabel('Coupling Term')
The correct plot should look similar to the one below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241671/image.png)
0 Commenti
Risposta accettata
Daniel M
il 8 Ott 2019
Very close. You need to reset the value of s for each y.
I also cleaned it up a bit. This now produces the specified figure.
clearvars
close all
clc
position = -0.1:0.001:0.1;
L = length(position);
Coupl = zeros(1,L);
s = 0;
r = [0.01746 0.0247];
z = [-0.0063 0.0063];
Ac = (r(2) - r(1))*(z(2)-z(1));
Nc = 1500;
Br = 1.31;
Vm = 3.218e-6;
Ff = 0.33;
for y = 1:L
for ii=1:2
for jj=1:2
% simplify the formula a bit
x1 = (-1)^(ii+jj);
Zij = sqrt(r(ii)^2 + (z(jj) - position(y))^2);
x2 = log(r(ii)+Zij);
x3 = r(ii)/Zij;
s = s + x1*(x2 - x3);
end
end
% multiply by a constant once (outside of loop)
s = s * Nc*Br*Vm*Ff/(2*Ac);
Coupl(y) = s;
s = 0; % reset value of s
end
figure
plot(position,Coupl)
xlabel('Position')
ylabel('Coupling Term')
5 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!