Solved

Posted on 2005-04-11

Hi, just wondered if anyone had any C code which could solve a second order differential equation. I am currently using matlab for the job but its too slow, and my C skills aren't particularly great.

I have the equation in the form

y''=f(t,y,v)

y'=v

Thanks,

Dan

I have the equation in the form

y''=f(t,y,v)

y'=v

Thanks,

Dan

13 Comments

a*d2y/dx2+b*dy/dx+c=d

so use dy/dx=v and d2y/dx2=dv/dx to separate into two first order equations. I just need a C program that solves them, preferably using something simple like Eulers method.

To use simple EM..

Say,

y' = -2xy (of the form y' = f(x, y) ); and initial condition : y(0) = 0 and 0<=x<=1;

The working formula is-

y(n+1) = y(n) + h f[x(n), y(n)]; where n = index; h = difference between x(n+1) and x(n).

For this example take h = 0.1 and so (1-0)/0.1 = 10 iterations needed.

To use the formula..

y(0) = 0; [Given]

y(1) = y(0) + h f[x(0), y(0)];

y(2) = y(1) + h f[x(1), y(1)];

y(3) = y(2) + h f[x(2), y(2)];

.

.

so on....

You can try it now...

For example:

y'' + 2y' - 3y = 0; and initial conditions y(0) = 0, y'(0) = 1. [ here, y = y(t) ]

Solving it gives-

y(t) = 1/4{ exp(t) - exp(-3t)}.

Now you can find any value of y(t) by plugging the value of t.

If you need to find the numerical values of only one differential equation, u can use this method, i.e- first solve it on papers and then try to find the true values using a C program.

Is that what you suggest when the equation is something like

y'' = f(t,y,v) = cos(v^2) + Exp(y^2)*cos(y) + 1 + t = 0

y' = v

Or f(t,y,v) is similarly complicated.. ?

I don't think solution via paper is very good for anything other than the

totally trivial forms.

If Matlab is slow, then I suspect f(t,y,v) is very much non-trivial.

>> y'' = f(t,y,v) = cos(v^2) + Exp(y^2)*cos(y) + 1 + t = 0

>> y' = v

No way... I would better try something else

Anyway thanks to those who posted but I'm gonna ask for it to be removed unless there are any objections.

http://sources.redhat.com/

http://www.gnu.org/softwar

Or see here: http://matwww.ee.tut.fi/~p

But as I understand it, the vast majority of freely available ODE solver libraries

are written in Fortran, not C

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Understanding of redefinition compiler error and multiple definition error. | 5 | 190 | |

Connecting Native C / C++ to SQL Server | 11 | 246 | |

My eclipse editor won't start ? | 4 | 133 | |

Problem in finding output of a program | 11 | 86 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**21** Experts available now in Live!