# Multiple outputs from a for loop

1 view (last 30 days)
Roger Kalvig on 21 Jun 2022
Edited: Stephen23 on 21 Jun 2022
Basic problem, but I can't find the solution:
consider basic a for loop:
for i = 1:5
z(i)=3*i
end
It produces a vector z= [3 6 9 12 15]. What I need is all the components of the vector z as a sperate variables(scalars), let,s say
z1=3, z2=6 and so on. How can I do that?
Roger Kalvig
Stephen23 on 21 Jun 2022

Stephen23 on 21 Jun 2022
Edited: Stephen23 on 21 Jun 2022
Rather than distracting with anti-pattern numbered variables (like you asked about), instead you should just use basic, normal, efficient, easy, simple, standard indexing. Indexing is how MATLAB works best.
d = 5;
xc = -100:10:280; % (some range suiting fcn function)
iv = nan(1,numel(xc));
fh = @(x) fcn(x) .* myGausstest(x);
for k = 1:numel(xc)
% do integration here of the product of both functions fcn*myGausstest for integral limts (xc-d,xc+d)" using trapz or integral
iv(k) = integral(fh,xc(k)-d,xc(k)+d);
end
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 6.7e-03. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
Warning: Minimum step size reached near x = 180. There may be a singularity, or the tolerances may be too tight for this problem.
plot(xc,iv) function y = fcn(x)
Bext = 0.6;
G = 10.553;
y = 1./(G.*Bext.*sind(x));
y(x<0) = 0;
y(x==0) = 100;
y(x==180) = 100;
y(x>180) = 0;
end
function y = myGausstest(x)
A = 10;
w = 20;
xc = 90;
y = A.*exp(-0.5*((x-xc)./w).^2);
end
Roger Kalvig on 21 Jun 2022
Thanks very much for help