Results 1 to 6 of 6

Thread: program to calculate definite integration

  1. #1
    Join Date
    Sep 2010
    Posts
    11

    program to calculate definite integration

    HOw do I write a program to calculate definite integration? Please explain

  2. #2
    Join Date
    Mar 2010
    Posts
    338

    Re: program to calculate definite integration

    What is the programming language you are using? If you are using python then I can help you.
    You will need to import the scipy module for this and here is an example of this.

    Code:
    >>> import scipy.integrate
    >>> func = lambda x: x**2
    >>> scipy.integrate.quadrature(func, 0.0, 1.0)
    Took 4 points.
    (0.333333333333, 0.0)

  3. #3
    Join Date
    May 2008
    Posts
    248

    Re: program to calculate definite integration

    Here is the code that can help you

    Code:
    using System;
    
    namespace SimpsonMethod
    {
        public class sim
        {
            public static void Main()
            {
                int itr = 100000;
                double x, str, ed, dst, sm = 0, smtt = 0;
                Console.Write("Input range str: ");
                str = double.Parse(Console.ReadLine());
                Console.Write("Input range ed: ");
                ed = double.Parse(Console.ReadLine());
                dst = (ed - str)/itr;
                for (int i = 1; i <= itr; i++)
                {
                    x = str + i * dst;
                    smtt += f(x - dst / 2);
                    if (i < itr)
                    {
                        sm += f(x);
                    }
                }
                sm = (dst / 6) * (f(str) + f(ed) + 2 * sm + 4 * smtt);
                Console.WriteLine("Value on range [{0}, {1}] is around: {2:0.00000}", str, ed, sm);
            }
    
            private static double f(double x)
            {
                return x*x+2*x;
            }
        }
    }

  4. #4
    Join Date
    Sep 2010
    Posts
    11

    Re: program to calculate definite integration

    I'm using C... Is there a way to write in in a for loop? I have no clue what I'm doing, an explaination of a program would also be very helpful. Thanks

  5. #5
    Join Date
    May 2008
    Posts
    248

    Re: program to calculate definite integration

    This is the logic behind this code.
    Here is the for loop

    Code:
      for (int i = 1; i <= itr; i++)
                {
                    x = str + i * dst;
                    smtt += f(x - dst / 2);
                    if (i < itr)
                    {
                        sm += f(x);
                    }
                }
    You have to just implement the same using c language.

  6. #6
    Join Date
    Sep 2010
    Posts
    11

    Re: program to calculate definite integration

    alrighty, i'll work on it. thanks

Similar Threads

  1. Replies: 5
    Last Post: 16-03-2011, 06:22 PM
  2. Program : How to calculate the median in C++?
    By UseME in forum Software Development
    Replies: 3
    Last Post: 19-01-2010, 12:08 PM
  3. What is the shell program to calculate addition of two numbers?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 02:34 PM
  4. What is the Program to calculate the percentage?
    By Ekpah in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 12:30 PM
  5. Calculate Average in C program
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 11:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,022,680.87575 seconds with 16 queries