Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411516 Posts in 69380 Topics- by 58436 Members - Latest Member: GlitchyPSI

May 01, 2024, 07:15:05 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to do ?
Pages: [1]
Print
Author Topic: How to do ?  (Read 1996 times)
Sigma
Level 1
*


View Profile WWW
« on: July 26, 2010, 05:14:10 AM »

Hey guys,
   can anyone tell me how to implement calculus through code for the example below

QUESTION:
   A rectangular water tank (see figure below) is being filled at the constant rate of 20 liters / second. The base of the tank has dimensions w = 1 meter and L = 2 meters. What is the rate of change of the height of water in the tank?(express the answer in cm / sec).

SOLUTION:
       * The volume V of water in the tank is given by.

      V = w*L*H

    * We know the rate of change of the volume dV/dt = 20 liter /sec. We need to find the rate of change of the height H of water dH/dt. V and H are functions of time. We can differentiate both side of the above formula to obtain

      dV/dt = W*L*dH/dt

    * note W and L do not change with time and are therefore considered as constants in the above operation of differentiation.

    * We now find a formula for dH/dt as follows.

      dH/dt = dV/dt / W*L

    * We need to convert liters into cubic cm and meters into cm as follows

      1 liter = 1 cubic decimeter
      = 1000 cubic centimeters
      = 1000 cm 3

      and 1 meter = 100 centimeter.

    * We now evaluate the rate of change of the height H of water.

      dH/dt = dV/dt / W*L

      = ( 20*1000 cm 3 / sec ) / (100 cm * 200 cm)

      = 1 cm / sec.

All Helps are appreciated.
Thanks in Advance.
Logged

j0d1
Level 0
***


indie@montreal


View Profile WWW
« Reply #1 on: July 26, 2010, 05:40:15 AM »

In Ruby, I would do it this way (let's say solution.rb) :

Code:
# Problem's variables

fill_rate = ARGV[0].to_f    # liter / second
tank_width = ARGV[1].to_f   # meters
tank_height = ARGV[2].to_f  # meters


# Solution

rate_of_change = (fill_rate * 1000) / (tank_width * tank_height * 10000)


# Print the solution

puts rate_of_change

and I would use my script this way :

Code:
ruby solution.rb 20 1 2

Not sure if it's accurate but it gives you an idea. Add some boilerplate code and you have a Java program Wink
Logged

I\'m currently making the game Commander.
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #2 on: July 26, 2010, 05:55:07 AM »

Technically right, although you used a really strange method. I'd use the chain rule instead of your way.

W = 1, L = 2 therefore A (area) = 2

DV/Dt = 20 (L/s)

DH/Dt = DH/DV * DV/DT (chain rule)

to find DH/DV we need h in terms of v

V = a*H
therefore
H = V/a

d/DV(H) = 1/a (as it is a constant, in this case 2)

therefore

DH/dt = (1/a)*DV/dt

=(1/a)*20L/s
=(1/2)*(20/1000)
=(0.5)*(1/50)
=0.01 M/s

Dimensional analysis:
M/s is correct, because it is (1/M^2)*(M^3/s)

generalised solution:
=(1/a)*(v/1000)
=v/(1000*a)

I think that's right...Its part of core 4 maths a-level, so I SHOULD know it.

I'm not sure what you mean by "implementing calculus through code" though, do you mean actually getting the computer to do calculus, or a general solution for this problem (which I've given)
Logged
Sigma
Level 1
*


View Profile WWW
« Reply #3 on: July 26, 2010, 08:34:18 AM »

do you mean actually getting the computer to do calculus
ya i dont want the solution for this problem. i just want in general how to get the calculus work done. i guess i have chosen a wrong example to get what i need. I know that Integrating the acceleration over time gives the velocity, and twice integrating the acceleration gives the position, but i don't know how to implement this. Reply with an example might be more helpful.

All helps are appreciated.

Thanks in advance
Logged

Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #4 on: July 26, 2010, 08:58:41 AM »

