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

  • Context: MATLAB 
  • Thread starter Thread starter kribosgiros
  • Start date Start date
  • Tags Tags
    Matlab Ode45 Pde
Click For Summary

Discussion Overview

The discussion revolves around solving a simple moving partial differential equation (PDE) using MATLAB's ODE solvers, specifically ODE45 and ODE23. Participants explore the numerical implementation of the PDE, initial conditions, and the use of Fourier transforms in the solution process.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes their approach to solving the PDE using Fourier transforms and MATLAB's ODE solvers, detailing their code and the initial condition.
  • Another participant suggests a well-known general solution to the PDE, indicating that the solution can be expressed in terms of an arbitrary function.
  • A later reply emphasizes the intention to define the constant c as a function of k, relating this to dispersion relations in water waves.
  • One participant expresses uncertainty about the problem and requests more time to understand it.
  • Another participant reports resolving their issue by realizing they needed to compute the diskx function at each time step rather than once, and they modified their code accordingly.
  • A follow-up comment reminds the participant to adjust the input to the diskx function to reflect the evolving dynamics of q.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solving the PDE, as there are multiple methods discussed, including direct analytical solutions and numerical methods. The discussion includes both agreement on the general solution and differing views on the implementation details.

Contextual Notes

The discussion highlights the complexity of numerical methods for PDEs and the importance of correctly implementing time-dependent calculations. There are unresolved aspects regarding the dependence of the constant c on k and its implications for the solution.

Who May Find This Useful

This discussion may be useful for individuals interested in numerical methods for solving PDEs, particularly in the context of MATLAB programming and Fourier analysis.

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 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K