1. ## 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

2. Originally Posted by rem88
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

3. 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];

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

4. Originally Posted by CaptainBlack
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

5. the initial conditions are, x = 0.1 and s = 40

6. Originally Posted by rem88
the initial conditions are, x = 0.1 and s = 40
Then try:

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

CB

7. 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));

8. Originally Posted by rem88
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));

CB

9. 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]);

10. Originally Posted by rem88
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

11. 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?

12. 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

13. 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.

14. Originally Posted by rem88
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

15. 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

Page 1 of 2 12 Last