Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 semantic error log function
#1
the following function gives an approximation to the natural log function
and is totally correct:

def applog(n,x):
    a0=(1+x)/2
    b0=sqrt(x)
   
    for i in range(n):
        a0=(a0+b0)/2
        b0=sqrt((a0)*b0)
This other code is supposed to make the above function reach convergence faster

def fastln(n, x):
    a0 =(1 + x) / 2
    b0 =x ** (1 / 2)
    for i in range(n):
        a0=(a0 + b0) / 2
        a0=((a0 * b0) ** (1 / 2))
    for i in range(n+1):
        for j in range(1,n):
            a0=xx
            cc=[]
            cc.append(xx)
            ((cc[0][1] - 4 ** (-j) * cc[0][1])/(1 - 4 ** (-j)))
    return (x - 1)/cc[-1][-1]

print(fastln(4,4))
print(fastln(3,3))

x = np.linspace(10, 500, 100)
nn=range(2,10)
colors = mpl.cm.rainbow(np.linspace(0, 1, len(nn)))

for c,n in zip(colors,nn):
    plt.plot(x, (ln(n,x)), label='$n = {nn}$'.format(nn=list(nn)  )) 
however, the graph I obtain is totally unrelated to the log function,
why so? What's wrong with the second code?
Quote
#2
Shouldn't you be assigning to b0 on line 6?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
you are actually right, but strangely so, I still get the same graph,
and one more thing:
on line 19, if I change the range to (1,10)
I get the following error:
local variable 'cc' referenced before assignment,
I cannot understand why.
Quote
#4
I don't think that's the right problem and I don't think you're showing use the code you are actually using. You would get that error if n was 1 in fastln, but you never call fastln with values from nn. Of course, that's probably why your graph doesn't change, since you are calling ln, not fastln. And if you did call fastln, I'd think you'd get a name error, since I don't see anywhere that you defined the xx you are using on lines 9 and 11.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
I have now changed the code and called fastln,

def fastln(n, x):
    a0 =(1 + x) / 2
    b0 =x ** (1 / 2)
    for i in range(n):
        a0=(a0 + b0) / 2
        b0=((a0 * b0) ** (1 / 2))
    for i in range(1,n):
        for k in range(1,n):
            a0=xx
            cc=[]
            cc.append(xx)
            cc[1][1]=((cc[0][1] - 4 ** (-k) * cc[0][0])/(1 - 4 ** (-k)))
    return (x - 1)/cc[-1][-1]

print(fastln(4,4))
#print(fastln(3,3))

for x in range(1,10):
    for n in range(1,10):
        plt.plot((fastln(n,x)))

but I am getting another error:
cc[1][1]=((cc[0][1] - 4 ** (-k) * cc[0][0])/(1 - 4 ** (-k)))

IndexError: list index out of range.

In order to be as clear as possible, I would like to write the sequence for you, so you will have a better idea about how correct the code I wrote is.

Keep in mind that the first 6 lines of my code are totally correct, and the code right after is based on it.

Now we have cc_sub(0,i)=a0
cc_sub(k,i)=( cc_sub(k-1,i)-4^(-k)*cc_sub(k-1,i-1) )/(1 - 4^(-k))


I hope I made myself clear enough.
In case of any further clarifications, feel free to ask.

Thank you for your help.
Quote
#6
The only thing you index in that line is cc. So what is cc? Given lines 10 and 11, we know that cc is [xx]. However, since you still have not shown me where you define xx, I have no idea what that means, and I can't help you.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
I set cc is meant to take the last value of a0 (see the first 6 lines),
but if I write cc=ao, I get an error and not sure why.
Because of this error, I had to set a0 to xx in order for the code to run.

I am not sure how else I should have defined it.
I hope my explanation is clear.
Quote
#8
This code comes from my last posting.
Can anyone let me please know if there is anything
that is not too clear and how to change it?
In case you are wondering about xx=a0
is because if I write directly cc.append(a0)
I get the following error:
TypeError: 'float' object is not subscriptable.

The way the code looks now,
I get :
cc[1][1]=((cc[0][1] - 4 ** (-k) * cc[0][0])/(1 - 4 ** (-k)))

IndexError: invalid index to scalar variable.


I am trying to plot the results for different values of n and x.

Some help would really be beneficial.
Thank you.

def fastln(n, x):
    a0 =(1 + x) / 2
    b0 =x ** (1 / 2)
    for i in range(n):
        a0=(a0 + b0) / 2
        b0=((a0 * b0) ** (1 / 2))
    for i in range(1,n):
        for k in range(1,n):
            a0=xx
            cc=[]
            cc.append(xx)
            cc[1][1]=((cc[0][1] - 4 ** (-k) * cc[0][0])/(1 - 4 ** (-k)))
    return (x - 1)/cc[-1][-1]
 
print(fastln(4,4))
#print(fastln(3,3))
 
for x in range(1,10):
    for n in range(1,10):
        plt.plot((fastln(n,x)))

buran wrote Apr-24-2019, 08:30 AM:
Please, don't start new threads unnecessarily. Keep the discussion in the original thread.
Also please, post the entire traceback that you get in error tags.
Take a time to read What to include in a post
Quote
#9
As ichabood has stated repeatdely xx is not defined and we/you get name error for xx. Running your latest code:
Error:
Traceback (most recent call last): File "somefile.py", line 15, in <module> print(fastln(4,4)) File "somefile.py", line 9, in fastln a0=xx NameError: name 'xx' is not defined >>>
Quote
#10
runfile('C:/Users/Desktop/python ode/untitled3.py', wdir='C:/Users/Desktop/python ode')
Traceback (most recent call last):

File "<ipython-input-283-24ba76ba66c4>", line 1, in <module>
runfile('C:/Users/Desktop/python ode/untitled3.py', wdir='C:/Users/Desktop/python ode')

File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)

File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Desktop/python ode/untitled3.py", line 86, in <module>
print(fastln(4,4))

File "C:/Users/Desktop/python ode/untitled3.py", line 83, in fastln
cc[1][1]=((cc[0][1] - 4 ** (-k) * cc[0][0])/(1 - 4 ** (-k)))

IndexError: invalid index to scalar variable.

I do not get the message "xx not defined"
and as I wrote before, I had to set a0=xx because
appending a0 directly to cc would cause a float object error to appear.
If xx is really causing a problem, can you please tell me how should I do
in order to append a0 to the cc list?
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)