Multiple Summation of Series using For Loops

1 次查看(过去 30 天)
Hello all,
I am trying to sum the following series for various positions, y being the position in the function.
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:

采纳的回答

Daniel M
Daniel M 2019-10-8
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 个评论
Daniel M
Daniel M 2019-10-8
I do stuff with magnet design so I was just curious.
RPS19
RPS19 2019-11-4
Hi Daniel, Could you possibly take a look at an updated version of this question if you have a chance, I realise I should have just amended this question my apologies for that..

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by