Solved

Integration in C

Posted on 2004-10-12
4
455 Views
Last Modified: 2011-09-20
Hello,
  I want to integrate a function. can anybody suggest me how to improve the code..

for( x = x1; x <= x2; x += 0.01)                  
{      
            f1 = some function(x)
            res += f1/10000.0;
                              
}

IS this right??
Can u plzz show me the code to do integration using any std rules..
TIA
Suma
0
Comment
Question by:hssuma
  • 3
4 Comments
 
LVL 1

Expert Comment

by:RajeshTN
ID: 12295622
hi Suma,

What exactly do you mean by "integrate a function" ? pls explain

Bye
-Rajesh
0
 
LVL 5

Accepted Solution

by:
van_dy earned 20 total points
ID: 12295964
Hi Suma,
       You can use the any of the availiable numerical integration
techniques to calculate the definite integral of a function. Have a look at

http://mathworld.wolfram.com/NumericalIntegration.html

the techniques range from fairly complex Newton-cote's method
to relatively simple trapezoidal rule.  You  need to be more specific
about the algorithm you want to implement.
As a simple implementation, consider dividing your integration space into
N equal sized intervals, and assume that the intervals are small enough
so that the function value in the i-th interval can be approximated as f((X[i] + fX[i+1])/2) throughout;
then a rough integration function can be;

#define N  SOME_LARGE_NUMBER

double function(X)
              double X;
{
        // The body of the function you are integrating over [X1, X2];
}

double integrator(X1,  X2)
               double X1, X2
{
         double Increment = (X2 - X1)/ N;
         double  X = X;
          double Result = 0.0;

          for (; X < X2; X += Increment)
                   Result +=  function(X + Increment/2);

            return Result;
}

Similarily you can implement any of the techniques once you have read about them.
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12295978
Well well, in the integrator(X1, X2), second line reads:

            double X = X1;
             double Result =  0.0;
              .....
              ............
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12295995
and the last line would be

         return Result * Increment;

:D, tooo much hurry;
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 119
Problem in finding output of a program 11 101
delete-remove 14 63
Memory going from 12gb to 64gb or 96gb. worth it? 15 132
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now