Implementing actual calculus is pretty hard without some specialised mathematically oorientated languages. I've tried it myself before, and it totally failed. However you could try writing a program to differentiate/integrate simple polynomials.

I would try something like this:
Code:
class terms:
   double coefficient //double precision
   double power
end class

class polynomial:
   array terms  //array of class term
end class
and them repeatedly check for terms with repeated powers.

The problem with calculus is that there are so many special cases, for example integrating:
Code:
d/dX(y = (x^2)+3y)
can be pretty hard.

Also my method doesn't allow for polynomials with non x term.

If you don't mind me asking, what mathematical training do you have? GCSE level? A-Level? Degree? Doctorate? Masters? BTEC?

You could also look into implementing wolfram alpha's API, that can do pretty complex differentiation and integration, including trigonometric and exponential functions.
« Last Edit: July 26, 2010, 09:03:47 AM by 14113 » Logged
Sigma
Level 1
*


View Profile WWW
« Reply #5 on: July 26, 2010, 12:34:16 PM »

i dont have any special training or Maths as major in engineering. just in curiosity i asked. Any case thanks to made things clear.
Logged

Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #6 on: July 26, 2010, 12:52:18 PM »

I dont want to put you off though; if you want to, try and do it, it might work  Gentleman
Logged
j0d1
Level 0
***


indie@montreal


View Profile WWW
« Reply #7 on: July 26, 2010, 12:57:18 PM »

I don't get what he wants to do. Could someone explain it to me like I was 4 years old?
Logged

I\'m currently making the game Commander.
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #8 on: July 26, 2010, 01:00:52 PM »

(I think)
He wants to have a way of doing calculus on a function of x
Logged
j0d1
Level 0
***


indie@montreal


View Profile WWW
« Reply #9 on: July 26, 2010, 03:22:28 PM »

Oh... ok!
Logged

I\'m currently making the game Commander.
Sigma
Level 1
*


View Profile WWW
« Reply #10 on: July 26, 2010, 09:31:50 PM »

ya exactly...
Logged

PleasingFungus
Level 7
**



View Profile WWW
« Reply #11 on: July 26, 2010, 09:37:16 PM »

http://en.wikipedia.org/wiki/Numerical_analysis
Logged

Finished games: Manufactoria! International King of Wine!
And others on my site.
bateleur
Level 10
*****



View Profile
« Reply #12 on: July 27, 2010, 12:37:17 AM »

The most fundamental problem with implementing calculus - before one even starts to consider the technical challenges involved - is that calculus isn't actually an algorithm. Indeed, as anyone who's ever looked into measure theory will tell you, it's not even all that clear what the answers should be in some cases.

So what you end up with in practice in a huuuge pile of algorithms which collectively cover the most common cases.

Stephen Wolfram has more-or-less spent his career implementing one of these. It's good. So good, in fact, that some commentators have described it as humankind's first AI.

I'm not completely sure they were joking, either.
Logged

dangerousday
Level 1
*


iacedrom


View Profile
« Reply #13 on: July 27, 2010, 07:00:41 PM »


I wholeheartedly agree. The topic of "differential and integral calculus" covers such a huge swath of mathematics, and only in the most exceptional special cases are you guaranteed a closed-form solution to what you're looking for. Numerical analysis is an entire field of study devoted solely to figuring out what kinds of problems can be programmed into a computer, how much time it will take to solve them, and how bad the error is going to be.

And you might not even need differential calculus to solve the problem you posted with the water tank. Each second, we're adding 20 liters (.02 cubic m) to the tank, whose cross-section is constant (1m * 2m). So, if we just take a snapshot of the water we add during one second, we see that its dimensions are 1m length and 2m width. How much height does that bit of water need to have, in order to make its volume .02 cubic m?

</smartypants>
Logged
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #14 on: July 28, 2010, 02:29:02 AM »

Yeah, thats how sigma originally did it. I feel more comfortable using the chain rule though  Shrug
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic