Hey Cheng
Kindly find the code below to use the Galerkin method with hat functions to solve the given boundary value problem (BVP) in MATLAB.
Also have compared the results with the exact solution ( y = t^3 )
intervals = [1/20, 1/40];
exact_solution = @(t) t.^3;
y_exact = exact_solution(t)';
abs_errors = abs(y_exact - y_approx);
fprintf('Results for h = %f:\n', h);
fprintf('Max Absolute Error: %e\n', max(abs_errors));
fprintf('Mean Absolute Error: %e\n\n', mean(abs_errors));
plot(t, y_exact, 'b', 'LineWidth', 2); hold on;
plot(t, y_approx, 'ro--');
legend('Exact Solution', 'Galerkin Approximation');
title(['Galerkin Method with h = ', num2str(h)]);
Results for h = 0.050000:
Max Absolute Error: 2.722128e-15
Mean Absolute Error: 1.475812e-15
Results for h = 0.025000:
Max Absolute Error: 8.523030e-15
Mean Absolute Error: 4.261312e-15