現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Problem dividing matrix by vector
1 回表示 (過去 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)
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
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)