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 CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by