【问题标题】:Quadratic formula find value for x1 and x2 given an equation给定方程的二次公式求 x1 和 x2 的值
【发布时间】:2016-02-09 16:26:30
【问题描述】:

给定一个包含系数值的嵌套列表l,我正在尝试计算二次公式以找到 x 的零点,表示为 x1,x2。我有一个 for 循环遍历这个列表,并从嵌套列表中为我提供 a、b 和 c 的值:

import math as m
l = [[1,2,1],[9,12,4],[1,-7,0],[1,2,-3]]#nested list
for x in l:
  q = x[1]*x[1]-4*x[0]*x[2] #b*b - 4*a*c
  q_sr = m.sqrt(q)#root of q
  x1 = (-x[1] + q_sr)/(2*x[0])#[1]=b and [0]=a
  x2 = (-x[1] - q_sr)/(2*x[0])#[1]=b and [0]=a
  eq = x[0]**2 + 2*x[1] + 1*x[2] #equation that im trying to get the x1 and x2

  print("a verdier: ", x[0])
  print("b verdier: ", x[1])
  print("c verdier: ", x[2])
  print("x1 verdier: ", x1)
  print("x2 verdier: ", x2) 

这里,x[0],x[1] 和 x[2] 是列表 l 中的对应位置,例如,0 = a, 1=b 和 2=c。这一切都有效,我得到了 x1 和 x2 的正确值。

我在计算零时遇到问题 (x1, x2)。我如何计算这些值?

【问题讨论】:

标签: python formula equation quadratic


【解决方案1】:

复杂的数学模块非常适合这样的事情。

import cmath
def quadratic(a, b, c):
    d = float(b**2 - 4*a*c)
    x1 = ((-b)-cmath.sqrt(d))/(2*a)
    x2 = ((-b)+cmath.sqrt(d))/(2*a)
    return [x.real if (x.imag == 0.0) else x for x in [x1, x2]]

为了好玩

class Quadratic:
    def __init__(self, a, b, c):
        self.a, self.b, self.c = a, b, c
        self.d = float(self.b ** 2 - 4*self.a*self.c)
        self.x1 = ((-b)-cmath.sqrt(self.d))/(2*a)
        self.x2 = ((-b)+cmath.sqrt(self.d))/(2*a)

    @property
    def solution(self):
        return [x.real if x.imag == 0.0 else x for x in [self.x1, self.x2]]

    def __str__(self):
        return "X1 = {}, X2 = {}".format(*self.solution)


myList = [[1, 2, 1], [9, 12, 4], [1, -7, 0], [1, 2, -3]]
for _ in myList:
    print Quadratic(*_)

【讨论】:

    【解决方案2】:

    这是您的代码的修改和注释版本,应该可以帮助您理解您所做的事情。

    from math import sqrt
    
    coef_list = [[1,2,1],[9,12,4],[1,-7,0],[1,2,-3]]
    
    # This following "for loop" will compute solutions x1 and x2 
    # for any quadratic equation summarized in your coef_list. In your
    # coef_list you have the following equations:
    # y(x) = 1*x^2 + 2*x + 1
    # y(x) = 9*x^2 + 12*x + 4
    # ...
    # y(x) = 1*x^2 + 2*x -3
    
    for coef in coef_list:
        a, b, c = coef   # extract a, b and c from the inner lists
        q = b**2 - 4*a*c
    
        # In case q > 0 you have two solutions
        if q > 0:
            q_sqrt = sqrt(q)
            x1 = (-b + q_sqrt)/(2*a)#[1]=b and [0]=a
            x2 = (-b - q_sqrt)/(2*a)#[1]=b and [0]=a
    
        # In case q = 0 you have only one solution
        elif q == 0:
            x1 = -b/(2*a)
            x2 = x1
    
        # In case q < 0 you have no real solution
        else:
            raise ValueError("q is negative.")
    
        # print at all iteration of the loop to have solutions for every
        # equation in given in coef_list
        print "x1 = ", x1
        print "x2 = ", x2
        print "a = ", a, ", b = ", b, "and c = ",c
        print "-----"
    
    
    # You don't need the next line since the equation you are trying to solve is 
    # is defined in coef_list at line 0 (i.e. coef_list[0])     
    
    #eq = x[0]**2 + 2*x[1] + 1*x[2] #equation that im trying to get the x1 and x2
    

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2018-09-24
      相关资源
      最近更新 更多