Python: Derivative of trig functions [closed]
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
python
closed as unclear what you're asking by tripleee, eyllanesc, jww, ewolden, Machavity Nov 29 '18 at 15:17
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
python
closed as unclear what you're asking by tripleee, eyllanesc, jww, ewolden, Machavity Nov 29 '18 at 15:17
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
please fix the indentation of your code. Simply paste it in, select it and then hitCtrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54
add a comment |
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
python
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
python
python
edited Nov 27 '18 at 7:50
AkshayNevrekar
4,83891837
4,83891837
asked Nov 27 '18 at 7:35
user10709742user10709742
81
81
closed as unclear what you're asking by tripleee, eyllanesc, jww, ewolden, Machavity Nov 29 '18 at 15:17
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by tripleee, eyllanesc, jww, ewolden, Machavity Nov 29 '18 at 15:17
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
please fix the indentation of your code. Simply paste it in, select it and then hitCtrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54
add a comment |
please fix the indentation of your code. Simply paste it in, select it and then hitCtrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54
please fix the indentation of your code. Simply paste it in, select it and then hit
Ctrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
please fix the indentation of your code. Simply paste it in, select it and then hit
Ctrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54
add a comment |
1 Answer
1
active
oldest
votes
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x)
evaluated at 5
(radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x))
, which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5))
.
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x)
evaluated at 5
(radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x))
, which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5))
.
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
add a comment |
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x)
evaluated at 5
(radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x))
, which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5))
.
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
add a comment |
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x)
evaluated at 5
(radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x))
, which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5))
.
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x)
evaluated at 5
(radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x))
, which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5))
.
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
edited Nov 27 '18 at 7:54
answered Nov 27 '18 at 7:48
b-fgb-fg
1,95911522
1,95911522
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
add a comment |
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
Thanks for answering! I have another problem though: If my function is let's say f(x) = sin(e^x) and I need to get the derivative of that, My code would be:
– user10709742
Nov 27 '18 at 11:34
add a comment |
please fix the indentation of your code. Simply paste it in, select it and then hit
Ctrl+K
– Ev. Kounis
Nov 27 '18 at 7:37
probably you are missing the chain rule of derivatives
– mikuszefski
Nov 27 '18 at 7:54