ODE reliant on dependent function

Hi,
So I am trying to use Matlab to solve a system of time-dependent differential equations, say vector dA/dt (not sure if I can TeX here) which have an additional dependence on a secondary vector function, C(t). The issue arises due to the secondary function being a function of A, my solution. That is,
dA/dt=f(t,C(t, A(t))).
Currently I am using ODE45, however this requires initial conditions to be given for C, which cannot be input as it is a not a function to be integrated. I'd like to know if this system can be solved with an inbuilt Matlab function or whether I should try and build a custom solver?
To be clear, here is some sample code:
[t,results]=ode45(@(t,x) functiontosolve(t,x), [0 t1],a]
function output=functiontosolve(t,x)
%Need to give C1 and C2 initial parameters so it will run
C1=b;
C2=c;
%all "p's" are parameters which can be defined inside the function.
dA1=(p1*C1)/(pa2+C1)
C1=A/((p3+(p4)/(p4+C1)+(p5)/(p6+C1))
dA2=p2*(C2-C1)-dA1;
output=[dA1;dA2]
So my issue is firstly that C1, C2 have to be given values to run, so therefore whenever MATLAB runs the function as it does in ODE45 it then always assigns those values. Secondly, I'd like the C1 and C2 values to be exported, if I could make it solve these equations simultaneously.
I may have over-complicated this, so my sincerest apologies if you have read this and think my question is nonsense or poorly phrased.
My only idea is to run ODE45 on very small intervals with initial values and then update C1, C2 and loop. This seems expensive so I'd like to know if there is an easier solution.
Thanks Michael

5 Comments

I think you can put the function C at the beginning of functiontosolve and use the current values for A and t. I am a bit confused by your code though: where is C? Is C actually C1 and C2?? You can't export C1 and C2 from the solver (it's numerically wrong), but you can reeval them in the main function.
Can you post input values for your problem (all of them) + the expression for C (or C1,C2)?
Yes, I think I've figured out that you can pass values into ode45 and have C being updated everytime, if the value did not depend on the integral of dA/dt. Apologies, I meant C to be the vector function. In reality there are 8 different concentration functions (Cs) which are to be solved for. The actual code is very interwoven, i.e. the same variables being used in many equations, and is about 200 lines. The code posted above was just meant to represent the issue.
So the crux comes down to that dA/dt requires C, however C depends on A. I can't see how to use ODE45 unless looping a small time interval.
Thanks a lot for replying.
After this line:
function output=functiontosolve(t,x)
you can put the eqn for C's as function of x (which is the current value of A). If this does not work, then I'm not understanding the problem :(
Michael
Michael on 6 Aug 2014
Edited: Michael on 6 Aug 2014
You're answer is actually what I need for a normal situation. The difference is that C is defined implicity (and seems to not be able to be rearranged).
So the problem essentially boils down to
c=f(x,c) dx/dt=t+c
where c-f(x,c)=0 cannot be easily solved for c. The error comes from trying to define c with c, i.e. the same as x=1/(1+x). I don't have the symbolic package which I think would make this simple.
I realise this is nothing like the question I originally asked but its been really helpful going through this and I'm quite a MATLAB newb.
Its ok I got it with fsolve. Thanks for all your effort

Sign in to comment.

Answers (0)

Asked:

on 6 Aug 2014

Commented:

on 7 Aug 2014

Community Treasure Hunt

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

Start Hunting!