From: Chad on
I'm not a frequent MATLAB programmer so I appreciate the
assistance with what I expect is a simple question for
many of you out there.

I need to solve the ODE below:

dx/dt = ka * (A - x) * (B - x) - kd (C - x)

I tried to use

f = inline('ka * (A - x) * (B - x) - kd (C - x)')

and then

subs(f)

to substitute for the known values which are all of them
but x.

This is my first problem. I get the error below:

??? Error using ==> sym.sym
Conversion to 'sym' from 'inline' is not possible.

Error in ==> subs at 60
r = subs(sym(f),varargin{:});

I would like to take the subsituted equation and solve
with ODE45. I could also use dsolve as I realize there is
an exact solution but unless I can substitute the
variables this as thought it will be a problem as well.

Using the solver should be straightforward if I can get
the function in the right format to plug in. My approach
would be as below if anyone would like to review and make
suggestions on this as well:

tspan = [t0 tf];
[t,x]=ode45(f,tspan,x0);
[xfi dummy]=size(x);
xf=x(xfi);

Thank you! I'd like to get past this so I can get back to
the chemistry I am good at!


From: Steven Lord on

"Chad " <chad.briscoe(a)remove.this.mdsinc.com> wrote in message
news:g89eh9$3ik$1(a)fred.mathworks.com...
> I'm not a frequent MATLAB programmer so I appreciate the
> assistance with what I expect is a simple question for
> many of you out there.
>
> I need to solve the ODE below:
>
> dx/dt = ka * (A - x) * (B - x) - kd (C - x)
>
> I tried to use
>
> f = inline('ka * (A - x) * (B - x) - kd (C - x)')

Don't do this. This will create an inline function f with 6 input arguments
[assuming that the last term is supposed to be kd*(C-x)] and no guarantee
that x will be the first input.

If you _must_ do this with an inline function, explicitly specify the input
arguments:

f = inline('ka * (A - x) * (B - x) - kd*(C - x)', 'x', 'A', 'B', 'C', 'ka',
'kd')

However, I strongly suggest you do this using either an M-file function (as
Maxim posted in his reply) or an anonymous function:

A = 1;
B = 2;
C = 3;
ka = 0.5;
kd = 0.7;
fp = @(t, x) ka * (A - x) * (B - x) - kd*(C - x);

and use ODE45 on that anonymous function.

[t, y] = ode45(fp, [0 10], 0.7)

> and then
>
> subs(f)
>
> to substitute for the known values which are all of them
> but x.
>
> This is my first problem. I get the error below:
>
> ??? Error using ==> sym.sym
> Conversion to 'sym' from 'inline' is not possible.

There's no function in Symbolic Math Toolbox to convert a sym object into an
inline function or vice versa.

> Error in ==> subs at 60
> r = subs(sym(f),varargin{:});
>
> I would like to take the subsituted equation and solve
> with ODE45. I could also use dsolve as I realize there is
> an exact solution but unless I can substitute the
> variables this as thought it will be a problem as well.

If using DSOLVE is acceptable:

syms A B C ka kd x
f = ka * (A - x) * (B - x) - kd*(C - x);
fs = subs(f, {A, B, C, ka, kd}, {1, 2, 3, 0.5, 0.7});
sol = dsolve(['Dx =' char(fs)])

--
Steve Lord
slord(a)mathworks.com


From: Theresa on
try this:

function [t x xf] = solve_ode(tspan, x0, params)

[t x] = ode45(@(t,x)odefun(t,x,params), tspan, x0);

function dxdt = odefun(t,x,params)

ka = params(1);
A = params(2);
B = params(3);
C = params(4);
kd = params(5);

x = x(1);

dxdt = ka*(A-x)*(B-x)-kd*(C-x);

end

xf = x(end,1);

figure()
plot(t,x)

end

Create a new mfile called solve_ode and copy the code I
provided into it. To run, type solve_ode(tspan, x0,
params) at the command prompt, replacing tspan, x0 and
params with the appropriate values. For example:

[t x xf] = solve_ode(0:0.1:10, 1, [1 2 3 4 5]);

This will return the solution of the ODE on the time
interval 0 to 10, with initial condition x0 = 1, and ka =
1, A = 2, B = 3, C = 4, kd = 5. You get back t, x, and x
at the final time xf. With this construction you can solve
the ODE on any time interval, with any initial condition,
and with any combination of parameters you like.

Hope that helps.

"Chad " <chad.briscoe(a)remove.this.mdsinc.com> wrote in
message <g89eh9$3ik$1(a)fred.mathworks.com>...
> I'm not a frequent MATLAB programmer so I appreciate the
> assistance with what I expect is a simple question for
> many of you out there.
>
> I need to solve the ODE below:
>
> dx/dt = ka * (A - x) * (B - x) - kd (C - x)
>
> I tried to use
>
> f = inline('ka * (A - x) * (B - x) - kd (C - x)')
>
> and then
>
> subs(f)
>
> to substitute for the known values which are all of them
> but x.
>
> This is my first problem. I get the error below:
>
> ??? Error using ==> sym.sym
> Conversion to 'sym' from 'inline' is not possible.
>
> Error in ==> subs at 60
> r = subs(sym(f),varargin{:});
>
> I would like to take the subsituted equation and solve
> with ODE45. I could also use dsolve as I realize there
is
> an exact solution but unless I can substitute the
> variables this as thought it will be a problem as well.
>
> Using the solver should be straightforward if I can get
> the function in the right format to plug in. My approach
> would be as below if anyone would like to review and make
> suggestions on this as well:
>
> tspan = [t0 tf];
> [t,x]=ode45(f,tspan,x0);
> [xfi dummy]=size(x);
> xf=x(xfi);
>
> Thank you! I'd like to get past this so I can get back
to
> the chemistry I am good at!
>
>