Warning: Matrix is singular to working precision Error
4 views (last 30 days)
Show older comments
Tatiane Cristine Antonio
on 22 Apr 2020
Commented: Tatiane Cristine Antonio
on 23 Apr 2020
Hello! I'm having throuble solving this system of equations using the implicit method for this a PDE.
Could someone help me to figure out how to fix my code?
Thank you in advance!
%%
clc;
clear;
close all;
%% Parameters
x0=0;
xn=1;
t=2;
tstep=0.04;
xstep=0.04;
nsteps_for_t=t/tstep;
nsteps_for_x=xn/xstep;
nx=nsteps_for_x+1;
nt=nsteps_for_t+1;
x = 0:xstep:1; % defining x-scale
t = 0:tstep:2; % defining t-scale
%% Defining the first three time-levels
%First time-level (Based on given initial Condition)
t_1= sin(pi*x)+0.5*sin(3*pi*x);
t_1(1,nsteps_for_x+1)=0; %from u(1,t)=0
%Second time-level
t_2= cos(pi*tstep)'*sin(pi*x)+0.5*cos(3*pi*tstep)'*sin(3*pi*x);
t_2(1,nsteps_for_x+1)=0; %from u(1,t)=0
%Third time-level
t_3= cos(pi*(2*tstep))'*sin(pi*x)+0.5*cos(3*pi*(2*tstep))'*sin(3*pi*x);
t_3(1,nsteps_for_x+1)=0; %from u(1,t)=0
%% Creating matrix A and B
%Inserting boundary conditions into matrix A
A=zeros(nx,nx);
A(1,:)=0;
A(nx,:)=0;
for i=2:nsteps_for_x
A(i,i-1)=(-tstep^2)/xstep^2;
A(i,i)=2+((2*tstep^2)/xstep^2);
A(i,i+1)=(-tstep^2)/xstep^2;
end
%Inserting boundary conditions into matrix B
B=zeros(nx,nx);
B(1,:)=0;
B(nx,:)=0;
for i=2:nsteps_for_x
B(i,i-1)=5;
B(i,i)=-4;
B(i,i+1)=1;
end
%% Finding the numerical solution (u_numer)
for i=1:nsteps_for_t
b(:,1)=t_1;
b(:,2)=t_2;
b(:,3)=t_3;
C=B*b;
u_numer=A\C;
end
%% Error
Difference_A_N = abs(u_numer-u_analit);
ERROR=(1/(n+1))*(sum(Error_A_N,'all'))
0 Comments
Accepted Answer
Walter Roberson
on 22 Apr 2020
The first and the last rows of B are all zero, because
B=zeros(nx,nx);
B(1,:)=0;
B(nx,:)=0;
Nothing other than zeros in the first and last row. You do not even need those assignments of 0 because you initialized it all to 0.
WIth the first and last row being the same (no matter whether it was all 0 or all (say) 8.7234309, then you can be sure that the matrix will be singular. But even if one of the two rows was different (such as if you had stored something non-zero in the last row) then you know that a row or a column that is all 0 guarantees that the matrix will be singular.
The same logic holds for A as well: you can be sure that it is singular matrix.
With B being singular, C=B*b will be singular, and we know that A is singular. So whether do A\C or A/C we know that both sides are singular and so neither \ nor / will be able to give a meaningful result.
You could do
u_numer = pinv(A)*C;
but you have to ask yourself if that is meaningful.
You are doing this in for i loop that does not involve i in any way, so you are repeating exactly the same calculation each time, which is not useful.
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!