Jump to content

Need help with script


Ultima2
 Share

Recommended Posts

Im trying to write a script that will solve a quadratic polynomial by using the bisection method. Here's how far I got

#include <GUIConstants.au3>

GUICreate("test",500,350)
$exitbutton = GUICtrlCreateButton("Exit", 30, 200)
$Avalue = GUICtrlCreateInput("",10,50,30,30)
$Bvalue = GUICtrlCreateInput("", 70, 50, 30, 30)
$Cvalue = GUICtrlCreateInput("", 130, 50, 30, 30)
$okbutton = GUICtrlCreateButton("ok", 2, 200)
$negative = GUICtrlCreateInput("", 30, 130, 40, 22)
$posistive = GUICtrlCreateInput("", 210, 130, 40, 22)
GUICtrlCreateLabel("Positive", 205, 106)
GUICtrlCreateLabel("Negative", 25, 106)
GUICtrlCreateLabel("^2",44, 50)
GUICtrlCreateLabel("X",40, 68)
GUICtrlCreateLabel("+",57, 54)
GUICtrlCreateLabel("X", 100, 68)
GUICtrlCreateLabel("+", 115, 54)
GUICtrlCreateLabel("Put in the form of ax^2 +bx +c", 10, 20)
GUISetState()
Do
    $msg = GUIgetmsg ()
    Select
    Case $msg = $okbutton
        Call ("math")
    Case $msg = $GUI_EVENT_CLOSE or $msg = $exitbutton
        Exit
    EndSelect
Until $msg = $GUI_EVENT_CLOSE or $msg = $exitbutton

Func math()
    $aVal = GUICtrlread($Avalue)
    $bVal = GUICtrlRead($Bvalue)
    $cVal = GUICtrlRead($Cvalue)
    $neg = GUICtrlread($negative)
    $pos = GUICtrlread($posistive)
    $x1 = ($neg + $pos)/2
    $test1 = ($aVal)*($x1)^2 + ($bVal)*($x1) + $cVal
    If $test1 < 0 Then
        $x2 = ($pos + $x1)/2
    ElseIf $test >0 Then
        $x2 = ($neg + $x1)/2
    EndIf
    $test2 = ($aVal)*($x2)^2 + ($bVal)*($x2) + $cVal
    If $test2 < 0 Then
        If $test1 > 0 Then
            $x3 = ($x2 + $x1)/2
        ElseIf $test1 < 0 Then
            $x3 = ($x2 + $pos)/2
        EndIf
    ElseIf $test2 > 0 Then
        If $test 1 < 0 Then
            $x3 = ($x2 + $x1)/2
        ElseIf $test1 > 0 Then
            $x3 = ($x2 + $neg)/2
        EndIf
    EndIf
    $test3 = ($aVal)*($x3)^2 + ($bVal)*($x3) + $cVal
    If $test3 < 0 Then
        If $test2 > 0 Then
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 < 0 Then
            If $test1 > 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 < 0 Then
                $x4 = ($x3 + $pos)/2
            EndIf
        EndIF
    ElseIf $test3 > 0 Then
        If $test2 < 0 Then 
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 > 0 Then
            If $test1 < 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 > 0 Then
                $x4 = ($x3 + $neg)/2
            EndIf
        EndIf
    EndIf
                
MsgBox(0,"The fourth iteration is...", $x4)
EndFunc

This works for the third iteration. But when I tried the fourth iteration, I keep getting the error "variable used without being declared". Can someone help me? I checked but Im sure I declared the variable $x4 in my If's statements. It works for the $x3 variable though, what's wrong with my script so far?

Here's the part of the script that I think I messed up on, but I cant find the problem

If $test3 < 0 Then
        If $test2 > 0 Then
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 < 0 Then
            If $test1 > 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 < 0 Then
                $x4 = ($x3 + $pos)/2
            EndIf
        EndIF
    ElseIf $test3 > 0 Then
        If $test2 < 0 Then 
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 > 0 Then
            If $test1 < 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 > 0 Then
                $x4 = ($x3 + $neg)/2
            EndIf
        EndIf
    EndIf
