Help, solving simple PDE with ODE45 or ODE23 solver in matlab

In summary, you are trying to solve a moving PDE equation in Matlab, with an initial condition in the form q(x,0)=exp(-(x-5)^2), and you use Fourier transform to discritize the initial condition, then solve the equation with Matlab ODE solver. You use the function rhs to solve for the dispersion relation, but you eventually get rid of it and solve the equation with diskx directly.
  • #1
kribosgiros
4
0
guys please help me, I'm trying to solve a simple moving PDE equation in matlab.
The equation I'm trying to solve is

dq(x,t)/dt=-c*dq(x,t)/dx

with initial condition for example q(x,0)=exp(-(x-5)^2)

c is a constant. What i want to do is to first discritize the initial condition with interval dx then because i have to know the value of -c*dq/dx, i use Fourier transform in MATLAB then i multiply it by -c*i*k after that i invert the Fourier transform back so i get the value of -c*dq/dx at every point in my grid. Then i solve it with MATLAB ODE solver such ODE45 but result is still incorrect, i don't know where i might have gone wrong, here is my code

******the main code********
N=2^14;c0=0.5;
dx=0.01;x=[0:dx:(N-1)*dx];%defining the dx for x
tspan=linspace(0,20,100);%defining the time span for the solution
%defining the initial condition
q0=exp(-(x-5).^2);
dqx = diskx(q0,dx,N,c0);
%calculating the solution
ode=@(t,q) rhs(t,diskx(q0,dx,N,c0)*q);
options = odeset('RelTol',1e-5,'AbsTol',1e-6);
[t,q] = ode23(ode,tspan,q0);
%animating the result
for i=1:length(tspan)
h=plot(x,q(i,:));axis([0 164 -10 11]);
name=['Simpangan q(x,t) saat t=',num2str(t(i))];
title(name);
pause(0.1)
set(h,'EraseMode','xor');
end

******function diskx********
function dxq = diskx(q0,dx,N,c0)
dk=2*pi/((N-1)*dx);
k=[-(N-1)/2*dk:dk:(N-1)/2*dk];
Q0=fft(q0);
Q0T=fftshift(Q0);
dQ0T=-c0*i*k.*Q0T;
dQ0=ifftshift(dQ0T);
dxq=real(ifft(dQ0));

******function rhs********
function dqdt = rhs(t,dqx,q)
dqdt = [dqx'];

please give me hint or correct me if I'm wrong, because I've checked and rechecked that the result suppose to be correct and yet I'm still not getting what i want which is just a moving function with the speed c. thanks guys
 
Physics news on Phys.org
  • #2
Wherefore such complexity? Your equation has well-known general solution

q(x,t) =F(x-t*c), where F is an arbitrary function.

Substituting t=0 you obtain that

F(x)=exp(-(x-5)^2)

so solution with your initial condition is

q(x,t) =exp(-(x-t*c-5)^2)
 
  • #3
that is correct, but this is just the first step from several steps that i want to do. Later on i want to define c as a function of k, which as we know only exists in Fourier domain this is why i try to solve it through Fourier doamain . And this kind of relation is called the dispersion relation in water waves
 
  • #4
So how's going? Have you sort out the problem?

Your problem looks interesting. But I have figure out anything yet. Need to have some time to understand it.
 
  • #5
i got it hehe.. finally, the problem is i just compute my diskx function onece when i should compute it for each time step, and i notice that i don't really need the rhs function so i erase it and put my diskx directly inside my ode45 or 23 bracket and it works perfectly.
 
  • #6
oh and don't forget to change q0 in diskx to q so it compute the dynamics
 

1. How do I set up a PDE in Matlab using ODE45 or ODE23?

To solve a PDE using ODE45 or ODE23 in Matlab, you will need to first create a function that represents the PDE. This function should take in the dependent variables and their derivatives as inputs, and return the values of the derivatives. Then, you can use the "pdepe" function to set up the PDE, specifying the function you created as the input. Finally, you can use ODE45 or ODE23 to solve the PDE numerically.

2. What is the difference between ODE45 and ODE23?

ODE45 and ODE23 are both numerical solvers used to solve ordinary differential equations (ODEs). The main difference between the two is the order of accuracy. ODE45 is a higher order solver (4th order) and is more accurate, while ODE23 is a lower order solver (2nd order) and is less accurate but can be more efficient for some problems.

3. Can ODE45 or ODE23 be used to solve PDEs with variable coefficients?

Yes, ODE45 and ODE23 can be used to solve PDEs with variable coefficients. However, you will need to specify the coefficients as functions in the PDE function you create, so that they can be evaluated at each time step.

4. How do I specify boundary conditions when using ODE45 or ODE23?

To specify boundary conditions when using ODE45 or ODE23, you will need to use the "pdepe" function. This function allows you to specify the boundary conditions as inputs, along with the PDE function and other parameters. Additionally, you can use the "bvpinit" function to specify initial values for the solution at the boundary points.

5. Can I use ODE45 or ODE23 to solve PDEs with non-constant domain boundaries?

Yes, it is possible to use ODE45 or ODE23 to solve PDEs with non-constant domain boundaries. However, you will need to use the "pdepe" function and specify the boundary conditions as functions that can be evaluated at each time step. Additionally, you may need to use the "bvpinit" function to specify initial values for the solution at the boundary points.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
933
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
819
Back
Top