0% found this document useful (0 votes)
9 views5 pages

Practical Work Nut

Practical work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Practical Work Nut

Practical work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

To solve the exercises in the provided TP4 file using MATLAB, I'll outline the MATLAB scripts for

each
exercise.

---

Exercise 1: Solve the ODE using the explicit Euler method.

% Parameters

t0 = 0; % Initial time

tf = pi; % Final time

y0 = 0; % Initial condition

n_values = [11, 101, 1001]; % Different n values

% Exact solution

exact_solution = @(t) log(t.^2 + 1);

figure;

hold on;

for n = n_values

h = (tf - t0) / (n - 1); % Step size

t = linspace(t0, tf, n); % Time vector

y = zeros(size(t)); % Initialize y

y(1) = y0;

% Euler Explicit Method

for i = 1:n-1

y(i+1) = y(i) + h * (t(i)^2 * exp(-y(i)));


end

% Plot numerical solution

plot(t, y, 'DisplayName', ['n = ', num2str(n)]);

end

% Plot exact solution

t_exact = linspace(t0, tf, 1000);

plot(t_exact, exact_solution(t_exact), 'k--', 'LineWidth', 1.5, 'DisplayName', 'Exact Solution');

xlabel('t');

ylabel('y(t)');

legend;

grid on;

title('Euler Explicit Method vs Exact Solution');

hold off;

---

Exercise 2: Solve the boundary value problem using finite differences.

% Parameters

x = linspace(0, 1, 11); % Uniform grid

h = x(2) - x(1); % Step size

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)

% Fill in the matrix using centered scheme

for i = 2:n-1

A(i, i-1) = 1 / h^2 - 1 / (2 * h);

A(i, i) = -2 / h^2 - 2;

A(i, i+1) = 1 / h^2 + 1 / (2 * h);

end

% Solve the system

c = A \ b;

% Plot the solution

figure;

plot(x, c, '-o');

xlabel('x');

ylabel('c(x)');

grid on;

title('Solution of Boundary Value Problem');

---
Exercise 3: Solve the mass diffusion problem using implicit Euler method.

% Parameters

L = 1; % Domain length

T = 1; % Final time

hx = 0.1; % Spatial step

ht = 0.1; % Time step

x = 0:hx:L;

t = 0:ht:T;

nx = length(x);

nt = length(t);

% Initial and boundary conditions

u = zeros(nx, nt);

u(1, :) = cos(2 * pi * t); % Left boundary

u(end, :) = cos(2 * pi * t); % Right boundary

u(:, 1) = 0; % Initial condition

% Coefficients

alpha = ht / hx^2;

A = (1 + 2 * alpha) * eye(nx-2) - alpha * diag(ones(nx-3, 1), 1) - alpha * diag(ones(nx-3, 1), -1);

% Time-stepping

for k = 1:nt-1

u_inner = u(2:end-1, k);

u(2:end-1, k+1) = A \ u_inner;

end

% Plot the results


[X, T] = meshgrid(x, t);

figure;

surf(X, T, u');

xlabel('x');

ylabel('t');

zlabel('u(x,t)');

title('Mass Diffusion Solution');

Let me know if you need help implementing or understanding any part of these solutions!

You might also like