Method of Lines 2D

28 vues (au cours des 30 derniers jours)
Tristan
Tristan le 3 Fév 2023
Modifié(e) : Torsten le 14 Fév 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.

Réponses (1)

Torsten
Torsten le 3 Fév 2023
Modifié(e) : Torsten le 3 Fév 2023
This line is wrong
T(2:99, 2:99) = y;
because of the error message given.
  25 commentaires
Tristan
Tristan le 14 Fév 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 le 14 Fév 2023
Modifié(e) : Torsten le 14 Fév 2023
I like programming more than debugging ... So I didn't check. But as you can see, same coarse resolution, no spikes.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Partial Differential Equation Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by