A derivative/limit calculator using python

In summary: If you could describe what f is and how many arguments it takes, that would be helpful. function f(x): return x*x
  • #1
ver_mathstats
260
21
Python:
import math

def derivative_quotient(f, a, h):
    return float((f(a + h) - f(a)) / h)
    if h==0:
        return "Division by zero is not allowed."

Here is my code so far, I am unsure of where to go from here or if I am even on the right track. I am a little confused what it means by "where f is function from float to float". Any help would be appreciated thank you.
 
Technology news on Phys.org
  • #2
The parameter f is a function. In particular, it's a function which takes one argument (a float) and returns one result (also a float).

So if you had a function myfun(x, y) you couldn't pass that as the argument f. If you had a function myfun(x) that returned a boolean value, you shouldn't pass that as the argument f either.

I'm not sure about the syntax of passing a function as an argument in Python or whether it checks any of those things. As far as I know, Python doesn't declare or check the types of its arguments and in fact could accept many different types for the same argument.

Perhaps it's up to you to check: is the argument f a function? How many arguments does f take? What is the return type of f? Again, I'm not sure how you do those things in Python or if you can.

I'm learning Python as a matter of fact but I'm kind of a novice. I've been working with it... let's see now... (counts on fingers and toes), since last Tuesday. So 1 week today.
 
  • #3
By the way, your function as written checks whether h is 0 AFTER using h. If h is 0, you're going to get an error message and never get to that check.
 
  • #4
RPinPA said:
By the way, your function as written checks whether h is 0 AFTER using h. If h is 0, you're going to get an error message and never get to that check.
Yes I did end up getting the error message however I think I have programmed it because my print statements and the tests come up with the results as expected. Thank you for the help.
Python:
import math

def derivative_quotient(f, a, h):
    result = 0
    if h==0:
        return "Division by zero is not allowed."
    else:
        return (f(a + h) - f(a)) / h
This is what I came up with.
 
  • Like
Likes pbuk
  • #5
RPinPA said:
The parameter f is a function. In particular, it's a function which takes one argument (a float) and returns one result (also a float).

So if you had a function myfun(x, y) you couldn't pass that as the argument f. If you had a function myfun(x) that returned a boolean value, you shouldn't pass that as the argument f either.

I'm not sure about the syntax of passing a function as an argument in Python or whether it checks any of those things. As far as I know, Python doesn't declare or check the types of its arguments and in fact could accept many different types for the same argument.

Perhaps it's up to you to check: is the argument f a function? How many arguments does f take? What is the return type of f? Again, I'm not sure how you do those things in Python or if you can.

I'm learning Python as a matter of fact but I'm kind of a novice. I've been working with it... let's see now... (counts on fingers and toes), since last Tuesday. So 1 week today.
Yes I understand. Yes I see, I have been working with Python for only about two or three weeks now so I am very new to it.
 
  • #6
ver_mathstats said:
Yes I did end up getting the error message however I think I have programmed it because my print statements and the tests come up with the results as expected. Thank you for the help.
Python:
import math

def derivative_quotient(f, a, h):
    result = 0
    if h==0:
        return "Division by zero is not allowed."
    else:
        return (f(a + h) - f(a)) / h
This is what I came up with.
This won't work, since you haven't defined a function f.

For example, if the function is f(x) = x2, you could have this definition:
Python:
def f(x):
    return x*x

This should be placed above your definition for derivative_quotient.

Regarding your code, since you're not doing anything that requires the math module, there's no need to import it. Also, you have a variable named result in your code, but aren't doing anything with it, so it should be deleted.
 
  • Skeptical
  • Like
Likes pbuk and sysprog
  • #7
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.

And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
 
  • Like
Likes ver_mathstats
  • #8
Mark44 said:
This won't work, since you haven't defined a function f.
Yes it will, f is passed as an argument to the function derivative_quotient. So after importing this code, typing print(derivative_quotient(math.sin, 0, 0.1)) in the REPL should have exactly the result asked for in the question.

Mark44 said:
This should be placed above your definition for derivative_quotient.
No.

Mark44 said:
Regarding your code, since you're not doing anything that requires the math module, there's no need to import it.
I suspect that there is a line
Code:
print(derivative_quotient(math.sin, 0, 0.1))
lower down in the OP's code that demonstrates the test case in the question which does require import math

Mark44 said:
Also, you have a variable named result in your code, but aren't doing anything with it, so it should be deleted.
Spot on!
 
