Iterative Calculation System
Posted: Sat Sep 21, 2013 2:05 pm
Hello everyone,
Sometime we have to deal with complex equation difficult to solve for a specific variable. So instead of wasting time solve equation, just go strait forward with the unsolvable equation and perform a binary search on the value required to match the expected result. See the given example to figure out what I mean.
And the example of use :
Regards
StarBootics
Sometime we have to deal with complex equation difficult to solve for a specific variable. So instead of wasting time solve equation, just go strait forward with the unsolvable equation and perform a binary search on the value required to match the expected result. See the given example to figure out what I mean.
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Iterative Calculation System
; File Name : IterativeCalculation.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 20-09-2013
; Last Update : 20-09-2013
; PureBasic code : V5.20 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Prototype.d ProtoCalculator(Input.d, *Pointer)
Procedure.d IterativeCalculation(Min.d, Max.d, Target.d, Calculator.i, *CalculatorExtraParam)
Calcul.ProtoCalculator = Calculator
While Min <= Max
Input.d = (Min + Max) / 2.0
Output.d = Calcul(Input, *CalculatorExtraParam)
If Output < Target
Min = Input
ElseIf Output > Target
Max = Input
EndIf
; If the absolute difference between the Min and Max
; is smaller than the threshold, then just give up !
If Abs(Min - Max) < 0.00000000001
Min = Max + 10
EndIf
Wend
ProcedureReturn Output
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY MEAN IT !!
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Dev-Type V4.0.0
; Project name : Iterative Calculation Demo
; File Name : IterativeCalculation - Demo.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 20-09-2013
; Last Update : 20-09-2013
; PureBasic code : V5.20 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
IncludeFile "IterativeCalculation.pb"
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Structure declaration <<<<<
Structure FinesseMax
Thrust.d
TakeOffWeight.d
LandingWeight.d
Sigma.d
WingArea.d
TOP.d
LP.d
CLMaxTO.d
CLMaxLA.d
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The observators <<<<<
Macro GetFinesseMaxThrust(FinesseMaxA)
FinesseMaxA\Thrust
EndMacro
Macro GetFinesseMaxTakeOffWeight(FinesseMaxA)
FinesseMaxA\TakeOffWeight
EndMacro
Macro GetFinesseMaxLandingWeight(FinesseMaxA)
FinesseMaxA\LandingWeight
EndMacro
Macro GetFinesseMaxSigma(FinesseMaxA)
FinesseMaxA\Sigma
EndMacro
Macro GetFinesseMaxWingArea(FinesseMaxA)
FinesseMaxA\WingArea
EndMacro
Macro GetFinesseMaxTOP(FinesseMaxA)
FinesseMaxA\TOP
EndMacro
Macro GetFinesseMaxLP(FinesseMaxA)
FinesseMaxA\LP
EndMacro
Macro GetFinesseMaxCLMaxTO(FinesseMaxA)
FinesseMaxA\CLMaxTO
EndMacro
Macro GetFinesseMaxCLMaxLA(FinesseMaxA)
FinesseMaxA\CLMaxLA
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The mutators <<<<<
Macro SetFinesseMaxThrust(FinesseMaxA, P_Thrust)
GetFinesseMaxThrust(FinesseMaxA) = P_Thrust
EndMacro
Macro SetFinesseMaxTakeOffWeight(FinesseMaxA, P_TakeOffWeight)
GetFinesseMaxTakeOffWeight(FinesseMaxA) = P_TakeOffWeight
EndMacro
Macro SetFinesseMaxLandingWeight(FinesseMaxA, P_LandingWeight)
GetFinesseMaxLandingWeight(FinesseMaxA) = P_LandingWeight
EndMacro
Macro SetFinesseMaxSigma(FinesseMaxA, P_Sigma)
GetFinesseMaxSigma(FinesseMaxA) = P_Sigma
EndMacro
Macro SetFinesseMaxWingArea(FinesseMaxA, P_WingArea)
GetFinesseMaxWingArea(FinesseMaxA) = P_WingArea
EndMacro
Macro SetFinesseMaxTOP(FinesseMaxA, P_TOP)
GetFinesseMaxTOP(FinesseMaxA) = P_TOP
EndMacro
Macro SetFinesseMaxLP(FinesseMaxA, P_LP)
GetFinesseMaxLP(FinesseMaxA) = P_LP
EndMacro
Macro SetFinesseMaxCLMaxTO(FinesseMaxA, P_CLMaxTO)
GetFinesseMaxCLMaxTO(FinesseMaxA) = P_CLMaxTO
EndMacro
Macro SetFinesseMaxCLMaxLA(FinesseMaxA, P_CLMaxLA)
GetFinesseMaxCLMaxLA(FinesseMaxA) = P_CLMaxLA
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Update operator <<<<<
Macro UpdateFinesseMax(FinesseMaxA, P_Thrust, P_TakeOffWeight, P_LandingWeight, P_Sigma, P_WingArea, P_TOP, P_LP, P_CLMaxTO, P_CLMaxLA)
SetFinesseMaxThrust(FinesseMaxA, P_Thrust)
SetFinesseMaxTakeOffWeight(FinesseMaxA, P_TakeOffWeight)
SetFinesseMaxLandingWeight(FinesseMaxA, P_LandingWeight)
SetFinesseMaxSigma(FinesseMaxA, P_Sigma)
SetFinesseMaxWingArea(FinesseMaxA, P_WingArea)
SetFinesseMaxTOP(FinesseMaxA, P_TOP)
SetFinesseMaxLP(FinesseMaxA, P_LP)
SetFinesseMaxCLMaxTO(FinesseMaxA, P_CLMaxTO)
SetFinesseMaxCLMaxLA(FinesseMaxA, P_CLMaxLA)
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Reset operator <<<<<
Macro ResetFinesseMax(FinesseMaxA)
SetFinesseMaxThrust(FinesseMaxA, 0.0)
SetFinesseMaxTakeOffWeight(FinesseMaxA, 0.0)
SetFinesseMaxLandingWeight(FinesseMaxA, 0.0)
SetFinesseMaxSigma(FinesseMaxA, 0.0)
SetFinesseMaxWingArea(FinesseMaxA, 0.0)
SetFinesseMaxTOP(FinesseMaxA, 0.0)
SetFinesseMaxLP(FinesseMaxA, 0.0)
SetFinesseMaxCLMaxTO(FinesseMaxA, 0.0)
SetFinesseMaxCLMaxLA(FinesseMaxA, 0.0)
; ClearStructure(FinesseMaxA, FinesseMax)
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Debugging macro <<<<<
Macro DebugFinesseMax(FinesseMaxA)
Debug "Thrust : " + StrD(GetFinesseMaxThrust(FinesseMaxA), 6)
Debug "TakeOffWeight : " + StrD(GetFinesseMaxTakeOffWeight(FinesseMaxA), 6)
Debug "LandingWeight : " + StrD(GetFinesseMaxLandingWeight(FinesseMaxA), 6)
Debug "Sigma : " + StrD(GetFinesseMaxSigma(FinesseMaxA), 6)
Debug "WingArea : " + StrD(GetFinesseMaxWingArea(FinesseMaxA), 6)
Debug "TOP : " + StrD(GetFinesseMaxTOP(FinesseMaxA), 6)
Debug "LP : " + StrD(GetFinesseMaxLP(FinesseMaxA), 6)
Debug "CLMaxTO : " + StrD(GetFinesseMaxCLMaxTO(FinesseMaxA), 6)
Debug "CLMaxLA : " + StrD(GetFinesseMaxCLMaxLA(FinesseMaxA), 6)
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 00.014 seconds (14428.57 lines/second) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Procedure.d TakeOffDistance(TOP.d, *FinesseMaxA.FinesseMax)
SetFinesseMaxTOP(*FinesseMaxA, TOP)
SetFinesseMaxCLMaxTO(*FinesseMaxA, (GetFinesseMaxTakeOffWeight(*FinesseMaxA) * GetFinesseMaxTakeOffWeight(*FinesseMaxA)) / (GetFinesseMaxWingArea(*FinesseMaxA) * GetFinesseMaxThrust(*FinesseMaxA) * GetFinesseMaxSigma(*FinesseMaxA) * TOP))
ProcedureReturn 20.9 * TOP + 87.0 * Sqr(TOP * GetFinesseMaxThrust(*FinesseMaxA) / GetFinesseMaxTakeOffWeight(*FinesseMaxA))
EndProcedure
Procedure.d LandingDistance(LP.d, *FinesseMaxA.FinesseMax)
SetFinesseMaxLP(*FinesseMaxA, LP)
SetFinesseMaxCLMaxLA(*FinesseMaxA, GetFinesseMaxLandingWeight(*FinesseMaxA) / (GetFinesseMaxWingArea(*FinesseMaxA) * GetFinesseMaxSigma(*FinesseMaxA) * LP))
ProcedureReturn 118 * LP + 400
EndProcedure
UpdateFinesseMax(FinesseMax.FinesseMax, 42000.0, 129000.0, 112000.0, 1.0, 1135.0, 0.0, 0.0, 0.0, 0.0)
IterativeCalculation(0.0, 1000.0, 5000.0, @TakeOffDistance(), FinesseMax)
IterativeCalculation(0.0, 1000.0, 5000.0, @LandingDistance(), FinesseMax)
DebugFinesseMax(FinesseMax)
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
StarBootics