# Thread: How to solve ODE for independent function

1. ## How to solve ODE for independent function

Hello,

I need to solve numerically an equation of the form

v(t) = k1*z(t)*w(t)-k2*i(t)-k3*di(t)/dt

The issue is that rungekutta methods are useful for solving

di(t)/dt = 1/k3 * [ k1*z(t)*w(t)-k2*i(t)-k3*-v(t) ]

but I need to solve for v(t)

What I did was:

v (t) = k1*z(t)*w(t)-k2*i(t)-k3*[i(t)-i(t-1)]/h

But is not a good approximation because the step size h cannot be small enough. I need a more sophisticated method than directly applying the difference quotient as I did.
Thanks a lot!

2. ## Re: How to solve ODE for independent function

What functions are known, and what functions are unknown? Presumably i and v are unknown. If so, you need another equation in order to solve for v numerically. Is this equation from a circuit? If so, please post the circuit.

3. ## Re: How to solve ODE for independent function

Thanks Ackbeet!

Well, the only unknown is v(t)
The equations are for an electric generator in stand alone operation, the actual equation is
vd=Lq*iq*wr-Rs*id-Ld*did/dt
where Lq, Ld, and Rs are constant parameters and vd, iq, wr, and id are functions of time.
My first approach of course was did/dt = (id(t)-id(t-h))/h, then I improved by a higher order approximation of the form did/dt = (3*id(t) - 4*id(t-h) + id(t-2h))/(2*h)
But still I have the same problem that did/dt oscillates too much and get's unstable with h less than 0.002, which is too big for me.
The variables are declare as double in the C code, to have a better precision.

4. ## Re: How to solve ODE for independent function

It doesn't look like you're actually solving an ODE. What you have, correct me if I'm wrong, are the numerical values for all the functions/values on the RHS of your equation, and from that you want to construct the function on the LHS. Is that correct? If so, I would investigate multi-point approximations for derivatives a bit more even than you have. You might want to check out the derivative on both sides of t. So far, all I see are t, t-h, and t-2h. What about t+h and t+2h? Those points might be useful. Perhaps a symmetric approximation of the derivative?

5. ## Re: How to solve ODE for independent function

You are right in your statement of what I'm trying to do. But I can't use future values because I'm using this equation to produce values of vd in a real-time application. So, I obtain say vd(t1) and with that value I calculate id(t2),iq(t2), and w(t2). With the new values I calculate vd(t2). So I can save the history of the system and use it to calculate the derivative, but I don't have future values.