FTCS code shows Error using zeros

19 visualizaciones (últimos 30 días)
Mingyi Shao
Mingyi Shao el 22 de Mzo. de 2020
Editada: Mingyi Shao el 20 de Abr. de 2020
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

Respuesta aceptada

Sriram Tadavarty
Sriram Tadavarty el 22 de Mzo. de 2020
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 comentarios
Mingyi Shao
Mingyi Shao el 22 de Mzo. de 2020
Ok! That solved it! Thanks
Sriram Tadavarty
Sriram Tadavarty el 22 de Mzo. de 2020
Do accept the answer, if helped

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by