FTCS code shows Error using zeros
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics 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!