Last edited:
  • Like
Likes ver_mathstats
  • #9
RPinPA said:
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.
Python does no (implicit) type checking at all.

RPinPA said:
And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
Annotations are exactly that, the Python interpreter doesn't use them at all. Python is not a compiled language (let's ignore JIT compilation for now) so can't work through the code checking that types are correct. That is why we almost always use an IDE with Python which DOES use those annotations, and often a "linter" to check conventions that help avoid problems.

If you are coming from a strongly typed, compiled language these things can take some time to get used to.
 
  • Like
Likes ver_mathstats
  • #10
Mark44 said:
This won't work, since you haven't defined a function f.
pbuk said:
Yes it will, f is passed as an argument to the function derivative_quotient.
In the code shown, there wasn't a call to derivative_quotient, so I didn't think that the function arg had been defined anywhere.
 
  • #11
Is it possible to do abstract differentiation and return a function instead of just a numerical value, e.g., return cosx for input sinx?
 
  • #12
WWGD said:
Is it possible to do abstract differentiation and return a function instead of just a numerical value, e.g., return cosx for input sinx?
That's somewhat off-topic, but yes you can do this with the SymPy package (this is usually called symbolic differentiation rather than abstract).
 
  • Like
Likes WWGD
  • #13
RPinPA said:
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.

And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
Thank you for the comment, I will take a look at this.
 
  • #14
Mark44 said:
In the code shown, there wasn't a call to derivative_quotient, so I didn't think that the function arg had been defined anywhere.
Sorry about that, I should have put it at the bottom.
 
  • #15
EDIT: it seems I already posted almost exactly the same comment as below in #9 which in one way is comforting (in that I am not on this occasion contradicting myself), but is I'm afraid a symptom of the fact that I have been around a lot longer than Python! I will leave the duplication as a reminder to myself of this.

RPinPA said:
It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message.
Python by design is not a strongly typed language so it doesn't do ANY type checking before trying ANY call* - you can try to pass a string where it is expecting a function and it won't notice until runtime. And it is not the call with the wrong type that throws the error, it is type checking code in the called function in this case, or if the called function does not do any type checking then it is the runtime that will error when it tries to execute the internal 'call' method of a string.

* The JIT compiler cannot in general know what the type of a variable is so it is left to the runtime to either implicitly coerce the type or throw an error if it is not checked in userspace.

RPinPA said:
And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that.
These annotations are designed to be used by an Integrated Development Environment (IDE). Programming Python (or really anything with the possible exception of assembler) without an IDE is not something I would recommend. The same goes for a linter.

Note that by IDE here I am including modern text editors with auto-indenting, brace matching, code-completion, type hints etc. such as Visual Studio Code, Sublime Text, Notepad++, Atom etc.
 

1. What is a derivative/limit calculator using python?

A derivative/limit calculator using python is a computer program that uses the programming language Python to calculate derivatives and limits of mathematical functions. It is used to solve complex mathematical problems quickly and accurately.

2. How does a derivative/limit calculator using python work?

A derivative/limit calculator using python works by using algorithms and mathematical formulas to calculate the derivative and limit of a given function. It takes the input of the function and any necessary parameters, and then uses the programming language Python to perform the necessary calculations and provide the output.

3. What are the benefits of using a derivative/limit calculator using python?

There are several benefits to using a derivative/limit calculator using python. Firstly, it can handle complex mathematical functions that may be difficult to solve by hand. It also provides quick and accurate results, saving time and reducing the chances of human error. Additionally, it can handle a large amount of data and can be easily modified for different functions and parameters.

4. Are there any limitations to using a derivative/limit calculator using python?

Like any computer program, a derivative/limit calculator using python has its limitations. It may struggle with certain types of functions, such as those with multiple variables or discontinuities. It also relies on the accuracy of the input and the algorithms used, so errors may occur if the input is incorrect or the algorithms are not precise enough.

5. How can I use a derivative/limit calculator using python for my own research or studies?

If you are a scientist or researcher, you can use a derivative/limit calculator using python to quickly and accurately solve complex mathematical problems related to your field of study. You can also use it for educational purposes to better understand the concepts of derivatives and limits. It is important to have a basic understanding of programming and the mathematical concepts involved to effectively use this tool.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
994
  • Programming and Computer Science
Replies
4
Views
3K
Replies
3
Views
768
  • Programming and Computer Science
Replies
7
Views
432
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top