Problem with for-loop and trapz
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!