Summing results at the end of a loop

1 次查看(过去 30 天)
Lena
Lena 2024-5-16
评论: VBBV 2024-5-17
I have a piece of code I want to execute 3 times, changing a set of two variables to different values each time, and then add the results of the three at the end to give the final y-value and plot it.
This is the full code right now.
%Constants
L = 79.14.*(10.^-3);
N = 58;
I = 0.7;
mu = 4.*pi*(10.^-7);
% Range and steps of x-values to plot:
x = linspace(0, 90.*(10.^-3), 100);
%Variables;
w = 7.5.*(10.^-3);
u = (8.883).*(10.^-3);
%Calculation;
a = (mu.*I.*N)/(2.*L.*(u-w));
b = sqrt((u.^2)+(x.^2));
c = sqrt((u.^2)+((x-L).^2));
d = sqrt((w.^2)+(x.^2));
e = sqrt((w.^2)+((x-L).^2));
f = (b+u)./(d+w);
g = x.*log(f);
h = (c+u)./(e+w);
i = (x-L).*log(h);
j = (mu.*I.*N)/(2.*L.*(u-w));
y = j.*(g-i);
%Plotting;
figure;
plot(x, y);
xlabel('Position Along the Center Axis (m)');
ylabel('Magnetic Field (T)');
title('Magnetic Field Along the Central Axis of the Solenoid');
What I want to do is repeat the code for 3 sets of values of u and w, add the three results at the end to give the final value of y, and then plot it against x.
I thought about using for loops but I can't figure out how to sum the values at the end.
  1 个评论
VBBV
VBBV 2024-5-17
%Constants
L = 79.14.*(10.^-3);
N = 58;
I = 0.7;
mu = 4.*pi*(10.^-7);
% Range and steps of x-values to plot:
x = linspace(0, 90.*(10.^-3), 100);
%Variables;
w = [7.5.*(1e-3); rand(1)*7.5.*(1e-3); rand(1)*7.5.*(1e-3)];
u = [(8.883).*(1e-3);rand(1)*(8.883).*(1e-3);rand*(8.883).*(1e-3)];
x = linspace(0, 90*1e-3, 100);
for k = 1:numel(w)
%Calculation;
a = (mu.*I.*N)/(2.*L.*(u(k)-w(k)));
b = sqrt((u(k)^2)+(x.^2));
c = sqrt((u(k)^2)+((x-L).^2));
d = sqrt((w(k)^2)+(x.^2));
e = sqrt((w(k)^2)+((x-L).^2));
f = (b+u(k))./(d+w(k));
g = x.*log(f);
h = (c+u(k))./(e+w(k));
i = (x-L).*log(h);
j = (mu.*I.*N)/(2.*L.*(u(k)-w(k)));
y(k,:) = j.*(g-i);
end
y = sum(y,1)
y = 1x100
0.0010 0.0011 0.0012 0.0014 0.0015 0.0015 0.0016 0.0017 0.0017 0.0017 0.0018 0.0018 0.0018 0.0018 0.0018 0.0018 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019 0.0019
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%Plotting;
figure;
plot(x, y);
xlabel('Position Along the Center Axis (m)');
ylabel('Magnetic Field (T)');
title('Magnetic Field Along the Central Axis of the Solenoid');

请先登录,再进行评论。

回答(1 个)

Torsten
Torsten 2024-5-16
编辑:Torsten 2024-5-16
x = linspace(0, 90.*(10.^-3), 100);
w = [7.5.*(10.^-3),7.5.*(10.^-3),7.5.*(10.^-3)];
u = [(8.883).*(10.^-3),(8.883).*(10.^-3),(8.883).*(10.^-3)];
y = zeros(numel(w),numel(x));
for i = 1:numel(w)
y(i,:) = fun(x,w(i),u(i));
end
sum_y = sum(y,1);
%Plotting;
figure;
plot(x, sum_y);
xlabel('Position Along the Center Axis (m)');
ylabel('Magnetic Field (T)');
title('Magnetic Field Along the Central Axis of the Solenoid');
function y = fun(x,w,u)
%Constants
L = 79.14.*(10.^-3);
N = 58;
I = 0.7;
mu = 4.*pi*(10.^-7);
% Range and steps of x-values to plot:
%x = linspace(0, 90.*(10.^-3), 100);
%Variables;
%w = 7.5.*(10.^-3);
%u = (8.883).*(10.^-3);
%Calculation;
a = (mu.*I.*N)/(2.*L.*(u-w));
b = sqrt((u.^2)+(x.^2));
c = sqrt((u.^2)+((x-L).^2));
d = sqrt((w.^2)+(x.^2));
e = sqrt((w.^2)+((x-L).^2));
f = (b+u)./(d+w);
g = x.*log(f);
h = (c+u)./(e+w);
i = (x-L).*log(h);
j = (mu.*I.*N)/(2.*L.*(u-w));
y = j.*(g-i);
end

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by