I am trying to write a script that will solve the 1D heat equation for a variety of boundary conditions (constant T, constant flux, etc.) and initial conditions. My goal is for the user of the program to enter these conditions through the command window.
I am running into an issue where the user is being prompted for the initial condition many times before the solver will actually do its business. The number of times the initial condition is requested seems to equal the number of mesh points being used for the x-axis.
I am also starting to worry that I will have this same issue when I start prompting the user for the boundary conditions and other variables (right now I just have them hard-coded in for simplicity while I built up the script).
Main function:
%Declare global variables
global thermal_diff length time T_ic x t%User specified data (general)
%Thermal diffusivity in m^2/s
thermal_diff = 2.3e-05;%Length in meters
length = 1;%Time in seconds
time = 60;%Initial condition
T_ic = @(x) input('Please input an initial condition.\nT(x,0) = ');%Retreive solution from appropriate function
sol = TPS_cT_cT();%Plot graph of solution
T = sol(:,:,1);surf(x,t,T) title('Numerical Solution')xlabel('Distance (m)')ylabel('Time (s)')zlabel('Temperature (K)')
PDE function:
function [c, f, s] = qeq_pde(x, t, T, dTdx)%qed_pde specifies heat equation using physical parameters
%Retrieve gloabl variables
global thermal_diff%Specify PDE
c = 1/(sqrt(thermal_diff));f = dTdx;s = 0;end
Initial Condition function:
function [T0] = TPS_ic(x)%TPS_ic specifies initial condition for heat equation
%Retrieve global variables
global T_ic%Specify initial condition
T0 = T_ic(x);end
Constant Temp/Constant Temp specific function:
function [sol] = TPS_cT_cT(~)%TPS_cT_cT solves PDE for cT/cT boundary conditions
%Declares global variables
global T1 T2 length time x t%User specified data (specific to cT/cT)
%Constant temperatures in K
T1 = 300;T2 = 200;%Sets mesh points
x = linspace(0, length, 20);t = linspace(0, time, 5);%Solves heat equation for constant temperature conditions (change later)
m = 0;sol = pdepe(m, @qeq_pde, @TPS_ic, @TPS_cT_cT_bc, x, t);end
Boundary Condition function:
function [pl, ql, pr, qr] = TPS_cT_cT_bc(xl, Tl, xr, Tr, t)%TPS_cT_cT_bc specifies boundary conditions for cT/cT case
%Retrieves global variables
global T1 T2%Specify boundary conditions
pl = Tl - T1;ql = 0;pr = Tr - T2;qr = 0;end
Best Answer