Summing results at the end of a loop
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.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. 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. solenoid, physics, matlab, for loop, iteration MATLAB Answers — New Questions