Search code examples
matlabbuilt-indifferential-equationsfluid-dynamics

Nonlinear differential equation, how do I solve this numerically in MATLAB?


I have been working on a project where I need to find a solution to the given nonlinear differential equation, see figure below:

enter image description here Now, I have tried using matlabs built-in function bvp4c, but the syntax is difficult and I don't know if the results are reliable. For some values the bvp4c function only generates an error. I also have boundary conditions to take into consideration, see figures below:

enter image description here

enter image description here

I am sorry for the terrible sizes of the figures. Now I know this is not a mathforum, but I need to numerically solve this and I want to solve it with the best method available. My code as of now is seen below:

function [theta_0 y2]=flow_BVP

theta_0=linspace(0,1,1000); % pi/18
solinit2=bvpinit(theta_0,[0 1 1]);
sol2=bvp4c(@flow_ode,@flow_bc,solinit2);
x2=sol2.x;
y2=sol2.y(1,:);


hold on
%plot(x1,y1) %gammal
plot(x2,y2) %ny
%hold off


function v=flow_init(x)
v=[sin(x); 1; 1];

function dydx=flow_ode(x,y)
q=0.0005;
v=1;
dydx = [y(2); y(3); 2*q/v*y(1)*y(2)-4*y(2)];

function res=flow_bc(ya,yb)
res=[ya(1);yb(1);ya(2)-5.59];

To repeat my question, which is the best method, easiest, simplest to understand and implement to solve this? Shooting perhaps?

Best regards SimpleP.

Edit The result I have gotten so far is this enter image description here

The plot shows f vs. \theta . The integral is 1, as it should be.


Solution

  • The generic way to incorporate the integral is to add the anti-derivative F of f to the ODE system. That is, as fourth component and variable

    F' = f  with   F(0)=0,  F(alpha)=1
    

    and the other components need to be shifted one index,

    function v=flow_init(x)
    v=[sin(x); 1; 1; 1-cos(x)];
    
    function dydx=flow_ode(x,y)
    % y is [ f, f', f'', F ]
    q=0.0005;
    v=1;
    dydx = [y(2); y(3); 2*q/v*y(1)*y(2)-4*y(2); y(1)];
    
    function res=flow_bc(ya,yb)
    res=[ya(1); ya(4); yb(1); yb(4)-1];
    

    Using python:

    q, v = 0.0005, 1
    def flow_ode(t,u): return [ u[1], u[2], 2*q/v*u[0]*u[1]-4*u[1], u[0] ]
    
    def flow_bc(u0, u1): return [ u0[0], u0[3], u1[0], u1[3]-1 ]
    
    x = x_init = np.linspace(0,1,11);
    u_init = [ 6*x*(1-x), 0*x, 0*x, x ]
    
    res = solve_bvp(flow_ode, flow_bc, x_init, u_init, tol = 1e-5)
    
    print res.message
    if res.success:
         x = x_sol = np.linspace(0,1,201);
         u_sol = res.sol(x_sol);
         plt.subplot(2,1,1)
         plt.plot(x_sol, u_sol[0]); plt.plot(x, 6*x*(1-x), lw=0.5); plt.grid()
         plt.subplot(2,1,2)
         plt.plot(x_sol, u_sol[3]); plt.grid()
         plt.show()
    

    plot of solution and anti-derivative

    As one can see, the initial guess here is quite close. As the ODE is a small perturbation of 4f'+f'''=0, the solution has to be close to its solution a+b*sin(2x)+c*cos(2x) which with the boundary conditions evaluates to

    f(x)=A * [ (1-cos(2))*sin(2*x)-sin(2)*(1-cos(2*x)) ]
        = 4*A*sin(1) * sin(x)*sin(1-x)
    

    with A such that the integral is one.


    If the parameter values in the ODE were switched, q=1 and v=0.0005, the solution would have boundary layers of size sqrt(v/q). plot of another scenario