It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Help with program that calculates roots of equations

Moderator: dafunkymunky

Help with program that calculates roots of equations

Postby Scout1 on Sat Nov 13, 2010 6:52 pm

I'm trying to write a program in Microsoft Visual Basic 2010 to solve for the roots of the equation using the bisection method. The equation I am writing the program for is: x^5 - 3x^2 + 4 =0. I have designed the program so the user only has to input a range of x values (x1 and x2) to solve for the root. I am having difficulty with the code inside the loop. When I run the program keeps coming up with a 0 root. I am new to programming and Visual Basic. Here is my code

Code: Select all
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x1 As Double
        Dim x2 As Double
        Dim xmid As Double
        Dim fx1 As Double
        Dim fx2 As Double
        Dim fxmid As Double
        Dim root As Double

        x1 = Val(TextBox1.Text)
        x2 = Val(TextBox2.Text)

        xmid = (x1 + x2) / 2
        fx1 = (x1 ^ 5) - (3 * (x1 ^ 2)) + 4
        fx2 = (x2 ^ 5) - (3 * (x2 ^ 2)) + 4
        fxmid = (xmid ^ 5) - (3 * (xmid ^ 2)) + 4

        If fx1 * fx2 > 0 Then
            MsgBox("Imput Different Range")

        End If

        If fx1 * fx2 < 0 Then
        End If
        Do
            If fx1 * fxmid < 0 Then
                x2 = xmid

            ElseIf fx1 * fxmid > 0 Then
                x1 = xmid

            ElseIf (fx1) * (fxmid) = 0 Then
                xmid = root
            End If
            Exit Do

        Loop Until Math.Abs(xmid - x1) < 0.00001

        TextBox6.Text = Str(root)

    End Sub



I am not sure what I am doing wrong. Any help would be greatly appreciated.
Scout1
 
Posts: 1
Joined: Sat Nov 13, 2010 5:23 pm

Who is online

Users browsing this forum: No registered users and 3 guests