FTCS code shows Error using zeros

17 ビュー (過去 30 日間)
Mingyi Shao
Mingyi Shao 2020 年 3 月 22 日
編集済み: Mingyi Shao 2020 年 4 月 20 日
The error displays:
Error using zeros
Size inputs must be scalar.
Error in ftcsHeatEquation (line 32)
Ynum = zeros(1,size(x)); % Solution at timestep n
function [Ynum, errMean] = ftcsHeatEquation(nx,nt)
L =10; % Length of domain [m]
D =1.76E-5; % Diffusivity constant [m^2/s]
%analytical
Ya=zeros(1,nx);
Ys=zeros(1,nx);
y0=0.78; %y when x=0
nf=100; %given
% Calculate the dx and dt terms
x = linspace(0,L,nx); % x-domain [m]
dx = L/(nx-1); % x-domain spacing [m]
dt=1;
sigma = D*dt/(dx^2);
% Initialise all necessary vectors
Y=zeros(nt,nx);
Ynum = zeros(1,size(x)); % Solution at timestep n
Ynp1 = zeros(1,size(x)); % Solution at timestep n+1
Ynum(1:end-1) = 1; %initial
Ynum(end) = 0.78;
Ynp1(1) = -0.22; %boundary
Ynp1(end) = (-0.22/L)*x;
tEnd=dt*nt;
% Solve for the numerical solution
for n = 2:nt
% Loop over internal points
for i= 2:nx-1
Ynp1(i) = Ynum(i)-sigma*yn(i+1)-2*Ynum(i)+Ynum(i-1);
end
% Update solution for next timestep
Ynum = Ynp1;
end
end

採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 22 日
Hi Mingyi,
The error you observe is due to the incorrect usage of zeros function.
This can be solved with any of the following options:
% Option 1: Directly using nx, instead of size
Ynum = zeros(1,nx);
Ynp1 = zeros(1,nx);
% Option 2: Using length instead of size
Ynum = zeros(1,length(x));
Ynp1 = zeros(1,length(x));
% Option 3: Using the zise of x, without any other dimensions placed
Ynum = zeros(size(x));
Ynp1 = zeros(size(x));
% if you are ware that Ynp1 is same size as Ynum,
% you directly assign Ynp1 = Ynum;, rather than defining separately as Ynum
Hope this helps.
Regards,
Sriram
  4 件のコメント
Mingyi Shao
Mingyi Shao 2020 年 3 月 22 日
Ok! That solved it! Thanks
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 22 日
Do accept the answer, if helped

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by