Reason why this condition is generating NaN everywhere?
Show older comments
Hi all, my code is not working correctly. I suspect it is because of a condition I set in the final if statement, but I am not sure how to modify it to run correctly. This code takes velocity data from PIV and uses xcorr2 to find a characteristic length scale. When I call it outside the script, it fails to pass the final if statement, and everything turns into a NaN value. Does anyone know what I am doing incorrectly? I am a newbie so any help will be appreciated!
The first block is the function, and the second block is where I call it
code:
function [lambda,L] = length_scale(v_x,v_y,mesh)
b = size(v_x);
c = min(b);
vx= v_x(1:c,1:c);
vy= v_y(1:c,1:c);
N = xcorr2(ones(c,c));
corr_x= xcorr2(vx)./N;
corr_y= xcorr2(vy)./N;
[X,Y] = meshgrid(-c+1,c-1);
[~,rho] = cart2pol(X,Y);
for i = 1:c
F(i)=mean(corr_x(i-1<=rho && rho<i) + corr_y(i-1<=rho && rho<i));
end
L=NaN.*ones(1,c);
if (max(isnan(F)) == 0) && (min(F)<0)
L=F./F(1);
f=fit((0:c-1).', L(1:c).', 'exp1', 'Starpoint', [0 0]);
lambda = -mesh/f.b;
else
lambda=NaN;
end
end
calling it:
count=1;
for i =1:cell_size
velocity = (vel_x{i}.^2+vel_y{i}.^2).^0.5;
avg_v(i) = mean(velocity, 'all', 'omitnan');
std_vel(i) = std(velocity, 1, 'all', 'omitnan');
[temp_lambda, Len(i,:)] = length_scale(vel_x{i},vel_y{i},mesh);
lambda(count) = temp_lambda.*(pxsize);
% uncomment these lines when the length scale script works
count = count + 1;
end
4 Comments
Abderrahim. B
on 15 Jul 2022
Hi!
Tried to run this code to see if I can help. Seems that the code you shared is incomplete.
cell_size = ?
vel_x = ?
vel_y = ?
Please share the correct code.
Hriday Talreja
on 15 Jul 2022
Hriday Talreja
on 15 Jul 2022
dpb
on 15 Jul 2022
What it is "essentially" is of little help -- unless you can supply a dataset that illustrates the problem, you're asking folks to do your debugging for you completely from scratch. That's just not a reasonable expectation for code given with no explanation of what it is trying to do nor even a single comment.
The condition
if (max(isnan(F)) == 0) && (min(F)<0)
looks flaky to me at best -- the first could more concisely be written as
if all(isfinite(F))
but if these are correlations, one wouldn't expect a negative necessarily.
Again, it's going to depend upon the data although it also appears you may not be defining everything although again without data it's too tough to try to debug.
Use the debugger and set breakpoints and see where your logic fails to produce what you expect.
Answers (0)
Categories
Find more on Fluid Dynamics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!