Unit 2 learn the maximum value problem and related variable rate of calculus with python

Posted by yasir_memon on Sun, 09 Jan 2022 12:25:35 +0100

The content of this article comes from learning MIT open course: univariate calculus - correlation rate - Netease open course

1, Examples of best value problems

1. Cut a line with a length of 1 into 2 segments, circle each segment into a square, and find the maximum area that can be obtained

,

Calculate both ends:

Therefore, when the stagnation point meets the conditions, the greater the x, the greater the value of the function. When x - > 1, the function is the largest

Therefore, when the stagnation point meets the conditions, the smaller x, the greater the value of the function. When X - > 0, the function is the largest

Considering the stagnation point of high, middle and low on both sides, the minimum area value at the stagnation point is 1 / 32

from sympy import *
import numpy as np 

import matplotlib.pyplot as plt 

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_aspect(10 ) 

def DrawXY(xFrom,xTo,steps,expr,color,label,plt):
    yarr = []
    xarr = np.linspace(xFrom ,xTo, steps) 
    for xval in xarr:
        yval = expr.subs(x,xval)
        yarr.append(yval)
    y_nparr = np.array(yarr) 
    plt.plot(xarr, y_nparr, c=color, label=label)    

def TangentLine(exprY,x0Val,xVal):
    diffExpr = diff(exprY)
    x1,y1,xo,yo = symbols('x1 y1 xo yo')
    expr = (y1-yo)/(x1-xo) - diffExpr.subs(x,x0Val)
    eq = expr.subs(xo,x0Val).subs(x1,xVal).subs(yo,exprY.subs(x,x0Val))
    eq1 = Eq(eq,0)
    solveY = solve(eq1)
    return xVal,solveY

def DrawTangentLine(exprY, x0Val,xVal1, xVal2, clr, txt):
    x1,y1 = TangentLine(exprY, x0Val, xVal1)
    x2,y2 = TangentLine(exprY, x0Val, xVal2)
    if len(txt)>0:
        plt.plot([x1,x2],[y1,y2], color = clr, label=txt)
    else:
        plt.plot([x1,x2],[y1,y2], color = clr)
        
x= symbols('x')
y = x*x/16 + (1-x)*(1-x)/16
DrawXY(0,1,100,y,'green','x*x/16 + (1-x)*(1-x)/16',plt)
plt.plot([0,1],[1/16,1/16], color = 'gray', label= 'y=1/16')

plt.legend(loc='lower right')
plt.show() 

 

2. Find a box with a fixed volume without a top cover and minimize its surface area. (tip: the bottom is a square - because the perimeter of a square is the shortest)

Here y can be called constraint,

(no top)

A' = 0

(stagnation point)

Endpoint

Because the stagnation point is So the picture should be

Consider the second derivative

(from x > 0, the formula is always positive, so the figure should be concave, and the stagnation point should be the minimum point)

The optimal solution is: (my answer is different from that in the video...)

These ratios are meaningful only with dimensionless solutions, and the units need to be unified:

(my answer is different from that in the video...)

So the best solution for making a box is that the ratio of the bottom edge to the height is 2

3. Implicit function derivation method

, V is a fixed value

(no top), minimum required

Because y is a function of x,

It is understood quickly, but it is impossible to check whether the solution is the maximum, minimum or stagnation point

For this problem, only one method can be used to calculate the boundary, but the teacher said that in real life, many formulas can easily see the boundary and so on, so it is easy to distinguish whether this solution is needed

2, Correlation rate

1. There's a policeman, 30 feet from the road. You drive here. The police have a radar and are measuring the speed. The radar shows a distance of 50 feet from you, and your car is approaching at 80 feet per second in the direction of the radar. Speeding 95 feet per second, 65 miles per hour. The question is, are you speeding?

Note: the distance from the car to the vertical foot of the straight line on the figure changes, so the distance is x. at the same time, the distance between the car and the police also changes, set it as D, and the time change is set as t. here is mainly the calculation .

Topics: Python Back-end