python 最速曲线

关于最速曲线的介绍有 

http://zhidao.baidu.com/s/daily/2014-04-21/1403015178.html

内容比较丰富,还比较好玩

最速曲线公式

理论解很久之前就已经有了,如下

参考:https://blog.csdn.net/WhoisPo/article/details/47149395

# 下面用数值方法求解最速曲线
import math

x0 = 10;
y0 = 10;
etof = 1e-2; # 误差,精度为0
.1
a0 = 0; # 入射角的边值
a1 = math.pi / 30;

dex = 0.1; # 最大步长
dey = 0.1;

while (1):
    a_init = (a0 + a1) / 2; # 入射角的初值

    a = a_init;
    dx = dex; # 起始步长
    dy = 1/math.tan(a) * dx;
    while (dy > dey):
        dx = dx / 10; # 细化步长
        dy = 1/math.tan(a) * dx;
    #end
    x = dx;
    y = dy;
    while (x + dx <= x0):
        yt = y +1/math.tan(a) * dx;
        if (yt < 0):
            break; # 已久上升到初始高度,结束
        #end
        if (math.sin(a) / math.sqrt(y) * math.sqrt(yt) >= 1): # 考虑全反射
            a = -a;
            yt = y + 1/math.tan(a) * dx;
        #end
        at = math.asin(math.sin(a) / math.sqrt(y) * math.sqrt(yt));
        x = x + dx;
        y = yt;
        a = at;

        dy = 1/math.tan(a) * dx;
        while (dy < dey / 10 and dx < dex): # 适当放大步长
            dx = dx * 10;
            dy = 1/math.tan(a) * dx;
        #end
    #end

    if (abs(y - y0) < etof and x + dx > x0):
        break;
    #end
    if (x + dx <= x0 or y < y0):
        a1 = a_init;
    else:
        a0 = a_init;
    #end
#end

a = (a0 + a1) / 2;

# scatter(0, 0, 'go');
dx = dex;
dy = 1/math.tan(a) * dx;
while (dy > dey):
    dx = dx / 10;
    dy = 1/math.tan(a) * dx;
#end
# scatter(dx, dy, 'go');
x = dx;
y = dy;
while (x + dx <= x0):
    yt = y + 1/math.tan(a) * dx;
    if (math.sin(a) / math.sqrt(y) * math.sqrt(yt) >= 1):
        a = -a;
        yt = y + 1/math.tan(a) * dx;
    #end
    at = math.asin(math.sin(a) / math.sqrt(y) * math.sqrt(yt));
    x = x + dx;
    y = yt;
    a = at;
    # scatter(x, y, 'go');
    dy = 1/math.tan(a) * dx;
    while (dy < dey / 10 and dx < dex):
        dx = dx * 10;
        dy = 1/math.tan(a) * dx;
    #end

你可能感兴趣的:(python,拾起高数)