//////////////////////////////////////////////////////////////////////////////
// RÉSOLUTION NUMÉRIQUE EDO PREMIER ORDRE
// Forme : y'(t) + a(t)y(t) = f(t)
//////////////////////////////////////////////////////////////////////////////
function [t, y] = resolver_edo1(a, f, y0, tmin, tmax, n)
h = (tmax - tmin) / n;
t = zeros(1, n+1);
y = zeros(1, n+1);
t(1) = tmin;
y(1) = y0;
for k = 1:n
t(k+1) = t(k) + h;
y(k+1) = y(k) + h * (f(t(k)) - a(t(k)) * y(k));
end
endfunction
//////////////////////////////////////////////////////////////////////////////
// ÉQUATION (E1): y'(t) = cos(t), y(0) = 0
//////////////////////////////////////////////////////////////////////////////
// Définition des fonctions pour (E1)
deff("z = a1(t)", "z = 0");
deff("z = f1(t)", "z = cos(t)");
deff("z = sol_exacte1(t)", "z = sin(t)");
// Paramètres (E1)
tmin1 = 0; tmax1 = 10*%pi; y01 = 0;
// Résolution pour différentes valeurs de n
n_values = [5, 10, 100, 1000];
for i = 1:length(n_values)
n = n_values(i);
[t, y] = resolver_edo1(a1, f1, y01, tmin1, tmax1, n);
// Tracé
scf();
plot(t, y, 'ro-', 'markersize', 4, 'linewidth', 1);
// Solution exacte
tt = linspace(tmin1, tmax1, 1000);
yy_exacte = sol_exacte1(tt);
plot(tt, yy_exacte, 'b-', 'linewidth', 2);
xlabel('t');
ylabel('y(t)');
title('E1: y''(t) = cos(t) - n = ' + string(n));
legend(['Solution approchée'; 'Solution exacte'], 2);
xgrid(1);
end
// Analyse pour n = 5
disp("=== ANALYSE POUR n = 5 (E1) ===");
[t5, y5] = resolver_edo1(a1, f1, y01, tmin1, tmax1, 5);
mprintf("Points t(k): "); disp(t5);
mprintf("cos(t(k)): "); disp(cos(t5));
mprintf("y(k) calculés: "); disp(y5);
//////////////////////////////////////////////////////////////////////////////
// ÉQUATION (E2): y'(t) + y(t) = 3e^(-t), y(0) = 1
//////////////////////////////////////////////////////////////////////////////
// Définition des fonctions pour (E2)
deff("z = a2(t)", "z = 1");
deff("z = f2(t)", "z = 3*exp(-t)");
deff("z = sol_exacte2(t)", "z = (1 + 3*t).*exp(-t)");
// Paramètres (E2)
tmin2 = 0; tmax2 = 5; y02 = 1;
// Résolution pour différentes valeurs de n
n_values = [10, 100, 1000];
for i = 1:length(n_values)
n = n_values(i);
[t, y] = resolver_edo1(a2, f2, y02, tmin2, tmax2, n);
// Tracé
scf();
plot(t, y, 'ro-', 'markersize', 4, 'linewidth', 1);
// Solution exacte
tt = linspace(tmin2, tmax2, 1000);
yy_exacte = sol_exacte2(tt);
plot(tt, yy_exacte, 'b-', 'linewidth', 2);
xlabel('t');
ylabel('y(t)');
title('E2: y'' + y = 3e^{-t} - n = ' + string(n));
legend(['Solution approchée'; 'Solution exacte'], 2);
xgrid(1);
end