Hi, in my code, in the third loop, somehow the variable:
z_i_meshgrid
always have half of it fill with NaNs, can anyone explain this plz? why doesnt this happen in the first few loops.
Many thanks,
x_0=1;
y_0=6;
gamma=0.2;
tau=0.0002;
x_grid=-9:0.2:9;
y_grid=-8:0.2:8;
[X, Y] = meshgrid(x_grid,y_grid);
z_meshgrid=cos(X/2).*cos(Y) + Y/10 - X/5;
%%
interpolation_accuracy=5;
[x_meshgrid,y_meshgrid]=meshgrid(x_grid,y_grid);
x_i_location=find(x_grid==x_0);
y_i_location=find(y_grid==y_0);
z_0=z_meshgrid(y_i_location,x_i_location);
clear x_grid y_grid;
[gradient_x_grid,gradient_y_grid] = gradient(z_meshgrid);
gradient_zx_0 = gradient_x_grid(y_i_location,x_i_location)
gradient_zy_0 = gradient_y_grid(y_i_location,x_i_location)
cnt=1;
A=[x_0,y_0,z_0];
threshold = tau+1;
while threshold > tau
% calculation of (x_i,y_i)
x_i = x_0 - gamma*gradient_zx_0;
y_i = y_0 - gamma*gradient_zy_0;
% Interpolation around the point of (x_i,y_i)
x_i_grid=linspace(x_0,(2*x_i-x_0),interpolation_accuracy);
y_i_grid=linspace(y_0,(2*y_i-y_0),interpolation_accuracy);
[x_i_meshgrid, y_i_meshgrid] = meshgrid(x_i_grid,y_i_grid);
z_i_meshgrid = interp2(x_meshgrid,y_meshgrid,z_meshgrid,...
x_i_meshgrid,y_i_meshgrid,'cubic');
if cnt==1
x_i_location = sum(size(x_i_grid)/2);
y_i_location = sum(size(y_i_grid)/2);
end
z_i=z_i_meshgrid( y_i_location , x_i_location );
%output
cnt=cnt+1
A(cnt,:)=[x_i,y_i,z_i];
%new gradient
gradient_x_grid = interp2(x_meshgrid,y_meshgrid,gradient_x_grid, ...
x_i_meshgrid,y_i_meshgrid);
gradient_x_i=gradient_x_grid( y_i_location , x_i_location )
gradient_y_grid = interp2(x_meshgrid,y_meshgrid,gradient_y_grid, ...
x_i_meshgrid,y_i_meshgrid);
gradient_y_i=gradient_y_grid( y_i_location , x_i_location )
threshold = sqrt( (x_i-x_0)^2+(y_i-y_0)^2 );
if cnt== 3
break
end
x_0=x_i;
y_0=y_i;
x_meshgrid=x_i_meshgrid;
y_meshgrid=y_i_meshgrid;
z_meshgrid=z_i_meshgrid;
end
A

 Réponse acceptée

Matt J
Matt J le 16 Jan 2019
Modifié(e) : Matt J le 16 Jan 2019

0 votes

Because you are interpolating at locations outside the boundaries defined by x_grid,y_grid. The locations where you interpolate, defined by x_i_grid, y_i_grid, vary from iteration to iteration and will not necessarily lie inside x_grid,y_grid for any apparent reason.

1 commentaire

Qiang Liu
Qiang Liu le 16 Jan 2019
Many thanks, that was indeed the problem is, thanks a lot for a quick answer, that was rly rly rly helpful

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by