Borrar filtros
Borrar filtros

Problem with for-loop and trapz

1 visualización (últimos 30 días)
Erik
Erik el 1 de Oct. de 2013
Comentada: Sean de Wolski el 8 de Oct. de 2013
Hello,
I have a problem with using a function generated by a for-loop in the trapz-function. The loop looks like:
for a = 1:1:m
for b = 1:1:m
y = y + a .* b .* exp(-(b.^2 + a.^2)./(4 .* m.^2 .* sigma.^2)) .* besseli(0,sigma) .* b .* a ./(2 .* m.^2 .* sigma.^2);
end
end
I use the resulting y in the trapz-function
trapz(sigma,y)
with a defined 'sigma' there is the error: Undefined function 'max' for input arguments of type 'sym'.
But if I just copy the result of the loop:
f = (50.*exp(-1./(2.*sigma.^2)).*besseli(0, sigma))./sigma.^2 + (576.*exp...
trapz(sigma,f)
it gives me the right result. How can I do it without copying the result from the loop? Thanks for your help!
  2 comentarios
Sean de Wolski
Sean de Wolski el 1 de Oct. de 2013
What is sigma? Can you give us full reproduction steps?
Erik
Erik el 7 de Oct. de 2013
Editada: Erik el 8 de Oct. de 2013
syms a b sigma;
m = 10;
for a = 1:1:m
for b = 1:1:m
y = y + a .* b .* exp(-(b.^2 + a.^2)./(4 .* m.^2 .* sigma.^2)) .* besseli(0,sigma) .* b .* a ./(2 .* m.^2 .* sigma.^2)
end
end
sigma = 0.1:0.1:1;
trapz(sigma,y)

Iniciar sesión para comentar.

Respuestas (1)

Sean de Wolski
Sean de Wolski el 8 de Oct. de 2013
You need to subs-stitute the values in for sigma:
syms a b sigma y;
m = 10;
for a = 1:1:m
for b = 1:1:m
y = y + a .* b .* exp(-(b.^2 + a.^2)./(4 .* m.^2 .* sigma.^2)) .* besseli(0,sigma) .* b .* a ./(2 .* m.^2 .* sigma.^2);
end
end
sigma_values = 0.1:0.1:1;
subs(y,sigma,sigma_values);
trapz(y)
  2 comentarios
Erik
Erik el 8 de Oct. de 2013
Editada: Erik el 8 de Oct. de 2013
I tried that, but for some reason "trapz(y)" is just zero.
Sean de Wolski
Sean de Wolski el 8 de Oct. de 2013
I don't really know what you're trying to do. You have sigma in your equation for y. If you want to substitute this with the values in sigma_values, use subs like I did. If you then want to integrate this with respect to sigma values, you can do that as well:
syms a b sigma y;
m = 10;
y = sym.empty;
for a = 1:1:m
for b = 1:1:m
y = y + a .* b .* exp(-(b.^2 + a.^2)./(4 .* m.^2 .* sigma.^2)) .* besseli(0,sigma) .* b .* a ./(2 .* m.^2 .* sigma.^2);
end
end
sigma_values = 0.1:0.1:1;
Z = trapz(sigma_values,subs(y,sigma,sigma_values))
double(Z);
But I have no clue what you want so it's just a shot in the dark.

Iniciar sesión para comentar.

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