Mathematics connects with biology in such an intuitive way | how to calculate a microbe in Python

0. introduction

Let's take a look at these words from the book "the acme of Science: talking about artificial intelligence":




This part introduces the game of life - another very interesting topic. But now let's mainly consider how to draw the above radiolaria, and even all the microbes in the last picture.

1. drawing

Let's not consider those complex iterative equations first, that is to say, if we have a convenient iterative equation iterative equation (x, y), it will return the results after iteration. In this way, we can first concentrate on how to carry out the iteration for a long enough time, and how to draw the results of the iteration.

Let's consider combining the iterative process with the drawing to avoid using another large list to record the results - the result of this is that the function written may not be very clear, but we try to do this. The drawing library we use is matplotlib.

As long as the iteration is long enough, we set it up to 100 times. The element of the iteration is the pixels in the entire canvas. We will choose a suitable range and adjust it appropriately (no need to be the same as the real pixel). At the same time, the choice of color is not very clear. We will adjust it in the process of writing.

Let's start with this:

def judge(x,y,r_square):
	'Judge whether the point is r In a circle of radius'

	distance = x ** 2 + y ** 2
	if distance >= r_square:
		return True
	else:
		return False

def draw(xstart=0,ystart=0,xend,yend):
	'Draw'

	plt.figure()

	#Guess an r value.
	r_square = ((xstart + xend) / 2) ** 2 + ((ystart + yend) / 2) ** 2

	for x in range(xstart,xend + 1):
		
		for y in range(ystart,yend + 1):
			
			for time in range(100):
				(xnew,ynew) = (x,y)
				xnew,ynew = iterated_function(xnew,ynew)
				#The coordinate values are given in the first two loops, and 100 iterations are carried out in the third loop.
				
			if judge(xnew,ynew,r_square):
				'The point falls outside the circle:'
				plt.scatter(x,y,c='black',s = 0.1)

Our current draw() function has been able to perform simple functions, but

  1. The value of r'squart may still need to be adjusted by ourselves.
  2. Too much gap between pixels will result in rough image.
  3. In order to improve the efficiency, we may need to discuss how to matrix the calculation process.

These problems are perfected in the discussion of iterative function.

2. Simple iterative function

Let's first convert the formula given in the introduction into Python code and run the whole program, and then perfect the whole program with the results of these simple steps.

The iterative function is as follows:

def iterated_function(x,y):
	'Iterated function'

	xnew = x ** 3 - 3 * x * (y ** 2) + 0.5
	ynew = 3 * (x ** 2) * y - y ** 3

	return xnew,ynew

We have a simple test of the function:

(x,y) = (1,1)

for i in range(10):

	x,y = iterated_function(x,y)

	print(x,y)

The test results are as follows:

-1.5 2
15.125 5.5
2087.986328125 3608.2578125
-72450800441.37303 214822239.65589142
-3.802928053126974e+32 3.382872253495475e+30
-5.4985885011831324e+97 1.4676808096754469e+96
-1.658916062194702e+293 1.3309206334846214e+292
Traceback (most recent call last):
  File "microorganism.py", line 44, in <module>
    x,y = iterated_function(x,y)
  File "microorganism.py", line 6, in iterated_function
    xnew = x ** 3 - 3 * x * (y ** 2) + 0.5
OverflowError: (34, 'Result too large')

No matter how much we adjust in the pre-determined range, the result is devastating - we have encountered new problems.

Try to solve the problem:

When reviewing the text in the introduction, we can see that the radius of the circle is not given. Combined with the previous understanding of the similar problem - mandelberg set, we guess that the condition for drawing black or white points is like that in mandelberg set - whether the iterative result will tend to infinity.

We will try new things, give new code and try to run it:

import matplotlib.pyplot as plt

def iterated_function(x,y):
	'Iterated function'

	xnew = pow(x,3) - 3 * x * pow(y,2) + 0.5
	ynew = 3 * pow(x,2) * y - pow(y,3)

	return xnew,ynew
'''
def judge(x,y,r_square):
	'Judge whether the point is r In a circle of radius'

	distance = x ** 2 + y ** 2
	if distance >= r_square:
		return True
	else:
		return False
'''
def draw(xend,yend,xstart=0,ystart=0):
	'Draw'

	plt.figure()
	'''
	#Guess an r value.
	r_square = ((xstart + xend) / 2) ** 2 + ((ystart + yend) / 2) ** 2
	'''
	for x in range(xstart,xend + 1):
		
		for y in range(ystart,yend + 1):
			try:
				for time in range(100):
					(xnew,ynew) = (x,y)
					xnew,ynew = iterated_function(xnew,ynew)
					#The coordinate values are given in the first two loops, and 100 iterations are carried out in the third loop.
				scatter(x,y)				
			except:
				continue
			

draw(100,100)

After running, it was found that there was no output image - that is, it did not solve the problem.

summary

Our problem may be in mathematics or in procedure.
In the next installment, we'll try to think about these equations mathematically, and at the same time draw an image to see.
And, we'll check the code. If necessary, we will draw mandebo sets to solve this problem.

Published 20 original articles, praised 0, visited 1026
Private letter follow

Tags: Python

Posted on Sun, 09 Feb 2020 07:04:15 -0500 by GodzMessanja