griddata command returns all NaN values

14 views (last 30 days)
V
V on 15 Oct 2021
Commented: Walter Roberson on 16 Oct 2021
Hi,
I am trying to create a contour plot from X and Y values that correspond to a theoretical temperature (Tt) value. I am trying to use the griddata command to interpolate the data so it can be graphed. But, the command returns NaN for most every value. I could really use some help. Thank you.
W = 6;
L = 5;
X1 = zeros(100,1);
Y1 = zeros(100,1);
Tt = zeros(100,1);
b = 1;
for a = 1:100
X1(a) = (5/200).*a;
c1 = rem(a,50);
if c1 == 0
Y1(a) = (6/200).*b;
b = b + 1;
else
Y1(a) = (6/200).*b;
end
end
end
if a == 100
for m = 1:100
syms m1
Tt_xy = 425.8343968*symsum((2/pi).*(((-1).^(m1+1)+1)./m1).*sin(m1.*pi.*(X1(m)./L)).*(sinh(m1.*pi.*(Y1(m)./L))./sinh(m1.*pi.*(W./L))),m1,[1 5]);
Tt(m) = Tt_xy;
end
end
n1 = 100;
[x1,y1] = meshgrid(linspace(.0250,5,n1),linspace(.03, 6,n1));
f2 = griddata(X1,Y1,Tt,x1,y1);
f3 = figure;
[C1, h1] = contour(x1,y1,f2);
clabel(C1,h1);
  2 Comments
Image Analyst
Image Analyst on 15 Oct 2021
Do you need symbolic variables? Can't you do it with actual numerical variables?
V
V on 15 Oct 2021
Edited: V on 15 Oct 2021
No I do not need symbolic variables. I need numerical variables. Is that where I am making a mistake? If so I'm not sure how to fix that? Sorry I'm not the greatest at MatLab.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 Oct 2021
for a = 1:100
X1(a) = (5/100).*a;
X1 is linear in a.
c = a/50;
a is a double precision integer, 1 to 100. c is the integer divided by 50. c will always be real-valued.
c1 = isreal(c);
if c1 == 1
so that branch is always going to be taken
Y1(a) = (6/100).*b;
Y1 in this branch is linear in b. But this branch is the only one that can get executed, so all Y1 are linear in b.
b = b + 1;
and b is increment by 1 each time the branch is met. But the branch is met every iteration. So every time, through, b will be the same as a at the time of the calculation of Y1. So X1 and Y1 are both linear in a .
else
Y1(a) = (6/100).*b;
This branch is never executed.
end
end
Your X1 and Y1 are now the same as if you had done
a = 1:100;
X1 = (5/100) .* a;
Y1 = (6/100) .* a;
This is a straight line of values, not even one value that is off of the line.
Later, you interpolate over an entire grid. But the values to interpolate from are strictly co-linear. There is no information available for sidewise movement, so everything off-line that is interpreted is going to be NaN.
  2 Comments
V
V on 15 Oct 2021
Wow ok, I made a dumb mistake. I was using "isreal" to see if there was a remainder of the division of "a" into 50. In otherwords I only want b to advance when it has reached a whole number, or every 50 iterations. However, that was the wrong command. Thank you so much for the help.
However, now after correcting the formula and using "rem" to test if the remainder is 0 before iterating my "f2" is still returning mostly NaN values. I am still confused if I'm missing something
for a = 1:200
X1(a) = (5/200).*a;
c1 = rem(a,50);
if c1 == 0
Y1(a) = (6/200).*b;
b = b + 1;
else
Y1(a) = (6/200).*b;
end
end
if a == 200
for m = 1:200
syms m1
Tt_xy = 425.8343968*symsum((2/pi).*(((-1).^(m1+1)+1)./m1).*sin(m1.*pi.*(X1(m)./L)).*(sinh(m1.*pi.*(Y1(m)./L))./sinh(m1.*pi.*(W./L))),m1,[1 5]);
Tt(m) = Tt_xy;
end
end
n1 = 50;
[x1,y1] = meshgrid(linspace(.0250,5,n1),linspace(.03, 6,n1));
f2 = griddata(X1,Y1,Tt,x1,y1);
f3 = figure;
[C1, h1] = contour(x1,y1,f2);
axis([0.001 5 0.001 6]);
clabel(C1,h1);
Walter Roberson
Walter Roberson on 16 Oct 2021
griddata() was the wrong function for that situation.
W = 6;
L = 5;
X1 = zeros(100,1);
Y1 = zeros(100,1);
Tt = zeros(100,1);
b = 1;
for a = 1:200
X1(a) = (5/200).*a;
c1 = rem(a,50);
if c1 == 0
Y1(a) = (6/200).*b;
b = b + 1;
else
Y1(a) = (6/200).*b;
end
end
if a == 200
for m = 1:200
syms m1
Tt_xy = 425.8343968*symsum((2/pi).*(((-1).^(m1+1)+1)./m1).*sin(m1.*pi.*(X1(m)./L)).*(sinh(m1.*pi.*(Y1(m)./L))./sinh(m1.*pi.*(W./L))),m1,[1 5]);
Tt(m) = Tt_xy;
end
end
n1 = 50;
[x1,y1] = meshgrid(linspace(.0250,5,n1),linspace(.03, 6,n1));
%f2 = griddata(X1,Y1,Tt,x1,y1);
F = scatteredInterpolant(X1, Y1, Tt);
f2 = F(x1, y1);
f3 = figure;
[C1, h1] = contour(x1,y1,f2);
axis([0.001 5 0.001 6]);
clabel(C1,h1);

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!