第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 matplotlib 中用輸入繪制拋物線

在 matplotlib 中用輸入繪制拋物線

幕布斯7119047 2022-07-12 16:25:03
所以我在python中創(chuàng)建了一個(gè)二次方程求解器,我想要一個(gè)圖表來配合它。我希望這張圖描繪拋物線。a = 二次方程中 a 的輸入b = 二次方程中 b 的輸入c = 二次方程中 c 的輸入y = -b/2*a (向量)import matha = int(input('Input a: '))b = int(input('Input b: '))c = int(input('Input c: '))discrim = int((b*b) - 4*a*c) posi = (-b + (math.sqrt(discrim)))/(2*a)neg = (-b - (math.sqrt(discrim)))/(2*a)if b > 0 and c > 0 and posi < 0:  posi = -posi  if neg < 0:    neg = -negy = -b/2*avector = (neg+posi)/2, y)使用這段代碼,我有我的 x 截距和足以創(chuàng)建拋物線的向量,我只是不確定如何使用 MatPlotLib 將這些值轉(zhuǎn)換為圖形。請(qǐng)?zhí)岢鋈魏螁栴}。
查看完整描述

1 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

我在你的代碼中做了一些更正。下面的代碼將繪制任何拋物線。您可以在繪圖中添加零點(diǎn)和極值點(diǎn)。


import matplotlib.pyplot as plt

import math

import numpy as np


a = int(input('Input a: '))

b = int(input('Input b: '))

c = int(input('Input c: '))


# calculate delta and zero points

delta = b**2 - 4*a*c

if delta > 0:

    x_1 = (-b + math.sqrt(delta))/(2*a)

    x_2 = (-b - math.sqrt(delta))/(2*a)

if delta == 0:

    x_0 = -b/(2*a)

else:

    pass


# calculate parabola extreme coordinates

p = -b/(2*a)

q = -delta/(4*a)

extreme = [p,q]


# define parabola function

def parabola(x,a,b,c):

    y = a*x**2 + b*x + c

    return y


# plot function

x = np.linspace(int(p)-5,int(p)+5,100)

y = parabola(x,a,b,c)

plt.plot(x,y)

plt.axhline(y=0, color='black', linestyle='-')

plt.axvline(x=0, color='black', linestyle='-')

plt.text(p-0.5, q-3, '[' + str(round(p,2)) +',' + str(round(q,2)) + ']',color='orange', fontsize=9)

plt.plot(p, q, marker="o")


if delta > 0:

    plt.plot(x_1, 0, marker="o", color='green')

    plt.text(x_1 - 0.5, 2, '[' + str(round(x_1,2)) + ']', color='green', fontsize=9)

    plt.plot(x_2, 0, marker="o", color='green')

    plt.text(x_2 - 0.5, 2, '[' + str(round(x_2,2)) + ']', color='green', fontsize=9)


if delta == 0:

    plt.plot(x_0, 0, marker="o", color='green')

    plt.text(x_0 - 0.5, 2, '[' + str(round(x_0,2)) + ']', color='green', fontsize=9)


plt.show()

樣本結(jié)果:

https://i.stack.imgur.com/bNprP.png


查看完整回答
反對(duì) 回復(fù) 2022-07-12
  • 1 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)