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

  • Thread starter Thread starter kribosgiros
  • Start date Start date
  • Tags Tags
    Matlab Ode45 Pde
AI Thread Summary
The discussion focuses on solving a moving partial differential equation (PDE) in MATLAB using ODE solvers. The equation involves a Fourier transform to compute the spatial derivative, but the user initially encounters incorrect results. It is revealed that the user needed to compute the derivative function at each time step rather than just once, and they also discovered that the right-hand side function could be simplified. After making these adjustments, the user successfully obtained the correct solution for the moving function. The conversation highlights the importance of updating calculations dynamically in numerical simulations.
kribosgiros
Messages
4
Reaction score
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
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)
 
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
 
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.
 
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.
 
oh and don't forget to change q0 in diskx to q so it compute the dynamics
 

Similar threads

Replies
2
Views
2K
Replies
7
Views
3K
Replies
4
Views
2K
Replies
6
Views
4K
Replies
3
Views
2K
Back
Top