给孩子写代码(一)--你在月亮上能跳多远

  1. 武汉近三年空气质量

import matplotlib.pyplot as plt


def plot_forecast():


    mo2019 = [125, 80, 76, 62, 65, 46, 44, 50, 58, 56, 77, 93]
    mo2020 = [75, 58, 0, 0, 0,23,35,39,47,54,55,99]
    mo2021 = [90, 66, 55, 48, 40]

    m=range(1,13)
    m2021=range(1,6)
    plt.plot(m,mo2019,m,mo2020,m2021,mo2021)
    plt.legend([2019,2020,2021])
    plt.show()

if __name__ == '__main__':
    plot_forecast()
  1. 8大行星和太阳间的引力

'''
quad_function_plot.py

Plot a Quadratic function 
'''
import matplotlib.pyplot as plt
import pylab
import sympy




def generate_F_r():




    F = []
    G = 6.674 * (10 ** -11)

    sunWeight = 2.0*(10**30)


    # mercury
    mercuryWeight = 4.67*(10**24)
    mercuryDist=57910 * (10 ** 6)
    mercuryForc = G * (sunWeight * mercuryWeight) / (mercuryDist ** 2)
    mercuryForc=mercuryForc/1000/1000/10000/10000/10000
    F.append(mercuryForc)

    # Venus
    VenusWeight = 4.8 * (10 ** 24)
    VenusDist = 10820 * (10 ** 7)
    VenusForc = G * (sunWeight * VenusWeight) / (VenusDist ** 2)
    VenusForc = VenusForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append(VenusForc)


    # 地球
    earthWeight = 5.96*(10**24)
    dist1=14710 * (10 ** 7)
    force1 = G * (sunWeight * earthWeight) / (dist1 ** 2)
    force1=force1/1000/1000/10000/10000/10000
    F.append(force1)

    #  Mars
    MarsWeight = 6.4219 * (10 ** 23)
    MarsDist = 22800* (10 ** 7)
    MarsForc = G * (sunWeight * MarsWeight) / (MarsDist ** 2)
    MarsForc = MarsForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append(MarsForc)

    # Jupiter
    JupiterWeight = 1.8982 * (10 ** 27)
    JupiterDist = 77854.7200* (10 ** 7)
    JupiterForc = G * (sunWeight *  JupiterWeight) / ( JupiterDist ** 2)
    JupiterForc =  JupiterForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append( JupiterForc)

 #  Saturn
    SaturnWeight = 5.6 * (10 ** 26)
    SaturnDist = 14294 * (10 ** 8)
    SaturnForc = G * (sunWeight *  SaturnWeight) / ( SaturnDist ** 2)
    SaturnForc =  SaturnForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append( SaturnForc)



    #  Uranus
    UranusWeight = 8.681 * (10 ** 25)
    UranusDist = 28710 * (10 ** 8)
    UranusForc = G * (sunWeight *  UranusWeight) / ( UranusDist ** 2)
    UranusForc =  UranusForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append( UranusForc)


    #  Neptune
    NeptuneWeight = 1.0241 * (10 ** 26)
    NeptuneDist = 45040 * (10 ** 8)
    NeptuneForc = G * (sunWeight *  NeptuneWeight) / ( NeptuneDist ** 2)
    NeptuneForc =  NeptuneForc / 1000 / 1000 / 10000 / 10000 / 10000
    F.append( NeptuneForc)

    plt.bar(range(len(F)), F)
    plt.show()

if __name__ == '__main__':
    generate_F_r()

孩子能跳多远:

import matplotlib.pyplot as plt
import pylab
import math


def draw_graph(x, y):
    plt.plot(x, y)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('long jimp?')
    plt.axis(ymax=2)



def frange(start, final, increment):
    numbers = []
    while start < final:
        numbers.append(start)
        start = start + increment;
    return numbers;

def draw_trajectory (u, theta):
    theta=math.radians(theta)
    g=9.8
    t_flight=2*u*math.sin(theta)/g
    intervals=frange(0,t_flight,0.001)
    x=[]
    y=[]
    for t in intervals:
        x.append(u*math.cos(theta)*t)
        y.append(u*math.sin(theta)*t-0.5*g*t*t)
    draw_graph(x, y)

if __name__ == '__main__':
    draw_trajectory(5, 20)
    plt.show()

你在月亮上能跳多远:

import matplotlib.pyplot as plt
import pylab
import math


def draw_graph(x, y):
    plt.plot(x, y)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('long jimp?')
    plt.axis(ymax=2)



def frange(start, final, increment):
    numbers = []
    while start < final:
        numbers.append(start)
        start = start + increment;
    return numbers;

def draw_trajectory (u, theta,g):
    theta=math.radians(theta)
    # g=9.8
    t_flight=2*u*math.sin(theta)/g
    intervals=frange(0,t_flight,0.001)
    x=[]
    y=[]
    for t in intervals:
        x.append(u*math.cos(theta)*t)
        y.append(u*math.sin(theta)*t-0.5*g*t*t)
    draw_graph(x, y)

if __name__ == '__main__':
    draw_trajectory(5,20,9.8)
    draw_trajectory(5, 20, 1.63)
    plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值