Problem with for-loop and trapz

1 view (last 30 days)
Erik
Erik on 1 Oct 2013
Commented: Sean de Wolski on 8 Oct 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 Comments
Sean de Wolski
Sean de Wolski on 1 Oct 2013
What is sigma? Can you give us full reproduction steps?
Erik
Erik on 7 Oct 2013
Edited: Erik on 8 Oct 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)

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 8 Oct 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 Comments
Erik
Erik on 8 Oct 2013
Edited: Erik on 8 Oct 2013
I tried that, but for some reason "trapz(y)" is just zero.
Sean de Wolski
Sean de Wolski on 8 Oct 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!