Solving the second problem of calculus with Ruby!

Started by Ryex, January 14, 2010, 09:47:28 pm

Previous topic - Next topic

Ryex

So... when I realized how to theoretically solve for the area under a curve I endeavored to make a ruby program to automate the process
and here was the result

link to the .rb file
http://docs.google.com/leaf?id=0B0KLI9M5vfiOZDI4OGIyMzktYTA5Mi00YTMyLTlhMGMtODQxOGVjNTcwNTc1&hl=en
link to a no ruby required .exe
http://www.mediafire.com/?wlzngwnotww
source code
def auc
   its = ac = x2 = x1 = nil
puts 'start point of domain:'
x1 = gets
x1 = x1.to_i
puts 'end point of domain:'
x2 = gets
x2 = x2.to_i
puts 'accuracy:'
ac = gets
ac = ac.to_i
puts 'solving...'
w = 1
area = 0
last_area = 0
i1 = 0
x = x1
loop do
area = 0
a=(1.0/(10)**i1)
(((x2 - x1) / a).floor).times { |i2|
fx = yield(x + (i2 * a))
box = fx * a
area += box
}
unless i1 == 0
difference = area - last_area

last_area = area
break if difference.abs <= (1*(10**(-ac)))
end
i1 += 1
puts 'iteration: ' + i1.to_s + 'Compleate...'
end
puts  '-----------------------------------------------------------'
puts 'area: ' + area.to_s
puts 'iterations: ' + (i1 + 1).to_s
puts  '-----------------------------------------------------------'
puts 'type \'exit\' to close, or press enter to continue with another function'
s = gets
case s.upcase
when /\AEXIT/
exit
else
start_program
end
end

def start_program
puts '=============================================='
puts 'Welcome to the area under a curve calculus solver'
puts 'to use simply type you function in below and then follow the prompts'
puts 'for exsample '
puts '((-x)**2) + 2'
puts 'Would be the function x^2 + 2'
puts 'the solver only recognises certain symbols for mathmatical'
puts 'operations, see the below for conversions'
puts 'make sure to use \"X\" when typeing in your equation'
puts '* >> Multiplication'
puts '** >> raise to a power '
puts '/ >> Division '
puts 'Math.cos(x) >> Cosine of x '
puts 'Math.sin(x) >> Sine of x '
puts 'Math.tan(x) >> Tangent of x '
puts 'Math.log(x) >> Ln of x '
puts 'Math.log10(x) >> Log base 10 of x '
puts 'Math.sqrt(x) >> Square root of x '
puts '(x).abs >> Absolute value of '
puts 'Math::E >> e '
puts 'Math::PI >> pi '
puts '=============================================='
puts 'type \'exit\' to close'
puts 'type your function below'
no_input = true
while no_input == true
s = gets
case s.upcase
when ''
puts 'ERROR Please enter a function'
when /\AEXIT/
puts 'Closing...'
exit
else
no_input  = false
end
end
f = 'auc{|x| ' + s.to_s + '}'
begin
eval(f)
rescue
puts 'ERROR'
puts $!.message
end
end

begin
start_program
end


note that if you are using a domain that is in any way large it will take a very long time depending of the number you put in for accuracy
a domain of 10 and an accuracy of 3  took a while (a testament to the slowness of ruby)

I was really proud of my self for making this in less than 3 hours (that includes looking up blocks something I didn't previously know how to use) .
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

fugibo

Ick! You used the box method. I hate that silly thing. Even though it's exactly the same as the integration method, I still hate it. Grr.

Also, after spending five seconds looking over it, it looks like you're using a rather imprecise method. I'm afraid this just won't work well without the fundamental theorem or limits, neither of which can be easily emulated in Ruby.

Ryex

well keep in mind that we were only given the problem to solve today.
(the teacher hasn't even gone over how to officially solve it yet)
I just did my best to create a loop method that would get the same effect as the limit as a approaches 0 of the summation of the area off all the boxes with width a the fit under the curve.
it's the only way I could think to solve it.

and how is it imprecise? it's results can be accurate to what ever place you want, thought considering the speed of the method I wouldn't suggest asking for an accuracy beyond 5. you will be waiting a while if you do.

I made this more to test my self and see it I COULD make, not so much to really use it. just an atempt to se if I could apply way I knew of ruby and the math behind this particular problem.

so what is this "integration method" you speak of?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

winkio

cool that you built it, but not really mathematically useful.  If you want to improve it, use trapezoids instead of boxes, or other even more precise methods if you wish.

Integration is Calculus, you will learn about it.

fugibo

The real solution to the problem is incredibly easy. The way they teach this crap to you is ridiculous. Integration is the opposite of derivation. You use it to find a function for f(x) that's derivative is f(x). Since the function is the sum of the slope of f(x) at each point, it automatically acts as an area function. You can derive a lot of the stuff you're probably going to spend the next week on by yourself if you try.

Ryex

Quote from: winkio on January 15, 2010, 12:43:02 am
cool that you built it, but not really mathematically useful.

you can say that again, it lags like hell.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

fugibo

Quote from: Ryexander on January 15, 2010, 01:21:06 am
Quote from: winkio on January 15, 2010, 12:43:02 am
cool that you built it, but not really mathematically useful.

you can say that again, it lags like hell.


I'm pretty sure even a native implementation would have a decent amount of lag. This is why we created the fundamental theorem. Iteration just doesn't work without an extraordinarily fast computer.