# Integrals

Page 1 of 1

## 13 Replies - 42019 Views - Last Post: 07 June 2008 - 03:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=54110&amp;s=53ae0b76d2e294f93574fd86d705542d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Khaunshar

Reputation: 0
• Posts: 28
• Joined: 23-April 08

# Integrals

Posted 07 June 2008 - 05:47 AM

Hello

Math exams are coming up and the subject is intergral calculation.

So I thought I'd try to write a program to calculate the integrals of a function, and the possibility to calculate the area for a given interval and/or the volume of the body discribed by spinning it around the x-axis, also for a given interval.

How can I accomplish this?

Is there a built in function in C to calculate the integral of a function or do I need to write the procedure myself?

If I need to write the procedure myself, how do I do this?
Break the function into blocks and let it produce the integral for the given block by applying the methods used to calculate it? (a;n; func=ax^n integralfunc=(ax^(n+1))/(n+1) )

Should I make rules for special functions such as ln(), so the program can find the integral? Or should I make those functions into "normal" forms. (By the use of logarithms for ln() for example)

Thank you

This post has been edited by Khaunshar: 07 June 2008 - 06:52 AM

Is This A Good Question/Topic? 0

## Replies To: Integrals

### #2 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

## Re: Integrals

Posted 07 June 2008 - 06:34 AM

C++ cannot calculate integrals. You will have to write the function yourself.

This would be very difficult. However, it would be simple to calculate the integrals of polynomials using a computer program. If you have "a*x^n", it becomes "ax^(n+1)/(n+1)" It isn't really hard to do this without a program.

There is a wonderful piece of software called Mathematica out there that could calculate integrals for you. Also, a Texas Instruments calculator with the Computer Algebra System can do that as well.

### #3 Khaunshar

Reputation: 0
• Posts: 28
• Joined: 23-April 08

## Re: Integrals

Posted 07 June 2008 - 06:43 AM

yes, I have the texas, we use it in school.

I was just wondering whether I could write such program myself.

This post has been edited by Khaunshar: 07 June 2008 - 06:44 AM

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Integrals

Posted 07 June 2008 - 06:55 AM

Yes you could--it would be fun--in fact I'm off to write one right now.

edit: parsing the equation is the hardest part atm

This post has been edited by KYA: 07 June 2008 - 07:01 AM

### #5 Khaunshar

Reputation: 0
• Posts: 28
• Joined: 23-April 08

## Re: Integrals

Posted 07 June 2008 - 07:06 AM

What do you mean by parsing? Getting the different parts of the equation?

edit: sorry, looked it up myself, i shoud look more and ask less..

This post has been edited by Khaunshar: 07 June 2008 - 07:11 AM

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Integrals

Posted 07 June 2008 - 07:11 AM

Yes and deciding how i should prompt for input like:

ask for x^3, etc... or some other format

### #7 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Integrals

Posted 07 June 2008 - 07:21 AM

Generally this is not the kind of program that you just "whip up". The first problem is parser. You need to get a string like "a*x^3" into some meaningful structure in memory. Finite State Machines to the rescue!! (I love FSMs)

The next part is integral patterns. Now if you are just working with polynomials then this is fairly easy as you only have a few rules to encode. If you are working with mathematical notation in general, well this is pretty complex.

For example: (x - 1)^2 is still the pattern p*u^n

So you can either try to put the equation in a proper format, or you can use patterns to calculate the integral based upon its structure.

Basically you need to capture that table of integrals in the back of the calculus textbook.

Now it turns out that people have been working on these problems for a long time now and there is quite a lot of information on how it is done (lots of masters theses on the subject). There are several open source CAS systems with code you can look though.

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Integrals

Posted 07 June 2008 - 07:31 AM

Indeed. Even hard coding just one specific equation still requires some effort.

### #9 Khaunshar

Reputation: 0
• Posts: 28
• Joined: 23-April 08

## Re: Integrals

Posted 07 June 2008 - 07:37 AM

Thanks for the information. Very interesting

While thinking about it just now I also figured it would be pretty complex, also how to deal with combinations of goniometric and/or other types of .. euh systems. Does a computer(or the program) have problems with combining these types at the same time?

This post has been edited by Khaunshar: 07 June 2008 - 07:41 AM

### #10 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Integrals

Posted 07 June 2008 - 09:46 AM

One of the wonderful things about mathematics is that it was developed to be incredibly abstract. This means that dealing with different geometries and different spaces or manifolds etc can pretty much all be abstracted to something like a Lebesgue integral. So basically as long as you tell the computer what kind of space you are integrating over the computer can do it.

Something I would like to note: In C++ the process of symbolic manipulation of equations is pretty hard. But in a functional language such as LISP these kinds of tasks are much more intuitive and easier to deal with.

Part of the reason for this is that functional languages look a little more like mathematics, part of the reason is that they tend to be more abstraction oriented.

### #11 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Integrals

Posted 07 June 2008 - 10:34 AM

Ah yes, I wrote a program like this not too long ago in Java...even though it only gave you a few equations to integrate, I am not advanced enough to figure out parsing equations...heh.

I used the fundamental theorem of calculus to do my math. So I'm giving a value back out, for instance...

integral of sin(x)dx from 0 to pi = 2

It was a lot of work...I just give the user some basic options of equations, mostly trig functions.

### #12 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Integrals

Posted 07 June 2008 - 01:20 PM

Actually, numeric integration is a totally different matter!!! You can write some pretty sophisticated numeric integration programs with only a smattering of programming knowledge.

The easiest is the trapezoid rule that you learn in calculus, but there are other more advanced methods that can give you an answer to integrating any real function over an interval to just about any error tolerance you need.

The thing is that these can't really tell you that the answer is pi/2 -- they can tell you that the answer is roughly 1.5708.

Symbolic manipulation is much harder than numeric calculation. But in practice often all you really need is the numeric version.

### #13 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Integrals

Posted 07 June 2008 - 01:41 PM

You can always approximate an Integral.
A few years back I had posted snippets on calculating differentials and integrals using first principle and Limit of Sums (Riemann sum) respectively.

Check 'em out.

### #14 perfectly.insane

Reputation: 70
• Posts: 644
• Joined: 22-March 08

## Re: Integrals

Posted 07 June 2008 - 03:13 PM

Doing this yourself would be very time consuming. Symbolic differentiation isn't that hard. It's still a pain, but could be achieved much quicker/simpler in comparison (to a certain degree, each rule can be deterministically applied based upon the basic operator found in each node of an abstract syntax tree, with some exceptions).

Here's a library for C++ that performs computations such as this (note, I've never used it, so I don't know how good it is).

http://www.ginac.de/

It uses expression objects and operator overloading to simulate symbolic computation directly in C++. It appears that it may also parse expressions in strings, as a program that comes with it is like a simple computer algebra system.

This post has been edited by perfectly.insane: 07 June 2008 - 03:17 PM