Dichotomy
Functions are shown in rres, This code makes the algorithm run twice
def asdf(x): rres=8*x**3-2*x**2-7*x+3 return rres i=2 left=0 right=1 while i>0 : i = i-1 ans = 0.1 mid1 = (left + right + ans) / 2 mid2 = (left + right - ans) / 2 a=asdf(mid1) c=asdf(mid2) if a > c : right = mid1 else : left = mid2 b=(left+right) / 2 print(" Left limit =%s, Right limit =%s, Minimum x=%s"%(left,right,b))
Left limit =0.45, Right limit =0.775, Minimum x=0.6125
Harvest :
This is my first implementation code . After learning the algorithm , The logical framework basically has , The rest that needs to be clear is the corresponding python Language . So I started looking for “ How to define a function ”( See mofan Youku ),“ The loop body ” and “if Conditional statements ” The format of (https://blog.csdn.net/qq_39407518/article/details/79822498)“ Mathematical symbols ”( See mofan Youku ), as well as print Use
1.def yes python The middle finger defines , Generally used to define functions , If you need deep learning, building a network can be used to define the network . It's worth noting that
return You must add another line after the function .
I don't know why , But if you don't add it , That function formula is a vase , It's like a result can't be lost .
2. The worst thing is logic . At first, the logic was not clear , Or there are omissions in the code , Cause I will left and right Put it in the circulation body , The result is predictable . But it's also because of this mistake , That's true. pycharm Medium debug How to use it? , Pretty simple , Baidu came out at once .
3. I don't know why , Don't bother watching the video print There is no way to output multiple variables together in my pycharm Used in , The result is strange . Maybe it's because I'm win10 No ios Well .print If multiple variables are output together, it must be print(" name :%s, name 2:%s"%(a,b)) The result output is the name :a , name 2:b
return It means to output this def The value of any variable in it is displayed as the result . In general , Is the name of the relational expression of the output function , So when you call this function , The function value corresponding to the variable can be displayed , Otherwise, only run without results , It won't work .
Lattice method —— Three point bisection
import numpy as np def qwer(x): third = np.exp(x) - 5*x return third left = 1 right = 2 mid1 =float(left+right) / 2 mid2 = (left+mid1) / 2 mid3 = (mid1+right) /2 a = qwer(mid1) b = qwer(mid2) c = qwer(mid3) i = 5 while i > 0: i=i-1 if a > b: if c > b : #b right = mid1 mid1 = mid2 a=b mid2 = (left + mid1) / 2 mid3 = (mid1 + right) / 2 b = qwer(mid2) c = qwer(mid3) else:#b>c #c left = mid1 mid1 = mid3 a = c mid2 = (left + mid1) / 2 mid3 = (mid1 + right) / 2 b = qwer(mid2) c = qwer(mid3) else:#b>a if a > c: #C left = mid1 mid1 = mid3 a = c mid2 = (left + mid1) / 2 mid3 = (mid1 + right) / 2 b = qwer(mid2) c = qwer(mid3) else:#b>a&c>a # a left = mid2 right = mid3 mid2 = (left + mid1) / 2 mid3 = (mid1 + right) / 2 b = qwer(mid2) c = qwer(mid3) print(" minimum value =%s"%mid1) print(" Function value =%s"%a)
minimum value =1.609375 Function value =-3.047189552275773
About python Data variables in . The result of the first run is obviously wrong , So I adopted debug. Results found ,mid1 The place has always been 1 instead of 1.5, So I began to understand the data variables . At first I guessed python All variables are integer by default , But according to the result of dichotomy, I realize that this guess is wrong , So change the whole file The variable format is not necessary . So I was there mid1 A is added to the formula float, The result is 1.5 了 . But if I use the whole equation () Cover up , Add in front float, The results are 1. I don't quite understand why . But I know python The data format of is determined according to the amount of input , In other words, if your input is an integer , Then the calculation output directly related to it must be integer , And it's still an integer without carry . I didn't use +float/+.0 Before these two methods ,mid1~3 All integers .
left = 1.0 right = 2.0 mid1 =(left+right) / 2
Or no more mid1 Add in front float, Just click a point after the input quantity
I really want to make complaints about it. print, It's so troublesome. I have to get one every time %s, And sometimes it can't be put together !!!!
Fibonacci Law
def fibonacci(n): i=0 a = 0 b = 1 for i in range(n): i=i+1 c = a+b a = b b = c return c def bn(x): ert = x**2 - 6*x + 2 return ert z = 2 p = 0 left = 0.00000 right = 10.00000 L1 = right - left while z < 100: m = fibonacci(z) l = L1/m k = 1.000/m if k < 0.03: print("n=%s,Fn=%s"%(z,m)) L2 = l*fibonacci(z-1) t = left + L2 r = right -L2 while p < 3: p = p + 1 l3 = t - r e= bn(t) o = bn(r) if e>o : right = t t = r r = left + l3 else:#o>e left = r r = t t = right - l3 break else: z = z + 1 okk=(left+right)/2 okky=bn(okk) print(left) print(right) print(" Minimum x=",okk) print(" Minimum y=",okky)
Don't ask what I have mastered , Ask me how much I love after writing this code now python The precision of the expression :-) I decided that as long as I write the code of mathematical formula in the future, I will fill a lot of small mathematical points after the input amount 0
fibonacci Function definition , Every time debug My hands are shaking after O(∩_∩)O~
The golden section
def gold(x): gg= x**2 - 6*x + 9 return gg left = 1 right = 7 ans = 0.4 a = left + 0.618 * (right - left) b = left + 0.382*(right - left) gga = gold(a) ggb = gold(b) i = 0 while i < 7: print("i=%s" % i) print("left=%s,right=%s" % (left, right)) print("x Left =%s,x Right =%s" % (a, b)) print("y Left =%s,y Right =%s" % (ggb, gga)) c = right - left if c > 0.4: i = i + 1 if gga > ggb: right = a a = b b = left + 0.382*(right - left) gga = ggb ggb = gold(b) else:#gga<ggb left = b b = a a = left + 0.618 * (right - left) ggb = gga gga = gold(a) else: break
I don't know when I have OCD , As long as there is “~” I have to eliminate . Smile to cry . This is very simple , The first four except Fibonacci , All very simple. .
def yy(x): y=x**4-4*x**3-6*x**2-16*x+4 return y def xing(xm1,xm2,xm3,fm1,fm2,fm3): yxxx=0.5000*((xm2**2-xm3**2)*fm1+(xm3**2-xm1**2)*fm2+(xm1**2-xm2**2)*fm3)/((xm2-xm3)*fm1+(xm3-xm1)*fm2+(xm1-xm2)*fm3) return yxxx x1 = -1.0000 f1 = yy(x1) x3 = 6 f3 = yy(x3) x2 = 0.50000*(x1+x3) f2 = yy(x2) xp = xing(x1,x2,x3,f1,f2,f3) fp = yy(xp) a = abs(xp-x2) while abs(xp-x2) > 0.05000: a = abs(xp - x2) if xp > x2: if fp > f2: x3=xp f3=fp xp = xing(x1, x2, x3, f1, f2, f3) fp = yy(xp) print("ans=%s" % a) print("left=%s,right=%s" % (x1, x3)) print("x*=%s,fp*=%s" % (xp, fp)) print("x2=%s,f2=%s" % (x2, f2)) print("******************") else:#f2>fp x1 = x2 f1 = f2 x2 = xp f2 = fp xp = xing(x1, x2, x3, f1, f2, f3) fp = yy(xp) print("ans=%s" % a) print("left=%s,right=%s" % (x1, x3)) print("x*=%s,fp*=%s" % (xp, fp)) print("x2=%s,f2=%s" % (x2, f2)) print("******************") else:#xp<x2 if fp > f2: x1 = xp f1 = fp xp = xing(x1, x2, x3, f1, f2, f3) fp = yy(xp) print("ans=%s" % a) print("left=%s,right=%s" % (x1, x3)) print("x*=%s,fp*=%s" % (xp, fp)) print("x2=%s,f2=%s" % (x2, f2)) print("******************") else: x3 = x2 f3 = f2 x2 = xp f2 = fp xp = xing(x1, x2, x3, f1, f2, f3) fp = yy(xp) print("ans=%s" % a) print("left=%s,right=%s" % (x1, x3)) print("x*=%s,fp*=%s" % (xp, fp)) print("x2=%s,f2=%s" % (x2, f2)) print("******************")
This formula looks troublesome , Be more careful when writing . Last time I put that 2 Put it under the semicolon , The result is great , So it's still converted to 0.5 Better (PS: Don't forget the long river 0).
Although the code is very long , But mainly because print Too much . I was going to start with print, The final result will miss the last part . Too lazy to think of another way , Just do it
indirect method —— Newton method
def fd(x): y = 4*x**3-12*x**2-12*x-16 return y def fdd(x): ys = 12*x**2-24*x-12 return ys i = 1 x0 = 3.00000 ans = 0.001 while i < 7: fd0 = fd(x0) fdd0 = fdd(x0) if abs(fd0) > ans: x1 = x0 - (fd0/fdd0) x0 = x1 print(" frequency :%s, The value obtained x:%s"%(i,x1)) i = i + 1 else:#fd0<0.001 print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") print("Bingo! Smooth customs clearance ! I wish you a happy start to school !") print("Boss X=%s"%x0) break
In limine while Inside < It has been written. >, Lead to run Don't come out . then ,debug It doesn't work . Check it online to find out “ No networking ”+“ No breakpoint selected ”. Finally, I want to try to else Output the content inside , Results found run Later I was swiped . So I changed it to i<7 Not in the future , So I thought of adding one break Out of the loop , It worked .
Then just by debug For a moment , It turned out to be i+1 stay if Inside , Because there's no way +1, therefore i=6 Always exist , It's going to keep cycling . Because plus break Good ,i+1 Good , Fine .
This is the first group I realized by myself python Code , It's a mathematical formula python Language assembled . At the beginning, I know what needs to be reflected in the language , But I'm not sure . So I found some dichotomy on the Internet , They are all different , But the framework is similar , But if you want to use our formula, you need to change a lot . Then I began to analyze our problem , I found that it generally needs two parts , Part of the function definition , Part of the circulatory body . But I don't know how to define functions , How to write mathematical formulas , How to make variables , In other words, some small points are not very good at , So I choose Baidu directly . Because I know my reading ability is good , Compared with extracting features from video , I'm better at getting points through reading . Find knowledge purposefully , Grasp more firmly .
So I started the first —— Preparation of dichotomy . I find , I have made a lot of mistakes, and many places are very basic . But I still didn't choose video , But find these problems directly on Baidu , Because after the video, maybe you didn't find something . Of course , This is a step-by-step process , Instead of just putting the program on it , Change little by little .
With the success of the first two , I found myself confident in these codes , Seems to see through their disguise , Grasp the essence . besides , I also realized that since 8 After the month , Learning ability seems to have improved a lot , And there are more effective learning methods . There has been a certain awakening in all aspects . Except for the first one who found a few wrong codes , Others are written according to their own logic , When the logic comes down , If you don't know how to translate a part of the corresponding language, go to Baidu , In fact, these routines are the same, or the routines of mathematical formula transformation are the same .
I also realized , Assembly is actually the most difficult language , What I have learned so far , Because many need to define themselves , Go to hell , Need to remember a lot of flexible and inflexible instructions . But others just need to write down some corresponding .python It's really easy . and , I find myself opening the door to a new world today , I fell in love with this spiritual thing , Full of rigorous beauty , And the unknown change , I found that I seem to be in love with code . It may not be limited to python, These languages are full of challenges . I think when you're confused , You need to trust your intuition , At least I found it accurate