Python: Derivative of trig functions [closed]












0















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?










share|improve this 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 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
















0















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?










share|improve this 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 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer


























  • 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




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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





share|improve this answer


























  • 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


















1














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





share|improve this answer


























  • 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
















1












1








1







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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





















  • 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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks