Implementing numerical method for PDE
Afficher commentaires plus anciens
Hello
I am trying to solve the following PDE
with intital and boundary conditions such that
. I used the second order centered finite difference discrtization for
and then want solve the ode system using ode15s
with intital and boundary conditions such that
and then want solve the ode system using ode15sHere is my attempt. When I plot the solution obtained from ode15s and compare it to the exact solution they are different. I am not if I made a mistake somewhere. Help is really appreciated
clc,clear,close all
% parameters
t0 = 0;
T = 1.0;
tspan = [t0 T];
xl = 0;
xr = 1;
m = 20;
x = linspace(xl,xr,m + 1);
dx = 1/m;
Uexact = @(t,x) exp(1i*(x-t));
% initial conditions
U0 = Uexact(0,x)';
U0 = U0(2:end-1);
% solve
fn = @(t,U) ODE(t,U,m,dx);
opts = odeset('RelTol',1e-13, 'AbsTol',1e-15);
[t,U] = ode15s(fn, tspan, U0, opts);
%compare with exact solution
plot(x(2:end-1),U(end,:))
hold on
plot(x(2:end-1),Uexact(T,x(2:end-1)))
function dUdt = ODE(t,U,m,dx)
A = eye(m-1);
A = A * (-2);
A = A + diag(ones(m-2,1),1);
A = A + diag(ones(m-2,1),-1);
A = (1/dx^2) * A;
g = zeros(m-1,1);
g(1) = g(1) + (1/dx^2) * exp(1i*(-1*t));
g(end) = g(end) + (1/dx^2) * exp(1i*(1-t));
dUdt = (1i) * (A*U) + g;
end
Thanks
2 commentaires
Davide Masiello
le 13 Oct 2022
What are exactly the boundary conditions?
Davide Masiello
le 13 Oct 2022
Nevermind, I got it, see answer below.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Ordinary Differential Equations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

