Jump to content

[Python] quadratic formulea

Bsmith

Hi,

 

I am trying to create a simple code wich helps calculating quadratic formulea in python.

formulea that I mean:

y(x)=a*x^2(+/-1)b*x(+/-2)c

 

My problem is at the point where I have to define the meaning of (+/-1) and (+/-2), this is the point that i can't bypass.

It is used to define the random points on the x-axis to draw the graph.

 

how my code looks so far(the notes are in dutch, my own language, please don't mind them.) :unsure: translating them atm, might be helpfull

#berekenen kwadratische verbanden.#a= eerste getal in formule(met kwadraat).#b= tweede getal in de formule.#c= derde en laatste getal in de forumle.#d= berekening van a,b en c.#kwadraat is getal maal zichzelf.#formule wortel math.sqrt(x).#y(x)=x^2(+/-)x(+/-)x.#test forumle's, getal 1=-3 getal 2=2 getal 3=1, voor positieve disciminant#1=1 2=2 3=3 voor negatieve discriminant.import mathprint ("WARNING + and/or- belong to the numbers at b and c!")a=input("enter first valvue(with kwadrant):")b=input("enter second valvue:")c=input("enter last valvue:")d=(b*b)-4*a*cif d>0:    print  d ,("on x-axis and 0 y-axis")    print(" ")    print (-b+math.sqrt(d))/(2*a) ,("on x-axis and 0 on y-axis, x-top 1.")    print(" ")    print (-b-math.sqrt(d))/(2*a) ,("on x-axis and 0 y axis, x-top 2.")    print(" ")    #needs to be added to the other formulea.    print c ,("on y-axisen 0 on x-axis, x-top.")    print(" ")    print("calculate more pints on x-axis")    print("y(x)=a*x^2(+/-1)b*x(+/-2)c.")    print(" ")    x=input("enter x")    func1=raw_input("enter function +/-1 in")    func2=raw_input("enter function+/-2 in")    print ("x-axis is")(a*(x*x))(func1)(b*x)(func2)(c) ,("y-axis is") ,x#if discriminant=0.if d==0:    print("discriminant is.") , d    print(" ")    print(-b+math.sqrt(d))/(2*a) ,("nulpunt x-as en enigste x-top.")#if discriminant is negatif follow this.if d<0:     print ("dicriminant, without 0 points on x axis") ,d    print(" ")    print c ,("on y-axis; 0 on x-axis")

edit:

there will be more stuff I need to know(like drawing the graph) but that will come later, first need to handle the part of getting the formulea correct and working.

 

edit2:

friend suggested me to use raw_input(changed in code), it worked but now getting the error; "typeError:'str' object is not callable"

when removing the brackets form func1 and func2 i get a syntex error, so i'm a a little dead end( hopefully my friend gets a idea.)

May the light have your back and your ISO low.

Link to comment
Share on other sites

Link to post
Share on other sites

Just to let you know, it's quadratic.

I realize English is not your first language :)

Personal Rig v3: AMD Ryzen 7 2700X | Noctua NH-U14S | Gigabyte B450 Aorus Pro ITX | Zotac GTX 2070 8GB | 16GB G-Skill Trident DDR4 3200MHz | EVGA Supernova 750B | Phanteks Enthoo Evolv ITX 

Peripherals: Sennheiser HD518 & Classic ModMic | Corsair K65 Luxe | Zowie EC2 | ASUS VG259QM  |  ASUS VG278E | Klipsch ProMedia 2.1

 

Link to comment
Share on other sites

Link to post
Share on other sites

http://www.purplemath.com/modules/quadform.htm

Wouldn't the +- be determined from the user input values for a,b,c. So your formula would always use the + sign (y=ax^2+bx+c), check for the negative sign on input and handle it accordingly. I believe python will let you convert the string "-2" to the integer -2 without a fuss. So if the user inputs a=1, b=-2, c=3 the formula you would solve would be y= (1)*x^2 + (-2)*x + (3)

To plot the curve use a loop with known values of x, 1,2,3...10,11. Solve for y each time and you'll end up with a set of points, your X-value, and the Y-value you solve for. Then draw lines from each point to the next in order. The trick is defining the viewing window ahead of tine and solving for as many values of x,y as you deem necessary. Though I'm sure there's a library somewhere that can actually plot functions, just have to look for it.

