Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jan 22, 2010 #1
    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. thx guys
     
  2. jcsd
  3. Jan 22, 2010 #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)
     
  4. Jan 22, 2010 #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
     
  5. Feb 4, 2010 #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.
     
  6. Feb 18, 2010 #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.
     
  7. Feb 18, 2010 #6
    oh and don't forget to change q0 in diskx to q so it compute the dynamics
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook