Temperature matrix giving NaN

3 views (last 30 days)
Izk
Izk on 22 Apr 2022
Edited: Torsten on 22 Apr 2022
clc
clear
close all
%% initialisation
alpha1=0.02;
a=0.6;
b=0.05;
c=0.01;
q=0.05;
g=20;
L=4;
tmax=100;
dx=0.01;
nx=L/dx+1;
dt=0.005;
nt=tmax/dt+1;
x=0:dx:L;
t=0:dt:tmax;
ap=1/dt+2*alpha1/dx^2;
T=zeros(nx,nt);
f=zeros(nx,1);
v=zeros(nt,1);
S=zeros(nx,1);
That is my code. I want to know what is wrong with my intialisation because my Temperature matrix keeps giving me NaN.
  2 Comments
Torsten
Torsten on 22 Apr 2022
What's the PDE you are trying to solve with your code ?
Izk
Izk on 22 Apr 2022
The PDE is above. This is the task.

Sign in to comment.

Accepted Answer

Sam Chak
Sam Chak on 22 Apr 2022
The T matrix gives NaN because the temperature T is an array of zeros to begin with
T = zeros(nx, nt);
and f is a scalar that returns a zero value at the end of the loop
f = T(i, k-1)/dt + S(i);
Thus, causing the following to give some Inf and NaN.
T = A/f
T =
Inf Inf NaN -Inf NaN
-Inf Inf Inf NaN NaN
NaN -Inf Inf Inf NaN
NaN NaN -Inf Inf Inf
NaN Inf NaN -Inf Inf
  6 Comments
Izk
Izk on 22 Apr 2022
Edited: Izk on 22 Apr 2022
clc
clear
close all
%% initialisation
alpha1=0.02;
a=0.6;
b=0.05;
c=0.01;
q=0.05;
g=20;
L=4;
tmax=100;
dx=0.01;
nx=L/dx+1;
dt=0.005;
nt=tmax/dt+1;
x=0:dx:L;
t=0:dt:tmax;
ap=1/dt+2*alpha1/dx^2;
T=zeros(nx,nt);
T(:,1)=g;
f=zeros(nx,1);
v=zeros(nt,1);
S=zeros(nx,1);
The matrix disappears after this and I get message saying:
'NaN/Inf breakpoint hit for workspacefunc.m on line 310.
310 v = evalin('caller', varargin{1});
I'm not sure what part is causing this.
Torsten
Torsten on 22 Apr 2022
Remove the line
v = zeros(nt,1)
These were the obvious errors in your code.
Now you will have to check whether the matrix A is correct and the boundary conditions are correctly implemented.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 22 Apr 2022
Edited: Torsten on 22 Apr 2022
%% initialisation
alpha1=0.02;
a=0.6;
b=0.05;
c=0.01;
q=0.05;
g=20;
L=4;
tmax=100;
dx=0.01;
nx=L/dx+1;
dt=0.005;
nt=tmax/dt+1;
x=(0:dx:L).';
t=0:dt:tmax;
T=zeros(nx,nt);
T(:,1)=g;
A=zeros(nx,nx);
f=zeros(nx,1);
%% main body
for k=2:nt
v=a+b*cos(0.5*pi*t(k))+c*cos(4*pi*t(k));
A = diag((1 + 2*alpha1*dt/dx^2)*ones(1,nx))+...
diag((v*dt/(2*dx) - alpha1*dt/dx^2)*ones(1,nx-1),1)+...
diag((-v*dt/(2*dx) - alpha1*dt/dx^2)*ones(1,nx-1),-1);
A(1,nx-1) = -v*dt/(2*dx) - alpha1*dt/dx^2;
A(nx,2) = v*dt/(2*dx) - alpha1*dt/dx^2;
f(1:nx) = T(1:nx,k-1)+dt*q*exp(-0.5*((x(1:nx)-L/2)/0.05).^2);
T(:,k) = A\f;
end
plot(x,[T(:,100),T(:,200),T(:,2000),T(:,10000)])
end

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!