Temperature matrix giving NaN
Show older comments
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.
Accepted Answer
More Answers (1)
%% 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
Categories
Find more on Creating and Concatenating Matrices 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!