Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Math Help - Matlab differential equations

  1. #1
    Junior Member
    Joined
    Apr 2009
    Posts
    45

    Matlab differential equations

    Hi,

    I have a problem with an itterative set of differential equations and im not sure how to put them in mat lab.

    the three functions are :

    u =[0.25*s]/[0.005 + s] ... 1

    dxdt = ux ...2

    dsdt=-0.42*ux ...3

    It starts by inputting s0 into equations 1 as 0.1, and generating a value for u.

    This value of u generated is then used in equation 2 to get a new value for x.

    Then this is used to get a new s, then s to get a new u and the process is repeated.

    This will show me the change in x and s over time.


    I started with the m file for u, which worked fine.

    But when i tried to put the function in for dxdt into an mfile i couldnt do it.

    I was wondering if anyone could provide me any help

    Thanks Rem
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rem88 View Post
    Hi,

    I have a problem with an itterative set of differential equations and im not sure how to put them in mat lab.

    the three functions are :

    u =[0.25*s]/[0.005 + s] ... 1

    dxdt = ux ...2

    dsdt=-0.42*ux ...3

    It starts by inputting s0 into equations 1 as 0.1, and generating a value for u.

    This value of u generated is then used in equation 2 to get a new value for x.

    Then this is used to get a new s, then s to get a new u and the process is repeated.

    This will show me the change in x and s over time.


    I started with the m file for u, which worked fine.

    But when i tried to put the function in for dxdt into an mfile i couldnt do it.

    I was wondering if anyone could provide me any help

    Thanks Rem
    This is a first order system with state vector:

     X={X_1 \brack X_2}={x \brack s}

    with derivative:

    X'={\frac{0.25X_2X_1}{0.005+X_2} \brack \frac{0.45\times 0.25 X_2X_1}{0.005+X_2}}

    Now look up the help for ode45

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Thanks!

    I dont knw why i didnt see that.

    I have been trying to do it for the past couple of days and looking and the help guide but i dont understand how to put it in.

    these are the two m files i attempeted

    a = [x;s];
    x = 0.25*s*x/(0.005+s);
    s = 0.45*0.25*s*x/(0.005+s);

    or

    a = [x;s];
    dadt = [0.25*s*x/(0.005+s);0.45*0.25*s*x/(0.005+s]


    but i get errors for both,

    i understand how to use ode45, (i think with tspan yo etc).

    Do you know what i am doing wrong?

    Rem
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by CaptainBlack View Post
    This is a first order system with state vector:

     X={X_1 \brack X_2}={x \brack s}

    with derivative:

    X'={\frac{0.25X_2X_1}{0.005+X_2} \brack \frac{0.45\times 0.25 X_2X_1}{0.005+X_2}}

    Now look up the help for ode45

    CB
    The function to eveulate the derivatives should look someting like:

    Code:
    function dx=derive(t,x)
      dx=zeros(size(x));
      dx(1)=0.24*x(1)*x(2)/(0.005+x(2));
      dx(2)=0.45*0.24*x(1)*x(2)/(0.005+x(2));
    But you give insufficient information to set up the initial conditions, what are s and x at t=0?

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    the initial conditions are, x = 0.1 and s = 40
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rem88 View Post
    the initial conditions are, x = 0.1 and s = 40
    Then try:

    [t,X]=ode45(derive, [0,5],[0.1;40]);

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Thanks for your reply, i tired that but i get this error message

    ??? Input argument "x" is undefined.
    Error in ==> g at 2
    dx=zeros(size(x));
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rem88 View Post
    Thanks for your reply, i tired that but i get this error message

    ??? Input argument "x" is undefined.
    Error in ==> g at 2
    dx=zeros(size(x));
    Can you give your calling code, and your derivayives function.

    CB
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Is this the information you wanted?

    My m-file

    function dx=g(t,x)
    dx=zeros(size(x));
    dx(1)=0.24*x(1)*x(2)/(0.005+x(2));
    dx(2)=0.45*0.24*x(1)*x(2)/(0.005+x(2));

    What i put in matlab workspace

    [t,x]=ode45(g, [0,5],[0.1;40]);


    Follow Math Help Forum on Facebook and Google+

  10. #10
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rem88 View Post
    Is this the information you wanted?

    My m-file

    function dx=g(t,x)
    dx=zeros(size(x));
    dx(1)=0.24*x(1)*x(2)/(0.005+x(2));
    dx(2)=0.45*0.24*x(1)*x(2)/(0.005+x(2));

    What i put in matlab workspace

    [t,x]=ode45(g, [0,5],[0.1;40]);

    Try:

    [t,x]=ode45(@g, [0,5],[0.1;40]);

    or:

    [t,x]=ode45('g', [0,5],[0.1;40]);

    CB
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Yeah that has worked! Thankyou.

    Can I just check, when i do plot(t,x) is this plotting X(x1) and S(x2) against time?
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Yeah it does plot them against time.

    One last question, sorry, can i export this data to something like excel? i know there is a imwrite xml, but is that it?

    If there is not too much data and this is not a production process but a one-off I would just cut abd paste the data with maybe a bit of sorting out at the Excel end.

    (Type help xxxx at the command prompt if you want to know what xxxx does)

    CB
    Last edited by CaptainBlack; April 7th 2009 at 04:43 AM.
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Thanks for all you help, you dont knwo how great it has been! .

    I just have one last question, sorry, i promise it will be the last.

    The formula for the next stage gets more complicated. So i tried this, but it didnt work :

    function dx=i(t,x)
    dx=zeros(size(x));
    dx(1)=0.177*x(1)*x(2)/(0.005+x(2)) + u;
    dx(2)=-0.177*x(1)*x(2)/(0.45*(0.005+x(2)));
    function du=i(t,x);
    du = 919.5*x(1);

    I thought by putting + u then stating u, it would make it simplier, as i keep inputting mistkaes the other way.
    Follow Math Help Forum on Facebook and Google+

  14. #14
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rem88 View Post
    Thanks for all you help, you dont knwo how great it has been! .

    I just have one last question, sorry, i promise it will be the last.

    The formula for the next stage gets more complicated. So i tried this, but it didnt work :

    function dx=i(t,x)
    dx=zeros(size(x));
    dx(1)=0.177*x(1)*x(2)/(0.005+x(2)) + u;
    dx(2)=-0.177*x(1)*x(2)/(0.45*(0.005+x(2)));
    function du=i(t,x);
    du = 919.5*x(1);

    I thought by putting + u then stating u, it would make it simplier, as i keep inputting mistkaes the other way.
    I don't understand what you are asking here.

    CB
    Follow Math Help Forum on Facebook and Google+

  15. #15
    Junior Member
    Joined
    Apr 2009
    Posts
    45
    Sorry i was not clear.

    What i meant was the next stage of my process involes the same equations as above, but with extra terms in.

    For example dX/dt now becomes :

    dX/dt= uX - FX/V

    Where u = 0.177*S/(S+0.005)
    F = 0.177*3443*20*exp(t)/(75*0.45)
    V = F + 3443

    Basically i am subtarcting FX/V from dx(1).

    I was wondering if there is a way to do what i have done above.

    Where the M file would simply say something like:
    ----------------------------------------------------
    dx(1)=0.177*x(1)*x(2)/(0.005+x(2)) - F*x(1)/V

    F = 0.177*3443*20*exp(t)/(75*0.45);
    V = F + 3443;
    -----------------------------------------------
    Is this possible? as when i try to type the code out in full i keep making mistakes and i htought this would simplify it (especially if i change any figures).

    (as later the equation becomes even more complex with more terms containing x and s)

    Rem
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. Riccati differential equation in Matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 27th 2011, 01:16 AM
  2. MatLab: Non-linear differential equations
    Posted in the Math Software Forum
    Replies: 3
    Last Post: June 30th 2010, 03:47 AM
  3. Matlab-Linear Differential Equation
    Posted in the Calculus Forum
    Replies: 0
    Last Post: March 22nd 2010, 11:12 AM
  4. Replies: 2
    Last Post: May 18th 2009, 03:49 AM
  5. Nonlinear differential equation MATLAB
    Posted in the Advanced Applied Math Forum
    Replies: 1
    Last Post: December 30th 2008, 06:27 AM

Search Tags


/mathhelpforum @mathhelpforum