Error: Matrix with NaN values

3 次查看(过去 30 天)
Shivam Yadav
Shivam Yadav 2021-11-8
编辑: Nihal 2024-4-12
clc
clear all
close all
%===get initial wall time:
time0= clock();
format long;
% Simulation cell parameters:
Nx= 960;
Ny= 960;
NxNy= Nx*Ny;
dx= 0.4;
dy= 0.4;
dt=0.003
%-- Cell cordinates
x = zeros(Nx,1);
y = zeros(Ny,1);
for i = 1:Nx
for j = 1:Ny
x(i) =(i-1)*dx;
y(j) =(j-1)*dy;
end
end
[X,Y] = meshgrid(x,y);
%--- Time integration parameters:
nstep = 1500;
nprint = 100;
%--- Material specific parameters:
% Literature parameters
tau0 = 1;
W0 = 1;
D = 10;
epsilon_m= 0.05;
theta0= 0;
m = 4;
seed= 5.0;
U=-0.3;
pix=4.0*atan(1.0);
lambda=D*tau0/(0.6267*W0*W0);
%--Initialize and introduce initial nuclei:
% plot(phi)
[phi,T] = nucleus(Nx,Ny,seed);
figure(1)
imagesc(phi)
colorbar
figure(2)
imagesc(T)
colorbar
%---
%- Evolution
%---
for istep=1:nstep
%----
% calculate the laplacians and epsilon:
%---
for i=1:Nx
for j=1:Ny
jp=j+1;
jm=j-1;
ip=i+1;
im=i-1;
jp=j+1;
jm=j-1;
ip=i+1;
im=i-1;
if(im== 0)
im=Nx;
end
if(ip== (Nx+1))
ip=1;
end
if(jm== 0)
jm=Ny;
end
if(jp== (Ny+1))
jp=1;
end
hne=phi(ip,j);
hnw=phi(im,j);
hns=phi(i,jm);
hnn=phi(i,jp);
hnc=phi(i,j);
hnen=phi(ip,jp);
hnes=phi(ip,jm);
hnwn=phi(im,jp);
hnws=phi(im,jm);
hne=T(ip,j);
hnw=T(im,j);
hns=T(i,jm);
hnn=T(i,jp);
hnc=T(i,j);
txx(i,j) = (T(ip,j) -2*T(i,j)+ T(im,j))./dx^2;
tyy(i,j) = (T(i,jp) -2*T(i,j)+ T(i,jm))./dy^2;
lap_T(i,j)= txx(i,j)+tyy(i,j);
%--gradients:
nx(i,j) = (phi(ip,j) - phi(im,j))/(2*dx);
ny(i,j) = (phi(i,jp) - phi(i,jm))/(2*dy);
nxy(i,j) = (phi(ip,jp) - phi(ip,jm)- phi(im,jp)+ phi(im,jm))/(4*dy*dx);
nxx(i,j) = (phi(ip,j) -2*phi(i,j)+ phi(im,j))/(dx*dx);
nyy(i,j) = (phi(i,jp) -2*phi(i,j)+ phi(i,jm))/(dy*dy);
%-- calculate angle:
theta(i,j)=atan2(ny(i,j),nx(i,j));
%--- W and its derivative:
a(i,j) = 1.0 + epsilon_m*cos(m*(theta(i,j)-theta0));
W(i,j) = W0*a(i,j);
tau(i,j)= tau0*a(i,j)*a(i,j);
W_dthehta(i,j) = -W0*m*epsilon_m*sin(m*(theta(i,j)-theta0));
end %for j
end %for i
phiold(i,j)=phi(i,j);
term0(i,j)= dt./tau(i,j);
term1(i,j)= (phi(i,j)-lambda.*T(i,j)*(1-(phi(i,j)*phi(i,j))))*(1-(phi(i,j)*phi(i,j)));
term2(i,j)= W_dthehta(i,j)*W_dthehta(i,j)+W(i,j)*m*m*(W0-W(i,j));
term3(i,j)= (2*nx(i,j)*ny(i,j)*nxy(i,j)-(nx(i,j)*nx(i,j)*nyy(i,j)+ny(i,j)*ny(i,j)*nxx(i,j)))/(nx(i,j)*nx(i,j)+ny(i,j)*ny(i,j));
term4(i,j)= 2*W(i,j)*W_dthehta(i,j);
term5(i,j)=(nxy(i,j)*(nx(i,j)*nx(i,j)-ny(i,j)*ny(i,j))-nx(i,j)*ny(i,j)*(nxx(i,j)-nyy(i,j)))/(nx(i,j)*nx(i,j)+ny(i,j)*ny(i,j));
term6(i,j)=W(i,j)*W(i,j);
term7(i,j)=nxx(i,j)+nyy(i,j);
phi(i,j)=phi(i,j)+term0(i,j).*(term1(i,j)+term2(i,j).*term3(i,j)+term4(i,j).*term5(i,j)+term6(i,j).*term7(i,j));
T(i,j)=T(i,j)+dt.*(D.*lap_T(i,j)+0.5.*(phi(i,j)-phiold(i,j)));
% end
% end
%---- print results
if(mod(istep,nprint)== 0)
fprintf('donestep:%5d\n',istep);
write_vtk_grid_values(Nx,Ny,dx,dy,istep,phi,T);
end %if
end %istep
%--- calculate compute time:
compute_time=etime(clock(),time0);
fprintf('Compute Time: %10d\n',compute_time);
% Final value of tempr and phi
figure()
imagesc(phi)
colorbar
figure()
imagesc(T)
colorbar
hne, hnw, hns all these matrix are showing Nan values and the other matrix of term 1, term 2, term 3, etc are are forming matrix with values zero which makes no sense. The Equation that I wish to code has also been attached in the attachment here.

