Warning: Matrix is singular to working precision. (Finite difference method)
1 view (last 30 days)
Show older comments
Hi!
I have a problem with the following code.
In the end the output u contains N-1 elements "Nan".
Does this warning depend on the input value u0? (I insert the input u0=1)
function [x,u]= fdnlin(N,u0, tol)
%x partition of the intervall
%u values of the approximated solution
%u0 is the initial solution
h=2/N;
x=-1:h:1;
uk=u0*ones(N,1); %because u0 is insered and can be a vector or a scalar
%In this way we have a coloumn vector with the same value u0
nfk=1+tol; %norm of phik. We set it in this way to enter in the while cycle.
while nfk>=tol
J=zeros(N);
phi=zeros(N,1); %coloumn vector
J(1,1)=1+h/4*exp(-x(2))*(uk(2)+ exp(-1)); %is D1
J(1,2)= -0.5+h/4* exp(-x(2))*uk(1); %is C1
phi(1)=-0.5*uk(2)+uk(1)+exp(-1)/2+h/4*exp(-x(2))*uk(1)*(uk(2)+ exp(-1))-...
h^2/2*exp(x(2))*(x(2)^2-2); %the non therm
for n=2:N-1
J(n,n)=1+h/4*exp(-x(n+1))*(uk(n+1)- uk(n-1)); %Dn
J(n,n+1)=-0.5+h/4* exp(-x(n+1))*(uk(n)); %Cn
J(n,n-1)=-0.5-h/4* exp(-x(n+1))*(uk(n));%En
phi(n)= -0.5*uk(n+1)+uk(n)-1/2*uk(n-1)+h/4*exp(-x(n+1))*uk(n)*(uk(n+1)-uk(n-1))-...
h^2/2*exp(x(n+1))*((x(n+1)^2-2));
end
J(N,N)= 1+h+h^2/2*exp(-x(N+1))*(3*exp(1)- 2*uk(N)); %DN
J(N,N-1)=-1; %EN
phi(N)=-uk(N-1)+(1+h)*uk(N)+h^2/2*exp(-x(N+1))*uk(N)*(3*exp(1)-uk(N))-...
h^2/2*exp(x(N+1))*((x(N+1)^2-2));
duk=J\phi; %solution of linear sistem (delta uk)
uk=uk+duk; %Eulero's method
nfk=sqrt(sum(phi.^2)); %norm of phi
end
u=[-exp(-1); uk]; %insert u0 in the first position
end
0 Comments
Answers (0)
See Also
Categories
Find more on Logical 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!