AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Equation Solver\Root Finder 2.0 - up to four degrees
Goto page 1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Sun Mar 21, 2010 12:42 pm    Post subject: Equation Solver\Root Finder 2.0 - up to four degrees Reply with quote

DISCONTINUED

Last edited by XYZ on Thu Apr 29, 2010 7:43 am; edited 16 times in total
Back to top
View user's profile Send private message
maximo3491



Joined: 10 Feb 2007
Posts: 92

PostPosted: Mon Mar 22, 2010 6:05 pm    Post subject: Reply with quote

The thing about polynomials is that the highest power variable is telling you how many results exist, in both the real and imaginary number lines.

For quadratic equations, there are ALWAYS 2 answers (unless the 2 answers are identical). You are only giving real answers and failing to give imaginary solutions when and if they exist.
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Sat Mar 27, 2010 2:04 pm    Post subject: Reply with quote

in v1.5
its a global solver
Back to top
View user's profile Send private message
maximo3491



Joined: 10 Feb 2007
Posts: 92

PostPosted: Sun Mar 28, 2010 8:15 am    Post subject: Reply with quote

I updated this a little bit to include the complex results. Note that in the complex plane, the coordinate are a combination of real and imaginary numbers.

I made the complex answers have integers rounded to 2 decimal places so the whole result can be seen.

Code:
;Quadratic Equation Solver v1.0
;By Muhammad Umar

;########## Add GUI Controls #############################
Gui, Add, Picture, x6 y0 w417 h102 , %A_ScriptDir%\button.gif
Gui, Add, GroupBox, x6 y110 w175 h190 , User Input
Gui, Add, Text, x16 y135 w160 h20 , Enter the terms of the equation
Gui, Add, Text, x16 y160 w40 h20 , Enter a:
Gui, Add, Text, x16 y190 w40 h20 , Enter b:
Gui, Add, Text, x16 y220 w40 h20 , Enter c:
Gui, Add, Edit, gEquation va1 x66 y160 w80 h20 , +1
Gui, Add, Edit, gEquation vb1 x66 y190 w80 h20 , +5
Gui, Add, Edit, gEquation vc1 x66 y220 w80 h20 , +6
Gui, Add, Button, gSolve x16 y250 w150 h40, Solve!
Gui, Add, Button, gAbout x385 y115 w45 h185, About
Gui, Add, GroupBox, x6 y305 w423 h50 , Equation
Gui, Add, Text,x16 y325, Original Equation:
Gui, Add, Edit, vEq ReadOnly r1 x120 y320 w150 h40 ,1x^2+5x+6=0
Gui, Add, GroupBox, x190 y110 w190 h190 , Answer
Gui, Font, S12 CDefault
Gui, Add, Text, r1 x216 y150 w70 h40 , x=
Gui, Add, Edit, vAn1 r1 x245 y150 w100 h40 , ?
Gui, Add, Text, r1 x216 y190 w70 h40 , or
Gui, Add, Text, r1 x216 y230 w70 h40 , x=
Gui, Add, Edit, vAn2 r1 x245 y230 w100 h40 , ?

;########## Show GUI #####################################
Gui, Show, center h360 w435, Quadratic Equation Solver
Return

;########## GUI Close ####################################
GuiClose:
ExitApp

;########## Equation Solver Functions ####################
Solve:
gui, submit, nohide
dis := (b1**2-4*a1*c1)
imag := (dis < 0) ? "i" : ""

dis1 := Sqrt(Abs(dis))
if imag = i
{
   ansp1 := round((-b1)/(2*a1),2)
   ansp2 := round((dis1)/(2*a1),2)
   
   ans1 := (ansp1 = 0 ? "" : ansp1 "+") ansp2 "i"
   ans2 := (ansp1 = 0 ? "" : ansp1) "-" ansp2 "i"
}
else
{
   ans1 := round((-b1+dis1)/(2*a1),2)
   ans2 := round((-b1-dis1)/(2*a1),2)
}

