0

To specify the topic: I am trying to create a program that can evaluate the derivative of at least polynomial and rational functions (trig/etc. functions would be interesting as well) at a specific point. The answer should be the equation of the tangent line at that specific point. I cannot think of any way to do this other than by using the definition of the derivative.

I am a beginner in Ruby and I am trying to make a neat little program to calculate some derivatives. Do any of you know how to do something like this? All I really want it to do is evaluate derivatives of polynomial and rational functions. I appreciate any and all help.

It seems as if I was too vague. I literally do not know where to start and have no code to show. If that is a problem (suspected cheating, etc.), then I will continue practicing Ruby and eventually figure it out. This was more of a post to receive new code than to receive edited code.

  • What particular part are you having issues with? If you were to show us your current code, we could help you in the right direction. – Justin Wood Apr 24 '14 at 23:28
  • That's my biggest problem. I don't really know where to start. I took Calculus a while back and I started dabbling in Ruby about a month ago. This is more for the sake of curiosity than anything else. – user3570975 Apr 24 '14 at 23:33
  • 2
    Are you looking for numerical or symbolic evaluations? – pjs Apr 25 '14 at 00:08
  • Without a code sample, it is not clear from your question what you need most help with. Some people posting as "beginner" have trouble converting basic ideas to syntax at all, others have got basic coding skills but decided to tackle something complex. To get the best help you need to give as many clues to us as possible where you are with understanding and implementing your goal. Usually a small piece of code gives the most information for the least effort (for you and for anyone answering). – Neil Slater Apr 25 '14 at 07:35

2 Answers2

1

Here's a numerical evaluation based on the two-point formula described on Wikipedia. Note that the test values in the comments are the theoretical answers. This seems to work quite well for the square and cube functions, and to 9 decimals for the square root.

# First argument is the function to be evaluated, as a Ruby lambda.
# Second argument is the value at which to evaluate the derivative.
# Returns a numeric approximation to f'(x), using machine epsilon
# scaling to avoid numerical instability issues that would occur if
# the h (delta-x) value is too close to zero.
def derivative(f = lambda {|value| value}, x)   # default f(x) = x
  h = x * Math.sqrt(Float::EPSILON)
  lower = x - h
  upper = x + h
  (f[upper] - f[lower]) / (upper - lower)
end

square = lambda {|value| value * value}    # or value**2 if you prefer
p derivative(square, 1.0)   # 2 * x = 2.0
p derivative(square, 2.0)   # 2 * x = 4.0
p derivative(square, 3.0)   # 2 * x = 6.0
square_root = lambda {|value| Math.sqrt(value)}
p derivative(square_root, 1.0)   # 1 / (2*sqrt(x)) = 0.5
p derivative(square_root, 2.0)   # 1 / (2*sqrt(x)) = 0.35355339059327373
p derivative(square_root, 3.0)   # 1 / (2*sqrt(x)) = 0.2886751345948129
cube = lambda {|value| value * value * value}   # or value**3 if you prefer
p derivative(cube, 1.0)   # 3 * x**2 = 3.0
p derivative(cube, 2.0)   # 3 * x**2 = 12.0
p derivative(cube, 3.0)   # 3 * x**2 = 27.0
pjs
  • 18,696
  • 4
  • 27
  • 56
  • Thanks for the help, but I'll be honest, I have no idea what is going on in this code. It looks simple enough, and after the comment from BroiSatse, I'm sure this is all making me look rather stupid. For example, let's say I want to find the derivative of 4x^2 + 5x, how would I use this code to figure that out? – user3570975 Apr 25 '14 at 10:54
  • You would create a lambda for your function: `my_function = lambda {|x| 4.0 * x**2 + 5.0 * x}`. Then evaluate it any value you were interested by calling `derivative(my_function, value_of_interest)`. For instance, `derivative(my_function, -2.0)` yields -11. The symbolic solution is that the derivative of your function is `8x + 5`, which is in fact -11 when you plug in -2. To see why this works, please read the linked Wikipedia article. – pjs Apr 25 '14 at 15:47
0

Firstly, you need to find some machine-readable representation for your functions. As long as you want to write this only for polynomial and rational function, this is doable and pretty simple (class Function with two subclasses, first one stores coefficients, second one two poynomials).

Once this is done, you need to define a method derivative on polynomial. This is extremely ease as it will return another polynomial.

Third step would be to use the formula for the derivative of quotient of two functions. Since both functions are polynomials (and you already have derivative define on those) you are done.

Please try implement this and post a question when you encounter any problem.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86