LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2011, 11:44 AM   #1
JAPANBOY
LQ Newbie
 
Registered: Jun 2011
Location: somewhere over the rainbow
Posts: 11

Rep: Reputation: Disabled
quadratic equation for python NEW


few days ago i posted something about the quadratic equation on how to write it in python and i wrote something like this

A=int(input("enter a number for A :"))
B=int(input("enter a number for B:"))
C=int(input("enter a number for C:"))

then i wrote

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

so the question im asking is how do i make python know that im making X a variable because i get this

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
QE=a*x**2+b*x+c
NameError: name 'x' is not definedTraceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
QE=a*x**2+b*x+c
NameError: name 'x' is not defined


thank you !!!
 
Old 06-26-2011, 12:18 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
NameError: name 'x' is not defined
What happens when you define 'x'? I think it would correctly assign the result of the expression to the variable 'quadratic_equation'.

--- rod.
 
Old 06-26-2011, 02:40 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
If you want to solve quadratic equation with given coefficients (numbers) then you should find (calculate) numbers x such that a*x**2+b*x+c == 0. That means you should define a function which performs such calculation, e.g.:
Code:
from cmath import sqrt  # numerical math functions in complex domain

# solves equation
# a*x^2 + b*x + c = 0
# returns: [D, x1, x2],
# where D (discriminant) could be:
#       positive - two different real roots;
#       0 - x1=x2, real;
#       negative - both roots are complex.    
def solve_quadratic(a,b,c):
        D = b**2 - 4*a*c
        x1 = (-b + sqrt(D) )/2.0/a
        x2 = (-b - sqrt(D) )/2.0/a
        return [D, x1, x2]

# test
print solve_quadratic(1,4,1)
If you want to save a quadratic expression to a variable symbolically then you should probably look at `sympy', `sage', `maxima' (not in python) or similar software for symbolic mathematics.

Finally, if you want to be able to compute quadratic expression a*x**2+b*x+c with different values of `x' you may define a function, for example:

Code:
# values of a,b and c are global
quadratic_equation = lambda(x): a*x**2+b*x+c
# evaluate at x=5
print quadratic_equation(5)
 
Old 06-26-2011, 11:59 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I think that what you want is to make quadratic_equation a function with variable x, something like:
Code:
def quadratic_equation(x) return a*x**2+b*x+c
where a, b and c are globals and you pass in to quadratic_equation the value of x that you are interested in:
Code:
<<<quadratic_equation(0)
<<<quadratic_equation(1)
<<<quadratic_equation(2)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fomula for coefficient for quadratic equation JAPANBOY Programming 4 06-25-2011 01:21 PM
Alpha equation Kenny_Strawn Programming 1 03-10-2010 10:59 PM
elementary python algebra equation solver x hellork Programming 0 12-19-2007 09:13 AM
Transparency Equation leonidg Programming 3 02-08-2005 02:38 AM
an equation in c language liquid sky Programming 5 03-22-2004 02:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration