Eigensheep Posted December 23, 2008 Share Posted December 23, 2008 It reallly only works for linear equations though. You start by entering how many variables you have and then enter the equations into the boxes. When you click solve it gives the answers to all the unknowns to a maximum of 3dp [you could change this - I used round($ans,3)] It's most useful if you have more than 2 variables. expandcollapse popup#include <GUIConstantsEx.au3> Global $num_unknowns = Number(InputBox("Enter number of unknowns","How many unknowns are there? [NB. You need as many equations as there are unknowns]",3)) Dim $coeff[$num_unknowns+1][$num_unknowns+2] Dim $values[$num_unknowns+1][$num_unknowns+2] GUICreate("Enter Equations:",(48*$num_unknowns) + 121,(26*$num_unknowns) + 77) For $n = 1 To $num_unknowns For $r = 1 To $num_unknowns $values[$n][$r] = GUICtrlCreateInput("",48 * $r, 26 * $n, 25, 21) GUICtrlCreateLabel(StringMid("abcdefghijklmnopqrstuvwxyz",$r,1), (48 * $r)+26, (26 * $n)+5, 10, 17) If $r < $num_unknowns Then GUICtrlCreateLabel("+", (48 * $r)+37, (26 * $n)+5, 10, 17) Else GUICtrlCreateLabel("=", (48 * $r)+37, (26 * $n)+5, 10, 17) EndIf Next $values[$n][$r] = GUICtrlCreateInput("",48 * $r, 26 * $n, 25, 21) Next $solve_button = GUICtrlCreateButton("Solve",(48*$num_unknowns) + 38,(26*$num_unknowns) + 36, 50, 25) GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $solve_button For $n = 1 To $num_unknowns For $r = 1 To $num_unknowns+1 $coeff[$n][$r] = Number(GUICtrlRead($values[$n][$r])) Next Next GUISetState(@SW_HIDE) $sols = _Solve($num_unknowns,$coeff) $solutWin = GUICreate("Solutions:", 220, (21 * $sols[0])+ 42) For $n = 1 To $sols[0] ;You can change the round() in the next line: GUICtrlCreateLabel(StringMid("abcdefghijklmnopqrstuvwxyz",$n,1) & " = " & Round($sols[$n],3) & " (Max. 3dp)", 50, 21 * $n, 200, 20) Next GUISetState(@SW_SHOW,$solutWin) EndSwitch Sleep(100) WEnd ; ====================================================================== ; Name...........: _Solve ; Description ...: Finds all unknowns in linear simultaneous equations ; Syntax.........: _Solve($num_unknowns, ByRef $coeff) ; Parameters ....: $num_unknowns - Number of unknown variables (i.e. X,Y,Z is 3) ; $coeff - 2d array containing equations as coefficients: $coeff[1-based $equation][1-based coefficients] ; Return values .: Success - Array: 1st element is number of unknowns, elements 1 to $i are their values, in order ; Author ........: Jordan ; Remarks .......: Will only solve for linear equations. All variables on left of = sign and constant (include as last element in array) on the right. ;======================================================================= Func _Solve($num_unknowns, ByRef $coeff) Dim $solutions[$num_unknowns+1] = [$num_unknowns] $backupArray = $coeff $numberOfUns = $num_unknowns For $r = 1 To $numberOfUns For $eliminatingValue = 1 To $num_unknowns-1 Dim $new_eqs[$num_unknowns+1-$eliminatingValue][$num_unknowns+2] For $current_eq = 1 To UBound($coeff,1)-2 Dim $bypassStep[2][$num_unknowns+2] $value = $coeff[$current_eq+1][$eliminatingValue] For $index = 1 To UBound($coeff,2)-1 $bypassStep[0][$index] = $coeff[$current_eq][$index] * $value Next $value = $coeff[$current_eq][$eliminatingValue] For $index = 1 To UBound($coeff,2)-1 $bypassStep[1][$index] = $coeff[$current_eq+1][$index] * $value Next For $index = 1 To UBound($coeff,2)-1 $new_eqs[$current_eq][$index] = $bypassStep[0][$index] - $bypassStep[1][$index] Next Next $coeff = $new_eqs Next $ans = $coeff[1][UBound($coeff,2)-1] / $coeff[1][UBound($coeff,2)-2] $solutions[$num_unknowns] = $ans For $eqn = 1 To UBound($backupArray,1)-1 $backupArray[$eqn][UBound($backupArray,2)-2] = $backupArray[$eqn][UBound($backupArray,2)-1] - ($backupArray[$eqn][UBound($backupArray,2)-2] * $ans) Next ReDim $backupArray[UBound($backupArray,1)-1][UBound($backupArray,2)-1] $num_unknowns = $num_unknowns - 1 $coeff = 1 $coeff = $backupArray Next Return $solutions EndFunc; ==>_Solve() Link to comment Share on other sites More sharing options...
lareneg Posted January 14, 2009 Share Posted January 14, 2009 Awesome! Pity my calculator can't do this =p Link to comment Share on other sites More sharing options...
Larnil Posted January 15, 2009 Share Posted January 15, 2009 Thank you - just what I needed but never came around to do myself Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now