I have made a function RC(n) that given any n changes the digits of n according to a rule. The function is the following
def cfr(n):
return len(str(n))-1
def n_cfr(k,n):
J=str(k)
if "." in J:
J2=J.replace(".", "")
return J2[n-1]
else:
return J[n]
def RC(n):
if "." not in str(n):
return n+1
sum=0
val=0
for a in range(1,cfr(n)+1):
O=(int(n_cfr(n,a)))*10**(-a+1)
if int(n_cfr(n,a))==9:
val=0
else:
val=O+10**(-a+1)
sum=sum+val
return sum
I would like to draw this function for non-integers values of n. A friend gave me this code that he used in other functions but it doesn't seem to work for me:
def draw(f,a,b,res):
import numpy as np
import matplotlib.pyplot as plt
x=[a+(b-a)*i/res for i in range(0,res)]
y=[f(elm) for elm in x]
plt.plot(np.asarray(x), np.asarray(y))
plt.show()
I'm not familiar with plotting functions using python so could anyone give me some help? Thanks in advance
The line in your function should be x = list(range(a, b, res))
the first two arguments of range
are start
and stop
. Here is a better version of draw:
def draw(f, a, b, res):
import numpy as np
import matplotlib.pyplot as plt
x = list(range(a, b, res))
plt.plot(x, map(f, x))
plt.show()