Problem with for-loop and trapz

조회 수: 1 (최근 30일)
Erik
Erik 2013년 10월 1일
댓글: Sean de Wolski 2013년 10월 8일
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