GuiControl,, An1, %ans1%
GuiControl,, An2, %ans2%
if (ans1="" or ans2="") {
   MsgBox,, Error, Invalid user input.`nCould not complete the operation.
}
return

;########## About ########################################
About:
AText =
(
Quadratic Equation Solver
version 1.6

By Muhammad Umar

Comments and suggestions are welcome.

THANKS TO ALL THE COMMMUNITY MEMBERS AT AUTOHOTKEY FORUM!

Changelog:
1.1#  Initial release.
1.2#  GUI Improved. Removed Factorization Functions due to big error.
1.3#  Added Equation GroupBox that displays complete equation entered by user.
1.4#  Entering the terms of the equation automatically updates Original Equation EditBox.
1.5#  Added Abs(num) in solving command so Sqrt can work on positive integers. Any equation can now be solved.
1.6#  Made Original Equation EditBox Read-Only.

Future Realeases:
I hope to improved equation entry controls by restricting entry to numbers only.
)
MsgBox,,About, %Atext%
Return

;########## Original Equation Updator ####################
Equation:
gui, submit, nohide
result = %a1%x^2+%b1%x+%c1%=0
StringReplace, result, result, `+`+, `+, A
StringReplace, result, result, `+`-, `-, A
GuiControl,, Eq, %result%
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Sun Mar 28, 2010 12:11 pm    Post subject: Reply with quote

thx
it needed improvement
will upload v1.7 with your changes later
Back to top
View user's profile Send private message
maximo3491



Joined: 10 Feb 2007
Posts: 92

PostPosted: Sun Mar 28, 2010 11:10 pm    Post subject: Reply with quote

Here is another addition

I added the up-down gui control as well as auto solve whenever you change the values. This removes the need of a solve button. Also, when the "a" term is 0, the equation becomes linear and still requires a solution. I added the functionality too. Check it out.

Code:
;Quadratic Equation Solver v1.0
;By Muhammad Umar

#singleinstance, force

;########## Add GUI Controls #############################
Gui, Add, Picture, x6 y0 w417 h102 , %A_ScriptDir%\button.gif
Gui, Add, GroupBox, x6 y110 w175 h140 , User Input
Gui, Add, Text, x16 y135 w160 h20 , Enter the terms of the equation
Gui, Add, Text, x16 y160 w40 h20 , Enter a:
Gui, Add, Text, x16 y190 w40 h20 , Enter b:
Gui, Add, Text, x16 y220 w40 h20 , Enter c:
Gui, Add, Edit, x66 y160 w80 h20,
Gui, Add, UpDown, gEquation va1 Range-100-100, 0
Gui, Add, Edit, gEquation x66 y190 w80 h20,
Gui, Add, UpDown, gEquation vb1 Range-100-100, 0
Gui, Add, Edit, gEquation x66 y220 w80 h20,
Gui, Add, UpDown, gEquation vc1 Range-100-100, 0
Gui, Add, Button, gAbout x385 y115 w45 h135, About
Gui, Add, GroupBox, x6 y255 w423 h50 , Equation
Gui, Add, Text,x16 y275, Original Equation:
Gui, Add, Edit, vEq ReadOnly r1 x120 y27320 w150 h40
Gui, Add, GroupBox, x190 y110 w190 h140 , Answer
Gui, Font, S12 CDefault
Gui, Add, Text, vAn1X r1 x216 y130 w70 h40 , x=
Gui, Add, Edit, vAn1 r1 x245 y130 w100 h40 , ?
Gui, Add, Text, vAnOr r1 x216 y170 w70 h40 , or
Gui, Add, Text, vAn2X r1 x216 y210 w70 h40 , x=
Gui, Add, Edit, vAn2 r1 x245 y210 w100 h40 , ?

gosub, solve

;########## Show GUI #####################################
Gui, Show, center h310 w435, Quadratic Equation Solver
Return

;########## GUI Close ####################################
GuiClose:
ExitApp

;########## Equation Solver Functions ####################
Solve:
gui, submit, nohide
dis := (b1**2-4*a1*c1)
imag := (dis < 0) ? "i" : ""

dis1 := Sqrt(Abs(dis))
if a1 != 0
{
   if imag = i
   {
      ansp1 := round((-b1)/(2*a1),2)
      ansp2 := round((dis1)/(2*a1),2)

      ans1 := (ansp1 = 0 ? "" : ansp1 "+") ansp2 "i"
      ans2 := (ansp1 = 0 ? "" : ansp1) "-" ansp2 "i"
   }
   else
   {
      
      ans1 := round((-b1+dis1)/(2*a1),2)
      ans2 := round((-b1-dis1)/(2*a1),2)
   }
}
else if b1 != 0
{
   ans1 := round(-c1/b1,2)
}

GuiControl,, An1, % FixSigns(ans1)
GuiControl,, An2, % FixSigns(ans2)

if (ans1="" or ans2="") {
   ;MsgBox,, Error, Invalid user input.`nCould not complete the operation.
}
return

;########## About ########################################
About:
AText =
(
Quadratic Equation Solver
version 1.6

By Muhammad Umar

Comments and suggestions are welcome.

THANKS TO ALL THE COMMMUNITY MEMBERS AT AUTOHOTKEY FORUM!

Changelog:
1.1#  Initial release.
1.2#  GUI Improved. Removed Factorization Functions due to big error.
1.3#  Added Equation GroupBox that displays complete equation entered by user.
1.4#  Entering the terms of the equation automatically updates Original Equation EditBox.
1.5#  Added Abs(num) in solving command so Sqrt can work on positive integers. Any equation can now be solved.
1.6#  Made Original Equation EditBox Read-Only.

Future Realeases:
I hope to improved equation entry controls by restricting entry to numbers only.
)
MsgBox,,About, %Atext%
Return

