Troubles with ode45 solving two ODEs?

1 次查看(过去 30 天)

I am solving two the first order ODEs:

$$p_0 p_0'=-\dfrac{32 \beta}{R^4}$$

$$(p_0p_1)'=-\dfrac{2-\sigma_v}{\sigma_v}\dfrac{8}{R}p_0'$$

I wrote following matlab code for solving it with ode45

    function f= odefun_moja(Z,P)
    P=zeros(2,1)
    p0=P(1);
    p1=P(2);
    beta=1;
    R=2;
    sig=1;
    %%%   EQUATIONS!!!
    dp0dz=-32*beta/(p0*R^4);
    dp1dz=(-((2-sig)/sig)*8*dp0dz/R-dp0dz*p1)/p0;
    %%%   EQUATIONS!!!
    f=[dpodz; dp1dz];

But after run with

    function main_moja
    z0=0;
    zf=100;
    zspan=[z0,zf];
    y0=[1 0; 1 0];
    options = odeset('RelTol', 10.0^(-7), 'AbsTol' , 10.0^(-7));
    [Z,P]=ode45(@odeset_moja,zspan,y0,options);

I got his message:

    > Error using odearguments (line 92) ODEFUN_MOJA returns a vector of
    > length 1, but the length of initial conditions vector is 4. The vector
    > returned by ODEFUN_MOJA and the initial conditions vector must have
    > the same number of elements.

When I try to solve first only the first equation, with code:

    function f= odefun_moja(Z,P)
    P=zeros(1,1)
    p0=P(1);
    beta=1;
    R=2;
    sig=1;
    %%%   EQUATIONS!!!
    dp0dz=-32*beta/(p0*R^4);
    %%%   EQUATIONS!!!
    f=[dp0dz];

Error message is this:

    Error using feval
    Error: File: odefun_moja.m Line: 15 Column: 1
    Function definitions are not permitted in this context.
    Error in odearguments (line 87)
    f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

My questions:

1. Is that good approach for solving this system, start with the first equation, solve it for p0, and after that try to solve second equation? Or use both equations in code would be better?

2. I have next conditions $p_0|_{z=0}=p_{0i}$ and $p_0|_{z=1}=1$. According to literature it is necessary to find $p_0'|_{z=0}$ with shooting method, according to already mentioned $p_0|_{z=1}=1$. *How to connect this two conditions* and shoot $p_0'|_{z=0}$ for already known $p_0|_{z=1}=1$? *Is that condition necessary*, because this is first order equation, is there only one initial condition enough? (It will be the same process for second equation and $p_1$.)

采纳的回答

Torsten
Torsten 2018-5-3
1. Solve both equations together:
a) Use
f=[dp0dz; dp1dz];
instead of
f=[dpodz; dp1dz];
b) Use
y0=[1 0];
instead of
y0=[1 0; 1 0];
2. This is a boundary value problem. Use "bvp4c" instead of "ode45".
Best wishes
Torsten.
  2 个评论
I G
I G 2018-5-3
I cannot find mistake in my equations, this is the last question I promise, but ode45 doesn`t work:
function f=fun(z,p)
R=2; sig=1; beta=1;
f(1)=-32*beta/(R^4*p(1));
f(2)=(-(2-sig)*8*f(1)/(sig*R)-f(1)*p(2))/p(1);
[zv,pv]=ode45('fun',[1 0],[1; 0])
Is it possible to call term on left side of the first equation in the second equation? What can be wrong here?
Torsten
Torsten 2018-5-3
function f=fun(z,p)
R=2; sig=1; beta=1;
f=zeros(2,1);
f(1)=-32*beta/(R^4*p(1));
f(2)=(-(2-sig)*8*f(1)/(sig*R)-f(1)*p(2))/p(1);
[zv,pv]=ode45(@fun,[1 0],[1; 0])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by