Problem with for-loop and trapz

1 次查看(过去 30 天)
Erik
Erik 2013-10-1
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 个评论
Sean de Wolski
Sean de Wolski 2013-10-1
What is sigma? Can you give us full reproduction steps?
Erik
Erik 2013-10-7
编辑:Erik 2013-10-8
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)

请先登录,再进行评论。

回答(1 个)

Sean de Wolski
Sean de Wolski 2013-10-8
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 个评论
Erik
Erik 2013-10-8
编辑:Erik 2013-10-8
I tried that, but for some reason "trapz(y)" is just zero.
Sean de Wolski
Sean de Wolski 2013-10-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by