;########## Original Equation Updator ####################
Equation:
gui, submit, nohide
result := (a1 != 0 ? a1 "x^2+" : "") (b1 != 0 ? b1 "x+" : "") (c1 != 0 ? c1 : "")
if result !=
{
   result := result "=0"
      
   if a1 = 0
   {
      guicontrol, hide, AnOr
      guicontrol, hide, An2X
      guicontrol, hide, An2
   
      if b1 = 0
      {
         guicontrol, hide, An1X
         guicontrol, hide, An1
      }
      else
      {
         guicontrol, show, An1X
         guicontrol, show, An1
      }
   }
   else
   {
      guicontrol, show, An1X
      guicontrol, show, An1
      guicontrol, show, AnOr
      guicontrol, show, An2X
      guicontrol, show, An2
   }
}
else
{
   guicontrol, show, An1X
   guicontrol, show, An1
   guicontrol, show, AnOr
   guicontrol, show, An2X
   guicontrol, show, An2
}
   
StringReplace, result, result, `+`+, `+, A
StringReplace, result, result, `+`-, `-, A
StringReplace, result, result, `+`=, `=, A
StringReplace, result, result, 1x, x, A
GuiControl,, Eq, %result%
gosub, Solve
return

FixSigns(in)
{
   StringReplace, in, in, `-`-, `+, A
   StringReplace, in, in, `+`+, `+, A
   StringReplace, in, in, `-`+, `-, A
   StringReplace, in, in, `+`-, `-, A
   
   return in
}
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Mon Mar 29, 2010 5:54 am    Post subject: Reply with quote

excellent work, maximo
i was also thinking of auto solving
actually i am working on another unlaunched project and paying little attention to this
but thx so much for your additions

i will have to make a large update to it now
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Mon Mar 29, 2010 10:42 am    Post subject: Reply with quote

i edited the post and updated it to v1.8

from version 2 i hope to renew the GUI Idea

and use 2 decimal placed versions like 1.81 Laughing
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Tue Mar 30, 2010 6:03 am    Post subject: Reply with quote

Code:

ans2 := (ansp1 = 0 ? "" : ansp1) "-" ansp2 "i"


i dont understand this

is it the ternary operator??
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Tue Mar 30, 2010 4:28 pm    Post subject: Reply with quote

UMAR wrote:
is it the ternary operator??

Why didn't you search?
1) Manual
2) [VxE]'s guide
_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
maximo3491



Joined: 10 Feb 2007
Posts: 92

PostPosted: Tue Mar 30, 2010 9:31 pm    Post subject: Reply with quote

Yes it is,

var := (condition ? "true" : "false")

If the condition is true, then var would be "true" otherwise it would be "false"
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Wed Mar 31, 2010 8:43 am    Post subject: Reply with quote

this project is a hit!
550+ views already!

its good in hw help Razz Razz
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Thu Apr 01, 2010 2:53 am    Post subject: Reply with quote

can i add this addition to restrict input to numbers only?

Code:

gui,submit,nohide
loop,parse,edit
{
 if a_loopfield not in 1,2,3,4,5,6,7,8,9,0,+,-
 stringreplace,edit,edit,%a_loopfield%,,all
}
guicontrol,,edit,%edit%
send,{end}
return


this is supposed to be in the gSubroutine for all the updown edits namely a1,b1,c1
Back to top
View user's profile Send private message
maximo3491



Joined: 10 Feb 2007
Posts: 92

PostPosted: Thu Apr 01, 2010 7:22 am    Post subject: Reply with quote

I would add this to the top of the Equation subroutine right after the gui, submit, no hide

Also, do not change '%var%' to 'val' in the initiation of the second loop. This ensures that the replace function doesn't change the value the loop is parsing midway through the actual parse. I'm not sure how AutoHotKey handles this, but this could usually create problems or inaccurate results.

Code:
vars := "a1,b1,c1"
loop, parse, vars, `,
{
   var := a_loopfield
   val := %var%
   
   first := SubStr(val, 1, 1)
   if first not in +,-
      first := ""
   
   loop,parse,%var%
   {
      if a_loopfield not in 1,2,3,4,5,6,7,8,9,0
      {
         stringreplace,val,val,%a_loopfield%,,all
      }
   }

   guicontrol,,%var%,% first val
}
Back to top
View user's profile Send private message
XYZ



Joined: 20 Mar 2010
Posts: 224

PostPosted: Thu Apr 01, 2010 5:28 pm    Post subject: Reply with quote

the script i wrote is without reference to correct vars in the real script

of course when i add it i will change and test it


Last edited by XYZ on Fri Apr 02, 2010 1:43 pm; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group