FTCS code shows Error using zeros
20 views (last 30 days)
Show older comments
Mingyi Shao
on 22 Mar 2020
Edited: Mingyi Shao
on 20 Apr 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
0 Comments
Accepted Answer
Sriram Tadavarty
on 22 Mar 2020
Hi Mingyi,
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 Comments
More Answers (0)
See Also
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!