Multiple outputs from a for loop

1 visualización (últimos 30 días)
Roger Kalvig
Roger Kalvig el 21 de Jun. de 2022
Editada: Stephen23 el 21 de Jun. de 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?
Thanks in advance
Roger Kalvig
  4 comentarios
Roger Kalvig
Roger Kalvig el 21 de Jun. de 2022
Ok seems I need to describe my entire problem instead of decomposing it into pieces.
The goal is to perform numerical integracion of two functions. One of them is my function fcn looking like this:
function y = fcn(x)
Bext = 0.6;
G = 10.553;
if x < 0
y = 0;
elseif x == 0
y = 100;
elseif x == 180
y = 100;
elseif x > 180
y = 0;
else
y = 1./(G.*Bext.*(sind(x)));
end
end
which is simple relationship defined for 3 ranges and 2 x points.
The second function is Gaussian function:
function y = myGausstest(x)
A = 10;
w = 20;
xc = 90;
y = A.*exp(-0.5*((x-xc)./w).^2);
end
Now fcn is always constant, but Gauss is moving along x axis. All parameters of Gauss function remain constat except xc. So my idea was to create a for loop:
for xc = -100:10:280 (some range suiting fcn function)
"do integration here of the product of both functions fcn*myGausstest for integral limts (xc-d,xc+d)" using trapz or integral
end
Finally I need do plot the result
plot(x, result of integration)
Any help is appreciated.
Roger Kalvig

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Jun. de 2022
Editada: Stephen23 el 21 de Jun. de 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
  3 comentarios
Stephen23
Stephen23 el 21 de Jun. de 2022
Editada: Stephen23 el 21 de Jun. de 2022
"One problem i can see is I already defined xc within"
Presumably you only intend the xc inside the function to refer to a single value, not the entire vector, in which case you can easily pass the value by parameterizing the function:
For example:
d = 5;
xc = -100:10:280; % (some range suiting fcn function)
iv = nan(1,numel(xc));
for k = 1:numel(xc)
fh = @(x) fcn(x) .* myGausstest(x,xc(k)); % parameterize function
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 1.7e+02. 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,xc)
A = 10;
w = 20;
y = A.*exp(-0.5*((x-xc)./w).^2);
end
Roger Kalvig
Roger Kalvig el 21 de Jun. de 2022
Thanks very much for help

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by