10 Replies - 425 Views - Last Post: 29 June 2012 - 10:00 AM Rate Topic: -----

#1 arengorn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 29-September 11

Program to solve 2º grade equations.

Posted 29 June 2012 - 08:35 AM

hey im developing a program to solve 2ºgrade equations (X²+2x+4 for example) by using the root formule,here's the code.
Public Class Form1
    Dim i1 As Single
    Dim i2 As Single
    Dim i3 As Single
    Dim delta As Single
    Dim x1 As Single
    Dim x2 As Single

    Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label5_Click(sender As System.Object, e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles T1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles T2.TextChanged

    End Sub

    Private Sub TextBox3_TextChanged(sender As System.Object, e As System.EventArgs) Handles T3.TextChanged

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        i1 = T1.Text
        i2 = T2.Text
        i3 = T3.Text
        delta = (i2 * i2) - 4 * (i1 * i3)
        x1 = -i2 + delta
        x2 = -i2 - delta
        Ld.Text = delta
        L1.Text = x1
        L2.Text = x2
    End Sub
End Class


ill attach a photo of the ui.
the problem is that for exemple,in a simple equation like X²+6X+9 the use inputs 1 in a 6 in b and 9 in c and the result should be X=3 or X=-3 but the program says X=-6 or X=-6.
any idea why its doing the maths wrong?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Program to solve 2º grade equations.

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 08:42 AM

Take a look at the equation again -

Its -i2 +- square root(delta) / (2 * i1)

Missing the sqrt bit and the division.

You should also check the sign of the discriminant (delta in this case) to make sure that there are two/one/no roots. Otherwise you will get an exception when you try square rooting a negative number.

This post has been edited by Ryano121: 29 June 2012 - 08:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 arengorn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 29-September 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 08:46 AM

View PostRyano121, on 29 June 2012 - 08:42 AM, said:

Take a look at the equation again -

Its -i2 +- square root(delta) / (2 * i1)

Missing the sqrt bit and the division.

You should also check the sign of the discriminant (delta in this case) to make sure that there are two/one/no roots. Otherwise you will get an exception when you try square rooting a negative number.

ok i rewrote the code
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        i1 = T1.Text
        i2 = T2.Text
        i3 = T3.Text
        delta = (i2 * i2) - 4 * (i1 * i3)
        x1 = (-i2 + Math.Sqrt(delta)) / 2 * i2
        x2 = (-i2 - Math.Sqrt(delta)) / 2 * i2
        Ld.Text = delta
        L1.Text = x1
        L2.Text = x2
    End Sub
End Class

but its still not working now x = -18 or x=-18
(with the same numbers as before:1,6 and 9)
when it should be 3 or -3.

This post has been edited by arengorn: 29 June 2012 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 08:56 AM

You divide by 2*a not 2*b

Oh and by the way x^2 + 6x + 9 has only one root at x = -3 :)
Was This Post Helpful? 1
  • +
  • -

#5 arengorn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 29-September 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 08:57 AM

ok but now its returning -3 or -3.
  i1 = T1.Text
        i2 = T2.Text
        i3 = T3.Text
        delta = (i2 * i2) - 4 * (i1 * i3)
        x1 = (-i2 + Math.Sqrt(delta)) / 2 * i1
        x2 = (-i2 - Math.Sqrt(delta)) / 2 * i1
        Ld.Text = delta
        L1.Text = x1
        L2.Text = x2
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 09:01 AM

Which is correct.
Was This Post Helpful? 0
  • +
  • -

#7 arengorn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 29-September 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 09:02 AM

View PostRyano121, on 29 June 2012 - 09:01 AM, said:

Which is correct.

no the correct would be 3 or -3 not -3 or -3
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 09:05 AM

Afraid not

x^2+6x+9 = 0

Has just the one solution at x = -3
Was This Post Helpful? 1
  • +
  • -

#9 arengorn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 29-September 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 09:07 AM

no your right -3 or -3 is correct
thank you very much :)
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 09:07 AM

If you want solutions at -3 and +3, then the equation would be

x^2 - 9

Where a = 1, b = 0, c = -9 :)
Was This Post Helpful? 1
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Program to solve 2º grade equations.

Posted 29 June 2012 - 10:00 AM

For future reference, this is VB.NET, NOT VB6.

Moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1