?
Solved

C code to numerically integrate a second order differential equation

Posted on 2005-04-11
16
Medium Priority
?
435 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:DanJW
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13753583
Are t and v constants, parameters, or what?
0
 

Author Comment

by:DanJW
ID: 13753695
t,y and v are all parameters
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13753993
y' is the derivative of y with respect to what?  In other words, y' = dy/d?, what is the '?' ?  


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:DanJW
ID: 13754282
I didn't think that would matter, basically its a second order diff of the form

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.

0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 13777808
Do u need Simple Eulers Method (EM) or Improved EM??..

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...
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 13777939
For a second order differential equation, u need to first solve it and then try to find the true values.

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.  
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 13778308
>> re: shah1d1698

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.
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 13778998
>> 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


No way... I would better try something else
0
 

Author Comment

by:DanJW
ID: 13780518
I have a few equations to solve and I wouldn't be using numerical solutions if I could just solve them by hand.  What you need to do is treat the two equations as a system of differential equations (like you would find in the lorentz equations) and solve them that way.  My problem is that I cant do it in C because I havent used it in three years and im not a programmer anyway.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 13841286
I think the solutions given so far are enough. Give points to any one of them

Rajeev
0
 

Author Comment

by:DanJW
ID: 13886451
Well, what I really needed was the C code, not how to (or whether or not I should) solve differential equations.  I am perfectly capable of doing that thanks to my degree in maths.  However, what I cant do is program in C so thats why I asked for help.  If someone were browsing this question and came upon that answer, do you think that would suffice? i.e. after reading this, do you know the C code for solution of a second order differential equation?

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

Accepted Solution

by:
Mysidia earned 1080 total points
ID: 13889091
Perhaps the GNU Scientific library?

http://sources.redhat.com/gsl/

http://www.gnu.org/software/gsl/manual/gsl-ref_toc.html

Or see here:  http://matwww.ee.tut.fi/~piche/industrialmath/odesolvers.html

But as I understand it, the vast majority of freely available ODE solver libraries
are written in Fortran, not C
0
 

Author Comment

by:DanJW
ID: 14152239
Apologies, I was sure that I had posted in the community support bit.  I will do so
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

579 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question