I need to write a program in C to integrate the following simple equation:
dy/y= x dx
and I have no clue whatsoever on how to integrate it... :confused:
Could somebody please show me the code or technique used to integrate equations in C?
Printable View
I need to write a program in C to integrate the following simple equation:
dy/y= x dx
and I have no clue whatsoever on how to integrate it... :confused:
Could somebody please show me the code or technique used to integrate equations in C?
I asked a friend who was more experienced at math than me, and I still have no idea what you're doing. I might be able to help more if I knew what exactly to read from that equation, because it could mean ...anything. From calculus to algebra.Quote:
citizen says:
does this math thing have like, any meaning? I wish I knew what he was doing though?
Leon says:
i have no idea
Math in general is easy to implement, all you have to do is type out the equation most of the time. Here's a function that converts a Fahrenheit tempurature to Centigrade, for example:Code:float convert_temp(float f) {
return (f - 32) / 1.8;
}
Integration can only mean calculus. What you've proposed however seems like it would be fairly involved. For a definite integral you could go back to the basics of left / right hand sums with enough precision to get fairly close. But actually working with the equation like you suggest.... Considering software that does this (Maple) can run $500+ I'm going to guess it's fairly complicated, especially the type of equation(differential equation) you are asking for, I know a TI-89 can't do seperation of variables, but it can do normal integration. My advice would be to seek out an open source project that has accomplished this and study it, because I'm thinking it's going to be beyond the scope of a one or 2 paragraph explanation. I could be wrong though, feel free to correct me if someone knows better.
I'm kind of unsure myself at the moment as of what you're trying to accomplish. If you just don't need the indefinite integral, it's easy to see that it's y = e^(x^2/2), but I'm aussuming you have bounds, in which case you may want to look into Riemann sums...
I haven't done the maths behind integration, but I was given this task to do to find the area of a parabola between itself and the x-axis - using random numbers. It's quite accurate, but I don't know if it's what you were looking for.
You'll have to analyse it to see how it works, but if you know the maths (which I don't), it shouldn't be too difficult. The main idea is that the program tracks how many randomly generated numbers fall under the parabola, then uses the known area of a rectangle to calculate the answer.
Code:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(int argc, char *argv[])
{
float x, y, ans;
float rand_x, rand_y;
int i, tally = 0, throws = 100000;
srand( time( NULL ));
for ( i = 0; i < throws; i++ )
{
rand_x = ( ( rand() % 2001 ) / 1000.0) - 1.0;
rand_y = ( ( rand() % 1001 ) / 1000.0);
y = -( rand_x * rand_x ) + 1; //this is the equation of the line, -(x^2) + 1 in this case
if ( rand_y <= y )
{
tally++;
}
}
printf("%d\n", tally);
ans = ( (float) tally / (float) throws ) * 2.0;
printf("ans = %.3f\n", ans);
system("PAUSE");
return 0;
}
OK. the equation I proposed was a put-together of two seperate functions. What I have to do is integrate the left side and the right side seperately... then make them equal to each other.Quote:
Originally Posted by RunDosRun
Analytically, I get :
ln (y) = (x^2)/2 + C as a result of $ dy/y = $ x dx
(by the way $ is used for the integral sign)
so when i solve for initial condition (y=1 when x=0), C=0.
All I have to do is know how to integrate (dy/y) and (x dx) in C seperately in the intervals [0,1].
The general way (from what I know), to integrate defined integrals with computers is to use Riemann sums :
http://mathworld.wolfram.com/RiemannSum.html
Any help ?
You need a constraint (like y(1) = 2) if you want to solve the problem numerically. (If you want an analytic solution--psst, it's a one-liner!--use Mathematica, Maple, etc.)Quote:
I need to write a program in C to integrate the following simple equation:
dy/y= x dx
and I have no clue whatsoever on how to integrate it...
You could then take a Riemann sum, or use Euler's method, or try something fancier like a Runge-Kutta.
There are lots of ways to do definate integrals actually. Though it can get complicated quite quickly you can get very accurate results using many previous studied algorithms. I will list some that I know of in order of how complicated they are mathematically.
Reiman Sums should work for this and is easy to implement
Euler methods (there are many branches to this method) will obtain you far greater results
Verlet Algorithms - these are what I use in my work though I'm not certain how well they pertain to non motion problems.
But in general, there is no C standard library function which integrates, and if you want to do indefinate integration your basically going to have to write a function that follows the logic a human would when solving it. It would be rather tedious to write a program which covers the entire scope of integration techniques but if your sure all the integration your going to need is just basic polynomials, or all rational functions, etc, then I am sure you could easily implement a function which exploits the power rule, partial fractions, etc.
If you need more information feel free to P.M. and I will gladly help you implement this (I am a computational physist and thus programming computers to do math is what I get paid for :) ) so if none of these posts helped you feel free to get in touch with me.
if you wanna intergrate a polynomial then do the following:
store f(x) in a linked list..with the index of each term..and the coefficient of x
then as you traverse the linked list just go on increasing the index by 1 and correspondingly divide the term by the index.Code:struct poly
{
int val; // to store the index of each term of f(x)
float coeff; // to store the coefficient of x in each term
struct poly *next;
};
hope this helps :)Code:if f(x) is a*x^3+b*x^2 then following above procedure will give
(a/4)*x^4+(b/3)*x^3