Problem dividing matrix by vector
    5 次查看(过去 30 天)
  
       显示 更早的评论
    
Hi
I have a matrix of 20301*20301 

and a vector of 20301*1  ...(after 201 iterations, so at cell 202 the numbers start.Before it was all zeros)
...(after 201 iterations, so at cell 202 the numbers start.Before it was all zeros)  
   
 ...(after 201 iterations, so at cell 202 the numbers start.Before it was all zeros)
...(after 201 iterations, so at cell 202 the numbers start.Before it was all zeros)  
   When I try dividing the using A\b, the error "Warning:Singular Matrix .." comes up and my answer is just NaNs. I compared my answer to a friend and while he got the exact values of A and b his division worked and he got actual values. I was wondering if anyone could shed light on this situation 
16 个评论
  the cyclist
      
      
 2020-3-28
				Can you upload your data (in a mat file) and the code you used (in either an m file, or by pasting the code)? Please don't paste an screenshot of the code, which would require us to retype it out to use it.
  the cyclist
      
      
 2020-3-28
				Are you sure that your friend has identical inputs? It seems to me that between you and your friend, you must have at least one of the following, if you are getting different results:
- different inputs
- different calculation
- different versions of MATLAB
- different hardware
(or maybe some other difference I am forgetting).
  Torsten
      
      
 2020-3-28
				Do you really want to solve A*x = b for x or do you only want to divide each row (column) of A by the vector b ?
  Maaz Madha
 2020-3-28
				
      编辑:Maaz Madha
 2020-3-28
  
			clear
clc 
clear all
%%code for temperature distribution over a sheet of metal using discertisation..
%uses central differencing method 
%%Initialisation 1 
alpha=0.1;
beta=0.1;
dx=(2*pi)/100;%%increments
dy=dx;
dt=0.01; %%timeskip
nu=0.1;
H=2*pi;%%height of metal sheet
L=4*pi;%length
T_end=5;
x=[0:dx:L];%%setting up x and y values
y=[0:dx:H];
n=round(size(x,2));
m=round(size(y,2));
t=round((T_end/dt)+1);
v=@(x,t) 5*(1-((x-2*pi)^2)/(4*pi^2))*cos(pi*t)*sin(x);%%velocity distribution
T=@(x,y) 20*cos(x)*sin(y);%%initial temperature
A=zeros(n*m);%%preallocation
b=zeros(n*m,1);
%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;%%discetising (a_north)
        as(i,k)= -v((i-1)*dx,dt)*dt/(2*dy) - beta*dt/dy^2; %a south
    end 
end 
ae = -dt*alpha/dx^2; %a east
ap = 1+(2*alpha*dt/dx^2)+(2*beta*dt/dy^2);%a present
aw=ae; %a west
%T1
for i=2:n-1
    for j=2:m-1
        pointer=(j-1)*n+i;%to map 2d onto 1d
        A(pointer,pointer)=ap;
        A(pointer,pointer-n)=as(i);
        A(pointer,pointer-1)=aw;
        A(pointer,pointer+1)=ae;
        A(pointer,pointer+n)=an(i); 
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%T2
 i=1;
    for j=2:m-1
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,pointer+n)=an(i);
        A(pointer,pointer-n)=as(i);
        A(pointer,pointer+i)=aw+ae;
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
%T3
for i=n
    for j=2:m-1
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,pointer-n)=as(i);
        A(pointer,pointer-1)=aw+ae;
        A(pointer,pointer+n)=an(i);
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%T4 
for i=2:n-1
    for j=1
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,pointer+n)=an(i);
        A(pointer,pointer-1)=aw;
        A(pointer,pointer+1)=ae;
        A(pointer,(m-2)*n+n)=as(i);
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    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);
        A(pointer,pointer-1)=aw;
        A(pointer,pointer+n)=an(i);
        A(pointer,pointer+1)=ae;
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%T6 
for i=1
    for j=1
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,pointer+n)=an(i);
        A(pointer,(m-2)*n+n)=as(i);
        A(pointer,pointer+1)=aw+ae;   
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%T7
for i=n
    for j=1
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,(m-2)*n+n)=as(i);
        A(pointer,pointer-1)=(aw+ae);
        A(pointer,pointer+n)=an(i);
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%T8
for i=1
    for j=m
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,pointer-1)=as(i);
        A(pointer,pointer+1)=(aw+ae);
        A(pointer,n+i)=an(i);
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end 
end 
%t9
for i=n
    for j=m
        pointer=(j-1)*n+i;
        A(pointer,pointer)=ap;
        A(pointer,(m-2)*n+n)=as(i);
        A(pointer,pointer-1)=aw+ae;
        A(pointer,i-n+2)=an(i);
        b(pointer)=T((i-1)*dx,(j-1)*dy);
    end
end 
Temp=A\b;
  Torsten
      
      
 2020-3-28
				an and as are matrices of two dimensions - you address them as arrays of one dimension.
T((i-1)*dx,(j-1)*dy) is usually undefined since (i-1)*dx and (j-1)*dy are not integers in general.
  Maaz Madha
 2020-3-28
				hmmm i see your point about as and an.  About the b vector my purpose was to input data in using the initial temperature at k=1 (time zero) which was 
T=@(x,y) 20*cos(x)*sin(y);%%initial temperature
So my thought was that by doing (i-1)*dx,(j-1)*dy over the whole grid it would give me all the initial temperatures across those points. What would you recommend
  Maaz Madha
 2020-3-28
				i've fixed as and an values 
% Discretisation
for k=2:t
    for i=1:n       
        v(i)=5*(1-((i-1)*dx-2*pi)^2/(4*pi^2))*cos(pi*(k-1)*dt)*sin((i-1)*dx); 
    end 
    as=-v*dt/(2*dy) - beta*dt/dy^2; % Calculate the coefficient matix of Tn(i,j-1)
    an=v*dt/(2*dy) - beta*dt/dy^2;  % Calculate the coefficient matix of Tn(i,j+1)    
    ae = -dt*alpha/dx^2; %discretised value of 'ae'
    ap = 1+(2*alpha*dt/dx^2)+(2*beta*dt/dy^2); %discretised value of 'ap'
    aw=ae; %discretised value of 'aw' which is the same as 'ae'
end 
but despite this the answer produced is still sinfular 
  Torsten
      
      
 2020-3-28
				
      编辑:Torsten
      
      
 2020-3-28
  
			Equations for T2 - T9 must be derived from the boundary conditions of your problem to get a nonsingular matrix A. I don't see where you incorporate boundary conditions.
Further, beginning with time step 2, T in the equations must be replaced by the Temp vector of the preceeding time step. Thus it will be better to work with Temp instead of T in the definition of b.
  Maaz Madha
 2020-3-28
				i used the boundary conditions when i derived the eqns for T2-T9. Using the boundary conditions I had to use the pointer function to manipulate the neighbours of ap(the basis of everything).
I am working with temperature as temperature is T is my code. If you look it says T=@(x,y) 20*cos(x)*sin(y) and this is what I have been using.
  Torsten
      
      
 2020-3-28
				Which boundary conditions did you try to incorporate at the four edges ?
T in your code is the initial temperature at time t=0. If you want to stick to T in your b vector, you will have to redefine your function for T since T becomes Temp.
  Maaz Madha
 2020-3-28
				
      编辑:Maaz Madha
 2020-3-28
  
			In the Q, the boundary conditions are  and T is periodic
 and T is periodic 
 and T is periodic
 and T is periodic 
回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)