回答(1 个)

Nihal
Nihal 2024-4-12
编辑:Nihal 2024-4-12
Hi Shivam,
Given the issue you're facing with NaN values in your matrices (hne, hnw, hns, etc.) and zeroes in your other matrices (term1, term2, term3, etc.), it seems there might be a problem with either the initialization of your variables or the calculations that involve divisions or square roots, which can easily lead to NaN values if not handled properly.
  1. Initialization Check: Ensure all variables are initialized properly before they are used in any operations that could generate NaN values. For instance, dividing by zero or taking the square root of a negative number can result in NaN.
  2. Boundary Conditions: When implementing numerical methods, boundary conditions are critical. Incorrect handling of boundary conditions can lead to NaN values. Ensure that your boundary condition logic (e.g., when im == 0 or ip == Nx+1) does not lead to invalid array indexing or division by zero.
  3. Division by Zero: Check all your division operations to ensure you're not dividing by zero. For example, when calculating gradients or other terms, adding a small number (like eps in MATLAB) to the denominator can prevent division by zero errors.
  4. Square Roots and Logarithms: If your equations involve square roots or logarithms, ensure the arguments to these functions are always valid (e.g., non-negative for square roots).
Debugging Zero Matrices
If your matrices are ending up with zero values, it might be due to:
  1. Initial Conditions: Verify that the initial conditions (initial values of phi, T, etc.) are set correctly and are not trivial (e.g., not all zeros if your system's evolution depends on non-zero initial states).
  2. Overwriting Data: Ensure you're not accidentally overwriting your data with zeros due to incorrect loop logic or variable assignments.
  3. Equation Implementation: Double-check the implementation of your equations. There might be a mistake in translating the equations to code, especially in complex term calculations. Make sure all terms are calculated as intended according to your equations.
General Suggestions
  • Add Debugging Statements: Temporarily insert disp or fprintf statements in your code to print intermediate variables and check where they might be going wrong. This can help you identify the exact point in your code where NaN or unexpected zero values start appearing.
  • Simplify: Temporarily simplify your model by reducing it to a simpler case (e.g., fewer equations, simplified geometry) that you can verify manually. Once the simpler case works as expected, gradually add complexity back.
  • Plotting: Use plots to visualize the evolution of your variables (phi, T, etc.) at each timestep. This can sometimes give you insights into what might be going wrong.
I hope it helps

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by