Practical Work Nut
Practical Work Nut
each
exercise.
---
% Parameters
t0 = 0; % Initial time
y0 = 0; % Initial condition
% Exact solution
figure;
hold on;
for n = n_values
y = zeros(size(t)); % Initialize y
y(1) = y0;
for i = 1:n-1
end
xlabel('t');
ylabel('y(t)');
legend;
grid on;
hold off;
---
% Parameters
n = length(x);
% Coefficients
A = zeros(n, n);
b = zeros(n, 1);
% Boundary conditions
A(1, 1) = 1;
A(end, end) = 1;
b(1) = 0; % c(0)
b(end) = 1; % c(1)
for i = 2:n-1
A(i, i) = -2 / h^2 - 2;
end
c = A \ b;
figure;
plot(x, c, '-o');
xlabel('x');
ylabel('c(x)');
grid on;
---
Exercise 3: Solve the mass diffusion problem using implicit Euler method.
% Parameters
L = 1; % Domain length
T = 1; % Final time
x = 0:hx:L;
t = 0:ht:T;
nx = length(x);
nt = length(t);
u = zeros(nx, nt);
% Coefficients
alpha = ht / hx^2;
% Time-stepping
for k = 1:nt-1
end
figure;
surf(X, T, u');
xlabel('x');
ylabel('t');
zlabel('u(x,t)');
Let me know if you need help implementing or understanding any part of these solutions!