Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 semantic error log function mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #1 Apr-23-2019, 12:54 PM 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 - 4 ** (-j) * cc)/(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? ichabod801 Number Four Posts: 4,092 Threads: 90 Joined: Sep 2016 Reputation: 254 Likes received: 1222 #2 Apr-23-2019, 01:45 PM 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 mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #3 Apr-23-2019, 06:16 PM 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. ichabod801 Number Four Posts: 4,092 Threads: 90 Joined: Sep 2016 Reputation: 254 Likes received: 1222 #4 Apr-23-2019, 06:58 PM 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 mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #5 Apr-23-2019, 07:12 PM 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=((cc - 4 ** (-k) * cc)/(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=((cc - 4 ** (-k) * cc)/(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. ichabod801 Number Four Posts: 4,092 Threads: 90 Joined: Sep 2016 Reputation: 254 Likes received: 1222 #6 Apr-23-2019, 07:24 PM 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 mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #7 Apr-23-2019, 07:29 PM 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. mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #8 Apr-24-2019, 08:22 AM (This post was last modified: Apr-24-2019, 08:30 AM by buran. Edited 1 time in total.) 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=((cc - 4 ** (-k) * cc)/(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=((cc - 4 ** (-k) * cc)/(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 buran  Posts: 4,786 Threads: 86 Joined: Sep 2016 Reputation: 323 Likes received: 1045 #9 Apr-24-2019, 08:32 AM (This post was last modified: Apr-24-2019, 08:33 AM by buran. Edited 1 time in total.) 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 print(fastln(4,4)) File "somefile.py", line 9, in fastln a0=xx NameError: name 'xx' is not defined >>>`````` mcgrim Spam, spam, eggs, and spam  Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #10 Apr-24-2019, 08:34 AM (This post was last modified: Apr-24-2019, 08:37 AM by mcgrim.) runfile('C:/Users/Desktop/python ode/untitled3.py', wdir='C:/Users/Desktop/python ode') Traceback (most recent call last): File "", line 1, in 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 print(fastln(4,4)) File "C:/Users/Desktop/python ode/untitled3.py", line 83, in fastln cc=((cc - 4 ** (-k) * cc)/(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? « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)