Polinomic equations resolution (upto 4th degree)
Posted: Fri Oct 28, 2005 8:36 pm
Code updated For 5.20+

Code: Select all
;Program to solve cuadratic, cubic, squared, and linear polinomic equations:
;By Albert 'Psychophanta' (Oct-2005)
;***************************************************
;-User interface:
Enumeration
#Window_0
EndEnumeration
Enumeration
#Text_0
#String_0
#String_1
#String_2
#String_3
#String_4
#Text_1
#Text_2
#Text_3
#Text_4
#Text_5
#Button_0
#Text_6
#Text_7
#Text_8
#Text_9
#Text_10
#Text_11
EndEnumeration
Procedure Open_Window_0()
If OpenWindow(#Window_0, 269, 187, 449, 163, "Cuadratic, cubic, squared and linear equations resolution", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
; If CreateGadgetList(WindowID())
TextGadget(#Text_0, 55, 5, 245, 20, "Input coefficients and push Solve button", #PB_Text_Center)
StringGadget(#String_0, 5, 30, 60, 25, "")
StringGadget(#String_1, 95, 30, 60, 25, "")
StringGadget(#String_2, 185, 30, 60, 25, "")
StringGadget(#String_3, 275, 30, 60, 25, "")
StringGadget(#String_4, 355, 30, 60, 25, "")
TextGadget(#Text_1, 5, 65, 55, 20, "Solutions:")
TextGadget(#Text_2, 65, 65, 345, 20, "")
TextGadget(#Text_3, 65, 90, 345, 20, "")
TextGadget(#Text_4, 65, 115, 345, 20, "")
TextGadget(#Text_5, 65, 140, 345, 20, "")
ButtonGadget(#Button_0, 325, 5, 65, 20, "Solve")
TextGadget(#Text_6, 65, 40, 30, 15, "x^4 +")
TextGadget(#Text_7, 155, 40, 30, 15, "x^3 +")
TextGadget(#Text_8, 245, 40, 30, 15, "x^2 +")
TextGadget(#Text_9, 335, 40, 20, 15, "x +")
TextGadget(#Text_10, 420, 40, 25, 15, " = 0")
TextGadget(#Text_11, 60, -60, 530, 20, "")
; EndIf
EndIf
EndProcedure
;END User Interface
DefType .f
Open_Window_0()
Repeat
EventID.l=WaitWindowEvent()
If EventID=#PB_Event_Gadget:EGID.l=EventGadget():EndIf
If EGID.l=#Button_0
a.f=ValF(GetGadgetText(#String_0))
b.f=ValF(GetGadgetText(#String_1))
c.f=ValF(GetGadgetText(#String_2))
d.f=ValF(GetGadgetText(#String_3))
e.f=ValF(GetGadgetText(#String_4))
Gosub solve
SetGadgetText(#Text_2,sol1.s)
SetGadgetText(#Text_3,sol2.s)
SetGadgetText(#Text_4,sol3.s)
SetGadgetText(#Text_5,sol4.s)
EndIf
Until EventID=#PB_Event_CloseWindow
End
solve:
If a<>0:Gosub cuartic
sol1.s=StrF(x1.f)
sol2.s=StrF(x2.f)
sol3.s=StrF(x3.f)
sol4.s=StrF(x4.f)
ElseIf b<>0:a=b:b=c:c=d:d=e:Gosub cubic
sol1.s=StrF(x1)
sol2.s=StrF(x2)+"+("+StrF(t4.f)+"·i)"
sol3.s=StrF(x3)+"-("+StrF(t4)+"·i)"
sol4.s=""
ElseIf c<>0:a=c:b=d:c=e:Gosub square
sol1.s=StrF(x1)
sol2.s=StrF(x2)
sol3.s="":sol4.s=""
ElseIf d<>0:a=d:b=e:Gosub linear
sol1.s=StrF(x1)
sol2.s="":sol3.s="":sol4.s=""
ElseIf e<>0
sol1.s="Impossible!"
sol2.s="":sol3.s="":sol4.s=""
Else
sol1.s="0":sol2.s="0":sol3.s="0":sol4.s="0"
EndIf
Return
; Ecuacion lineal --------------------------------
linear:
x1.f=-b/a
Return
; Ecuacion cuadrada ------------------------------
square:
x1.f=(-b+Sqr(b*b-4*a*c))/2/a
x2=(-b-Sqr(b*b-4*a*c))/2/a
Return
; Ecuacion cubica --------------------------------
cubic:
t1.f=Sqr(4*Pow(-b*b+3*a*c,3)+Pow(-2*b*b*b+9*a*b*c-27*a*a*d,2))
t0.f=Pow(-2*b*b*b+9*a*b*c-27*a*a*d+t1.f,1/3)
t2.f=(-b*b+3*a*c)/3/a/Pow(2,2/3)/t0
t3.f=t0/6/a/Pow(2,1/3)
t4=Sqr(3)*t2+Sqr(3)*t3
x1=-b/3/a-Pow(2,1/3)*(-b*b+3*a*c)/3/a/t0+t0/3/a/Pow(2,1/3)
;soluciones complejas:
x2.f=-b/3/a+t2-t3;+t4*i
x3.f=-b/3/a+t2-t3;-t4*i
Return
; Ecuacion cuartica --------------------------------
cuartic:
t4.f=2*c*c*c-9*b*c*d+27*a*d*d+27*b*b*e-72*a*c*e
t0.f=Sqr(-4*Pow(c*c-3*b*d+12*a*e,3)+Pow(t4,2))
t3.f=-b*b*b/a/a/a+4*b*c/a/a-8*d/a
t6.f=Pow(t4+t0.f,1/3)/3/a/Pow(2,1/3)
t7.f=b*b/2/a/a-4*c/3/a
t1.f=Pow(2,1/3)*(c*c-3*b*d+12*a*e)/3/a/Pow(t4+t0.f,1/3)
t2.f=-b/4/a
t5.f=Sqr(b*b/4/a/a-2*c/3/a+t1.f+t6.f)
x1.f=t2.f-1/2*t5.f-1/2*Sqr(t7.f-t1.f-t6.f-t3.f/4/t5.f)
x2.f=t2.f-1/2*t5.f+1/2*Sqr(t7.f-t1.f-t6.f-t3.f/4/t5.f)
x3.f=t2.f+1/2*t5.f-1/2*Sqr(t7.f-t1.f-t6.f+t3.f/4/t5.f)
x4.f=t2.f+1/2*t5.f+1/2*Sqr(t7.f-t1.f-t6.f+t3.f/4/t5.f)
Return