Link to comment
Share on other sites

Link to post
Share on other sites

Just to let you know, it's quadratic.

I realize English is not your first language.

 

ahh thanks, will change it asap.

 

http://www.purplemath.com/modules/quadform.htm

Wouldn't the +- be determined from the user input values for a,b,c. So your formula would always use the + sign (y=ax^2+bx+c), check for the negative sign on input and handle it accordingly. I believe python will let you convert the string "-2" to the integer -2 without a fuss. So if the user inputs a=1, b=-2, c=3 the formula you would solve would be y= (1)*x^2 + (-2)*x + (3)

To plot the curve use a loop with known values of x, 1,2,3...10,11. Solve for y each time and you'll end up with a set of points, your X-value, and the Y-value you solve for. Then draw lines from each point to the next in order. The trick is defining the viewing window ahead of tine and solving for as many values of x,y as you deem necessary. Though I'm sure there's a library somewhere that can actually plot functions, just have to look for it.

 

I already tried removing the +- input, but that keeps me with the same problem, witht the typeError::'str' object is not callable.

 

Could you explain the curve drawing a bit better? since all I know is what I learned by trail-and-error.

May the light have your back and your ISO low.

Link to comment
Share on other sites

Link to post
Share on other sites

ahh thanks, will change it asap.

 

 

I already tried removing the +- input, but that keeps me with the same problem, witht the typeError::'str' object is not callable.

 

Could you explain the curve drawing a bit better? since all I know is what I learned by trail-and-error.

Syntax is all part of the game, I'm just here for general oversight and guidance, google the error or play around with it. You'll get it eventually

What I meant was substitute known values of X into your equation and solve for Y.

So if your equation is Y=x^2+(-2)*x+3; you would loop from -10 to +10 in increments you define, each time substituting in for new values of X. So your first loop would be y=(-10)^2+(-2)(-10)+3; y=100+20+3=123. So the point (-10,123) exists on the curve. If your viewing window was x/y min=-10 and x/y max = 10, that point would not be visible because the Y coordinate would place it outside the visible range. Then you just keep looping through solving for points on the line plotting the ones that fit in the viewing window. If you solve for enough points and plot them close enough you won't need to draw lines from point to point because the dots will be touching each other.

Anyways, this is just a basic method. I'm positive there is a far more elegant solution when it comes to plotting coords and or functions. Did your teacher give you any hints or restrictions on what you can/can't do?

Link to comment
Share on other sites

Link to post
Share on other sites

Syntax is all part of the game, I'm just here for general oversight and guidance, google the error or play around with it. You'll get it eventually

What I meant was substitute known values of X into your equation and solve for Y.

So if your equation is Y=x^2+(-2)*x+3; you would loop from -10 to +10 in increments you define, each time substituting in for new values of X. So your first loop would be y=(-10)^2+(-2)(-10)+3; y=100+20+3=123. So the point (-10,123) exists on the curve. If your viewing window was x/y min=-10 and x/y max = 10, that point would not be visible because the Y coordinate would place it outside the visible range. Then you just keep looping through solving for points on the line plotting the ones that fit in the viewing window. If you solve for enough points and plot them close enough you won't need to draw lines from point to point because the dots will be touching each other.

Anyways, this is just a basic method. I'm positive there is a far more elegant solution when it comes to plotting coords and or functions. Did your teacher give you any hints or restrictions on what you can/can't do?

 

hmm i see, i guess i will give it a try then.

My teacher actually doesn't know about this, since my i'm study labratorytechnology(direct translation) and we recently had this in lessons, i could understand it easily and got the theory, but the thing form python just slipped into my mind and i gave it a try because i love playing around in it. But I guess it is harder to turn into code then calculating it with a calculator and writing stuff down, but that doesn't make it less fun.

This is actually what we got in the lessons as theory to find a few more "random" points on the x-axis.

1. look at what coords you roughly are with x-top1 and x-top2

2. take 2-3 numbers in the middle of it.

3. put these in place of x, as example 2, wich means y(x)=x^2(+/-)x(+/-)x becomes y(2)=2^2(+/-)2(+/-)2, the outcome of that equals Y of that coord, with the X between the brackets being a little reminder of what x coord you took.

4. draw the graph and see if the symetrical line is equal to the y-top(error correction method)

May the light have your back and your ISO low.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×