MATLAB Answers

Getting NaN after division of matrix by vector

5 views (last 30 days)
Maaz Madha
Maaz Madha on 26 Mar 2020
So for my assignment I had to form a matrix A and divide it by a column vector b to get the answer needed and plot it. Problem is that when I try to plot my answeri t gives nAn for all the values and i dont know why.
%%Initialisation 1
alpha=0.1;
beta=0.1;
dx=(2*pi)/100;
dy=dx;
dt=0.01; %%timeskip
nu=0.1;
H=2*pi;
L=4*pi;
T_end=5;
%Mesh
n=round((L/dx)+1);. Converts rectangle to a mesh
m=round(((H/dy)+1));
t=round((T_end/dt)+1);
x=[0:dx:L]';
y=[0:dy:H]';
v=@(x,t) 5*(1-(x-2*pi)^2/(4*pi^2)).*cos(pi*t)*sin(x);
T=@(x,y) 20*cos(x)*sin(y);
b=zeros(n*m,1);
A=zeros(n*m);
%A matrix
for i=1:n
for k=1:t
an(i,k)= v((i-1)*dx,dt)*dt/(2*dy) - beta*dt/dy^2;
as(i,k)= -v((i-1)*dx,dt)*dt/(2*dy) - beta*dt/dy^2;
end
end
ae = -dt*alpha/dx^2;
ap = 1+(2*alpha*dt/dx^2)+(2*beta*dt/dy^2);
aw=ae;
%T1
for i=2:n-1
for j=2:m-2
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer-n)=as(i,k);
A(pointer,pointer-1)=aw;
A(pointer,pointer+1)=ae;
A(pointer,pointer+n)=an(i,k);
end
end
%T2
for i=1
for j=2:m-2
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+n)=as(i,k);
A(pointer,pointer-1)=aw;
A(pointer,pointer+1)=ae;
A(pointer,pointer-m+2)=an(i,k);
end
end
%T3
for i=n
for j=2:m-2
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+m-2)=as(i,k);
A(pointer,pointer-1)=aw;
A(pointer,pointer+1)=ae;
A(pointer,pointer+n)=an(i,k);
end
end
%T4
for i=2:n-1
for j=1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+n)=as(i,k);
A(pointer,pointer+1)=(aw+ae);
A(pointer,pointer+n)=an(i,k);
end
end
%for T5
for i=2:n-1
for j=m-1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer-n)=as(i,k);
A(pointer,pointer-1)=(aw+ae);
A(pointer,pointer+n)=an(i,k);
end
end
%T6
for i=1
for j=1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+n)=as(i,k);
A(pointer,pointer+1)=(aw+ae);
A(pointer,pointer+2)=an(i,k);
end
end
%T7
for i=n
for j=1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+m-2)=as(i,k);
A(pointer,pointer+1)=(aw+ae);
A(pointer,pointer+n)=an(i,k);
end
end
%T8
for i=1
for j=m-1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+n)=as(i,k);
A(pointer,pointer-1)=(aw+ae);
A(pointer,pointer+2-m)=an(i,k);
end
end
%t9
for i=n
for j=m-1
pointer=(j-1)*n+i;
A(pointer,pointer)=ap;
A(pointer,pointer+m-2)=as(i,k);
A(pointer,pointer-n)=(aw+ae);
A(pointer,pointer+n)=an(i,k);
end
end
%matrix A produces 2301*2301
%%vector B
for i=1:n
for j=1:m
pointer=(j-1)*n+i;
b(pointer)=T((i-1)*dx,(j-1)*dy);
end
end
%%produces 2301*1
Temp=A/b
The Temp, when calculated,gives NaN for all the cells and I don't know why. I would appreciate some feedback.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by