summaryrefslogtreecommitdiff
path: root/tbc/static/uploads/Hardik/C
diff options
context:
space:
mode:
Diffstat (limited to 'tbc/static/uploads/Hardik/C')
-rw-r--r--tbc/static/uploads/Hardik/C/images/certificate.pngbin0 -> 71052 bytes
-rw-r--r--tbc/static/uploads/Hardik/C/images/pr.jpegbin0 -> 63824 bytes
-rw-r--r--tbc/static/uploads/Hardik/C/images/scipy.pngbin0 -> 14314 bytes
-rw-r--r--tbc/static/uploads/Hardik/C/newton.py35
-rw-r--r--tbc/static/uploads/Hardik/C/newton_raphson.py132
-rw-r--r--tbc/static/uploads/Hardik/C/nr.py51
6 files changed, 218 insertions, 0 deletions
diff --git a/tbc/static/uploads/Hardik/C/images/certificate.png b/tbc/static/uploads/Hardik/C/images/certificate.png
new file mode 100644
index 0000000..c473b82
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/images/certificate.png
Binary files differ
diff --git a/tbc/static/uploads/Hardik/C/images/pr.jpeg b/tbc/static/uploads/Hardik/C/images/pr.jpeg
new file mode 100644
index 0000000..a7edf07
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/images/pr.jpeg
Binary files differ
diff --git a/tbc/static/uploads/Hardik/C/images/scipy.png b/tbc/static/uploads/Hardik/C/images/scipy.png
new file mode 100644
index 0000000..0036b02
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/images/scipy.png
Binary files differ
diff --git a/tbc/static/uploads/Hardik/C/newton.py b/tbc/static/uploads/Hardik/C/newton.py
new file mode 100644
index 0000000..258dc58
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/newton.py
@@ -0,0 +1,35 @@
+from sympy import *
+import sys
+
+
+print "Enter a function: ",
+func = raw_input()
+
+try:
+ sym_x = Symbol('x')
+ fx = S(func)
+ fdashx = diff(fx, Symbol('x'))
+except:
+ print "Function could not be differenciated..."
+ sys.exit()
+
+print "Enter initial guess: ",
+curr_root = float(raw_input())
+print "Enter precision value: ",
+precision = float(raw_input())
+new_root = (curr_root - fx.subs({sym_x : curr_root})/fdashx.subs({sym_x : curr_root}))
+
+
+iterations = 1
+while(abs(new_root-curr_root)>precision):
+ curr_root = new_root
+ new_root = (curr_root - fx.subs({sym_x : curr_root})/fdashx.subs({sym_x : curr_root}))
+ iterations += 1
+ if iterations >= 50:
+ print "Root was not derived uptil 50 iterations, current root: ", curr_root
+ break
+
+print "Function: ", fx
+print "Given tolerance: ", precision
+print "Number of iterations: ", iterations
+print "Root to the funtion upto given precision: ", curr_root
diff --git a/tbc/static/uploads/Hardik/C/newton_raphson.py b/tbc/static/uploads/Hardik/C/newton_raphson.py
new file mode 100644
index 0000000..c18f135
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/newton_raphson.py
@@ -0,0 +1,132 @@
+import math # Need to import math to use the log function
+from Tkinter import *
+import ttk
+import tkMessageBox
+import tkFileDialog
+import os
+import sys
+# Variables that should be noted
+# G = Guess value
+# count = f(x)
+# den = f'(x)
+# Acc = Accuracy input from user
+
+def CheckVals():
+# Define variables as floats - decimal places
+ r = 1
+ if r == 1:
+ try: #If any of the variables have errors catch
+ G = float(GuessInp.get())
+ A = float(AInp.get())
+ B = float(BInp.get())
+ Precision = int(AccInp.get())
+ r = 0
+ CalcRoot()
+ except ValueError:
+ tkinter.messagebox.showinfo("Look carefully","Oops, a variable isn't properly set")
+ r = 0
+
+ return
+
+def CalcRoot():
+ # Start the while loop
+ G = float(GuessInp.get())
+ A = float(AInp.get())
+ B = float(BInp.get())
+ Precision = int(AccInp.get())
+ Acc = math.pow(0.1,Precision)
+
+ count = Acc+1 # Just so we can get into the while loop (count > Acc)
+ while count > Acc :
+ count = (G*(math.log(A*G)))
+ count = count - B
+ if count > Acc:
+
+ if count*A < 0: #Value not allowed
+ tkinter.messagebox.showinfo("Look carefully","Calculations will become unstable Pi * a <0")
+ return
+
+## if ((math.pow(math.exp(1),-1)/A)*count): #This seems like one of the prerequisites not sure if there is a typing error on the tut
+## tkinter.messagebox.showinfo("Look carefully","Calculations will become unstable Pi((e^-1)/a)")
+## return
+
+ #Actual calculation
+ den =(math.log(A*G)) #This is the derivtive denominator
+ den = den + 1
+ numb = count/den
+ Temp = G - numb #Guess value minus the calculated numb
+ G = Temp
+ count = (G*(math.log(A*G)))
+ count = count - B
+
+ button1.pack_forget() #Hide Calculate button after success
+ GText = StringVar()
+ GText.set("Calculated Root: ")
+ label1 = Label(app, textvariable=GText, height = 4)
+ label1.pack()
+ Roottxt = StringVar(None)
+ Roottxt.set(G)
+ label7 = Label(app, textvariable=Roottxt)
+ label7.pack()
+ return
+
+
+app = Tk()
+app.title ("Newton Raphson approximation")
+app.geometry('340x550+100+100')
+
+CommText = StringVar()
+CommText.set("Please complete the form and then click calculate")
+label1 = Label(app, textvariable=CommText, height = 4)
+label1.pack()
+
+fxtxt = StringVar()
+fxtxt.set("f(x) = (x * Ln(x * a)) - b\nf'(x) = 1 + Ln(ax)")
+label2 = Label(app, textvariable=fxtxt, height = 4)
+label2.pack()
+
+#Input a
+Atxt = StringVar()
+Atxt.set("a = ")
+label3 = Label(app, textvariable=Atxt, height = 2)
+label3.pack()
+
+A = StringVar(None)
+AInp = Entry(app, textvariable=A)
+AInp.pack()
+
+#Input b
+Btxt = StringVar()
+Btxt.set("b = ")
+label4 = Label(app, textvariable=Btxt, height = 2)
+label4.pack()
+
+B = StringVar(None)
+BInp = Entry(app, textvariable=B)
+BInp.pack()
+
+#Input acc
+DegText = StringVar()
+DegText.set("To what accuracy do you want the root: ")
+label5 = Label(app, textvariable=DegText, height = 4)
+label5.pack()
+
+Acc = StringVar(None)
+AccInp = Entry(app, textvariable=Acc)
+AccInp.pack()
+
+#Input guess
+Guesstxt = StringVar()
+Guesstxt.set("What is your guess of the root")
+label6 = Label(app, textvariable=Guesstxt, height = 4)
+label6.pack()
+
+G = StringVar(None)
+GuessInp = Entry(app, textvariable=G)
+GuessInp.pack()
+
+#Calculation button
+button1 = Button(app, text = "Calculate root", width = 20, command = CheckVals)
+button1.pack(padx = 15,pady = 15)
+
+app.mainloop()
diff --git a/tbc/static/uploads/Hardik/C/nr.py b/tbc/static/uploads/Hardik/C/nr.py
new file mode 100644
index 0000000..47ac557
--- /dev/null
+++ b/tbc/static/uploads/Hardik/C/nr.py
@@ -0,0 +1,51 @@
+import sys, math, argparse, time
+from sympy import *
+
+start_time = time.time()
+
+# using argparse to get command line arguments
+parser = argparse.ArgumentParser()
+parser.add_argument("-f", "--function", help = "Define a function")
+parser.add_argument("-s", "--starting", help = "Starting point value", type = float, default = 0.0)
+parser.add_argument("-p", "--precision", help = "Convergence precision", type = float, default = 5*10**(-6))
+args = parser.parse_args()
+
+sym_x = Symbol('x')
+# convert the given function to a symbolic expression
+try:
+ fx = S(args.function)
+except:
+ sys.exit('Unable to convert function to symbolic expression.')
+
+# calculate the differential of the function
+try:
+ dfdx = diff(fx, Symbol('x'))
+except:
+ sys.exit('Unable to differentiate function.')
+
+# e is the relative error between 2 consecutive estimations of the root
+e = 1
+x0 = args.starting
+iterations = 0
+
+while ( e > args.precision ):
+ # new root estimation
+ try:
+ r = x0 - fx.subs({sym_x : x0})/dfdx.subs({sym_x : x0})
+ except ZeroDivisionError:
+ print "Function derivative is zero. Division by zero, program will terminate."
+ sys.exit()
+ # relative error
+ e = abs((r - x0)/r)
+ iterations += 1
+ x0 = r
+
+total_time = time.time() - start_time
+
+print 'Function:'
+pprint(fx)
+print 'Derivative:'
+pprint(dfdx)
+print 'Root %10.6f calculated after %d iterations'%(r, iterations)
+print 'Function value at root %10.6f'%(fx.subs({sym_x : r}),)
+print 'Finished in %10.6f seconds'%(total_time,)