Professional Documents
Culture Documents
2nd Question
2nd Question
>> % Parameters
L = 2; % Length of the rod
T = 1; % Final time
h = 1/4; % Spatial step size
k = 1/40; % Time step size
N = round(L/h) + 1; % Number of spatial points (rounded to the nearest integer)
M = round(T/k) + 1; % Number of time steps (rounded to the nearest integer)
% Initial condition
u0 = zeros(N, 1);
u0(1:N/2) = 1;
% Boundary conditions
u0(1) = 0;
u0(end) = 0;
% Plotting
x = linspace(0, L, N);
plot(x, u, 'LineWidth', 2);
title('Solution of Heat Equation at t = 1' );
xlabel('x');
ylabel('u(x,1)');
ylim([0, 1.2]); % Adjust ylim as needed
grid on;
Warning: Integer operands are required for colon operator when used as index.
>>