Clear Filters
Clear Filters

Method of Lines 2D

17 views (last 30 days)
Tristan
Tristan on 3 Feb 2023
Edited: Torsten on 14 Feb 2023
So I am trying to solve the 2D heat equation with no source term using method of lines. So basically I want an ODE at each grid point, and solve those using one of the ODE solvers. My code for trying to run the function is below.
T0(1,:) = 35;
T0(:,1) = 0;
T0(100,:) = 0;
T0(:, 100) = 0;
tspan = [0 20];
y0 = T0(2:99, 2:99);
y0 = reshape(y0, [], 1);
[tsol, Tsol] = ode45( @ (t,y) functionheat(t,y), tspan, y0);
The code for the function is here:
function fval = functionheat(t,y)
T(1,:) = 35;
T(:,1) = 0;
T(100,:) = 0;
T(:, 100) = 0;
T(2:99, 2:99) = y;
dx = 0.1/100;
dy = 0.1/100;
dTdt = zeros(100,100);
for i = 2:(nx-1)
for j = 2:(ny-1)
Txx = (T(i+1,j) - 2*T(i,j) + T(i-1,j))/(dx^2);
Tyy = (T(i,j+1)-2*T(i,j)+T(i,j-1))/(dy^2);
dTdt(i,j) = Txx + Tyy;
end
end
fval1 = dTdt(2:99, 2:99);
fval = reshape(fval1, [],1);
Originally I just had fval = dTdt(2:99, 2:99);, but it was giving me this error, that I am still getting after adding a new line. The error is below
"Unable to perform assignment because the size of the left side is 98-by-98 and the size of the right side
is 9604-by-1." What exactly needs to change, I am not sure what is still a 98 by 98 matrix because I have reshaped the derivative array which is ultimately what is being solved.

Answers (1)

Torsten
Torsten on 3 Feb 2023
Edited: Torsten on 3 Feb 2023
This line is wrong
T(2:99, 2:99) = y;
because of the error message given.
  25 Comments
Tristan
Tristan on 14 Feb 2023
Oh okay, wow! This looks great.
So my previous reshaping was wrong? Or what change do you think was able to fix those pods away from the bounday?
Torsten
Torsten on 14 Feb 2023
Edited: Torsten on 14 Feb 2023
I like programming more than debugging ... So I didn't check. But as you can see, same coarse resolution, no spikes.

Sign in to comment.

Categories

Find more on Programming 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!