create a temperature distribution with a plane wall being cooled by a liquid on the right side and being heated by a heat flux for 0.2 seconds every 1 second on the left side in matlab

Here's an example code in MATLAB to create a temperature distribution for a plane wall being cooled by a liquid on the right side and being heated by a heat flux for 0.2 seconds every 1 second on the left side:

main.m
% Define constants and variables
k = 385;        % Thermal conductivity of wall (W/m.K)
rho = 8900;     % Density of wall (kg/m^3)
cp = 380;       % Specific heat capacity of wall (J/kg.K)
alpha = k/(rho*cp);     % Thermal diffusivity of wall (m^2/s)
dx = 0.01;      % Thickness of wall (m)
dt = 0.005;     % Time step (s)
t_max = 10;     % Total time (s)
L = 1;          % Length of wall (m)

% Define initial temperature distribution (T0) and boundary temperatures
T0 = ones(L/dx, 1)*293;
T0(1:10/dx) = 783;
T0(90/dx:end) = 293;
T_left = 1100;  % Heat flux boundary temperature (K)
T_right = 293;  % Liquid cooled boundary temperature (K)

% Perform simulation
T = T0;
t = 0;
while t < t_max
    T_left_boundary = T(1);
    T(1) = T_left;
    T(end) = T_right;
    
    % Calculate temperature distribution using forward Euler method
    for i = 2:length(T)-1
        T(i) = T(i) + alpha*dt/dx^2*(T(i+1)-2*T(i)+T(i-1));
    end
    
    % Apply heat flux for 0.2 seconds every 1 second on left side
    if mod(t, 1) < 0.2
        T(1) = T_left_boundary;
    end
    
    t = t + dt;
end

% Plot temperature distribution
x = 0:dx:L-dx;
plot(x, T);
xlabel('Distance (m)');
ylabel('Temperature (K)');
title('Temperature distribution of wall');
1243 chars
45 lines

This code defines the thermal properties of the wall, the initial temperature distribution, and performs a simulation to calculate how the temperature distribution changes over time, given the boundary conditions. The temperature distribution is plotted at the end of the simulation.

gistlibby LogSnag