Info

This question is locked. Reopen it to edit or answer.

Galerkin method fix the linear two-point BVP

6 views (last 30 days)
Cheng hua su
Cheng hua su on 2 Jan 2023
Locked: John D'Errico on 13 Aug 2024
I am a newcomer to matlab,I want to use the Galerkin method with the hat function as the set of basis functions to calculate the solution to the linear two-point BVP :
The hat knots are evenly distributed with the interval h = 1/20 and 1/40.
Compare the results to those of the exact solution, , to evaluate the order of accuracy using
the absolute errors for the two knot intervals.
Hope someone can teach or guide me how to do it .

Answers (1)

Anurag Ojha
Anurag Ojha on 13 Aug 2024
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 )
function galerkin_bvp()
% Define the intervals
intervals = [1/20, 1/40];
% Exact solution
exact_solution = @(t) t.^3;
% Loop over each interval
for h = intervals
% Discretize the interval
t = 0:h:1;
n = length(t);
% Initialize the matrix A and vector b
A = zeros(n, n);
b = zeros(n, 1);
% Fill the matrix A and vector b using the Galerkin method
for i = 2:n-1
A(i, i-1) = 1/h^2;
A(i, i) = -2/h^2;
A(i, i+1) = 1/h^2;
b(i) = 6 * t(i);
end
% Boundary conditions
A(1, 1) = 1;
b(1) = 0;
A(n, n) = 1;
b(n) = 1;
% Solve the linear system
y_approx = A \ b;
% Compute the exact solution
y_exact = exact_solution(t)';
% Compute the absolute errors
abs_errors = abs(y_exact - y_approx);
% Display the results
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 the results
figure;
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)]);
xlabel('t');
ylabel('y');
grid on;
end
end
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
  2 Comments
Torsten
Torsten on 13 Aug 2024
This is Finite Differencing, not Galerkin.
John D'Errico
John D'Errico on 13 Aug 2024
Please don't do an obvious homework assignment for a student who has made no effort. This does not teach the student anything, except how to get someone else to do their assignments for them.
But even more silly, you did not even use the method the student needs.

This question is locked.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!