Edited by Ultima2
Link to comment
Share on other sites

actually there were 2 problems.

i commented where i fixed them.

#include <GUIConstants.au3>

GUICreate("test",500,350)
$exitbutton = GUICtrlCreateButton("Exit", 30, 200)
$Avalue = GUICtrlCreateInput("",10,50,30,30)
$Bvalue = GUICtrlCreateInput("", 70, 50, 30, 30)
$Cvalue = GUICtrlCreateInput("", 130, 50, 30, 30)
$okbutton = GUICtrlCreateButton("ok", 2, 200)
$negative = GUICtrlCreateInput("", 30, 130, 40, 22)
$posistive = GUICtrlCreateInput("", 210, 130, 40, 22)
GUICtrlCreateLabel("Positive", 205, 106)
GUICtrlCreateLabel("Negative", 25, 106)
GUICtrlCreateLabel("^2",44, 50)
GUICtrlCreateLabel("X",40, 68)
GUICtrlCreateLabel("+",57, 54)
GUICtrlCreateLabel("X", 100, 68)
GUICtrlCreateLabel("+", 115, 54)
GUICtrlCreateLabel("Put in the form of ax^2 +bx +c", 10, 20)
GUISetState()
Do
    $msg = GUIgetmsg ()
    Select
    Case $msg = $okbutton
        Call ("math")
    Case $msg = $GUI_EVENT_CLOSE or $msg = $exitbutton
        Exit
    EndSelect
Until $msg = $GUI_EVENT_CLOSE or $msg = $exitbutton

Func math()
    $aVal = GUICtrlread($Avalue)
    $bVal = GUICtrlRead($Bvalue)
    $cVal = GUICtrlRead($Cvalue)
    $neg = GUICtrlread($negative)
    $pos = GUICtrlread($posistive)
    $x1 = ($neg + $pos)/2
    $test1 = ($aVal)*($x1)^2 + ($bVal)*($x1) + $cVal
    If $test1 < 0 Then
        $x2 = ($pos + $x1)/2
    ElseIf $test1 >0 Then;ElseIf $test >0 Then
        $x2 = ($neg + $x1)/2
    EndIf
    $test2 = ($aVal)*($x2)^2 + ($bVal)*($x2) + $cVal
    If $test2 < 0 Then
        If $test1 > 0 Then
            $x3 = ($x2 + $x1)/2
        ElseIf $test1 < 0 Then
            $x3 = ($x2 + $pos)/2
        EndIf
    ElseIf $test2 > 0 Then
        If $test1 < 0 Then;If $test 1 < 0 Then
            $x3 = ($x2 + $x1)/2
        ElseIf $test1 > 0 Then
            $x3 = ($x2 + $neg)/2
        EndIf
    EndIf
    $test3 = ($aVal)*($x3)^2 + ($bVal)*($x3) + $cVal
    If $test3 < 0 Then
        If $test2 > 0 Then
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 < 0 Then
            If $test1 > 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 < 0 Then
                $x4 = ($x3 + $pos)/2
            EndIf
        EndIF
    ElseIf $test3 > 0 Then
        If $test2 < 0 Then 
            $x4 = ($x3 + $x2)/2
        ElseIf $test2 > 0 Then
            If $test1 < 0 Then
                $x4 = ($x3 + $x1)/2
            ElseIf $test1 > 0 Then
                $x4 = ($x3 + $neg)/2
            EndIf
        EndIf
    EndIf
                
MsgBox(0,"The fourth iteration is...", $x4)
EndFunc
Link to comment
Share on other sites

Oh I know why the variables weren't declared. The quadratic I was putting had rational roots that was solved during the first or second iteration :) . The script works when I put in polynomial that has irrational roots like 2.5x^2 -7.2x +1.3 :) . Thanks for your help though o:)

Edited by Ultima2
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...