Sign in to follow this  
Followers 0
Ultima2

Need help with script

6 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



Yes, you did declare it, but obviously its skipping the "If" statements because it does not meet the requirements to go into the If statements, so, the variable goes undeclared.

Share this post


Link to post
Share on other sites

there error is here.

ElseIf $test >0 Then

i think the $test variable should have a number in the name, like $test1 or whatever one your trying to check.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Im still having problem with it not declaring my $x4 variable. Should I use Select...Case instead of IF's then?

Edited by Ultima2

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


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
Sign in to follow this  
Followers 0