have to implement 1-D heat equation using FTCS my code isnt getting the figure i need ?

1 view (last 30 days)
Clc;close all
%Initialiazing the Heat Equation
%Defining the constants and initial conditions
X0 = 0; X = 2; %maximum length in X direction
T0 = 0; T = 2; %final Time
m = 20; %number of cells
Alpha = 0.25; %Given Thermal Diffusivity Constant
dx = 0.1; %dx=h , using given value of h
dt = 0.01; %dt=k , using given value of k
%Calculating the grid size
mx = (X-X0)/dx+1; %number of nodes in x direction
nt = ceil((T-T0)/dt+1); %number of time steps or nodes in y direction
V = alpha*k/(h*h); %Constant used in calculating next point
%implementing the Output Matrix
W1 = sparse(mx,nt); % W(X,T) Numerical Solution
%executing the Initial Condition and Boundary Conditions
f = @(x) 100;
x = X0:dx:X; %value of steps in x direction
t = T0:dt:2; %value of time steps in y direction
W1(:,1) = f(x); %setting the initial condition at x=0
W1(1,:) = 0; W1(mx,:) = 0; %setting the boundary condition at x=L
A = sparse((V*eye(mx-2)zeroes(mx-2,2))+(zeros(mx-2,1)*(1-2*V)*eye(mx-2)zeros(mx-2,1))+(zeros(mx-2,2)
V*eye(mx-2)));
for ii=2:nt
W1(2:mx-1,ii) = A*W1(:,ii-1);
end
%Plotting the solution
figure(1)
imagesc(x,t,W1);
colorbar;
title("1D HEAT EQUATION SOLUTION USING FTCS METHOD");
xlabel('Time(T)');
ylabel('Space(X)');
  1 Comment
Ravi Narasimhan
Ravi Narasimhan on 8 Sep 2021
Edited: Ravi Narasimhan on 8 Sep 2021
I tried your code and got a bunch of errors.
Clc should be clc.
There are a lot of missing multiplication operators (I think) in the sparse command. Depending on what you're multiplying (arrays or scalars) you will have to look into the .* and * operators. If you're multiplying arrays, the array sizes need to be compatible with matrix multiplication.
k and h are not defined so
V = Alpha*k/(h*h)
doesn't work, etc.

Sign in to comment.

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!