> |
with(LinearAlgebra): with(Physics[Vectors]): with(ArrayTools): with(DEtools): with(inttrans): Setup(mathematicalnotation=true): #with(CodeGeneration): #with(StringTools):
|
> |
# Three different commands to measure the "size" of an expression. # Using `length` to measure simplification is not so sensible. # The command `simplify/size/length` is what simplify(...,size) uses. :-Length:=u->[length(u), `simplify/size/length`(u), MmaTranslator:-Mma:-LeafCount(u)]:
|
> |
e_x:=_i: e_y:=_j: e_z:=_k:
|
> |
e_x_I := e_x: e_y_I := cos(alpha(t))*e_y + sin(alpha(t))*e_z: e_z_I := -sin(alpha(t))*e_y + cos(alpha(t))*e_z:
|
> |
e_x_II := cos(beta(t))*e_x_I - sin(beta(t))*e_z_I: e_y_II := e_y_I: e_z_II := sin(beta(t))*e_x_I + cos(beta(t))*e_z_I:
|
> |
e_x_L := cos(eta(t))*e_x_II + sin(eta(t))*e_y_II: e_y_L := -sin(eta(t))*e_x_II + cos(eta(t))*e_y_II: e_z_L := e_z_II:
|
> |
e_x_R := -cos(delta(t))*e_x_L + sin(delta(t))*e_z_L: e_y_R := -e_y_L: e_z_R := sin(delta(t))*e_x_L + cos(delta(t))*e_z_L:
|
> |
r_L := x(t)*e_x + y(t)*e_y + z(t)*e_z:
|
> |
r_Schnitt := r_L + Lsy*e_y_L + Lsz*e_z_L:
|
> |
r_R := r_Schnitt - Lsz*e_z_R - Lsy*e_y_R:
|
> |
omega_L_in_I:=diff(alpha(t),t)*e_x + diff(beta(t),t)*e_y_I + diff(eta(t),t)*e_z_II:
|
> |
omega_R_in_I:=omega_L_in_I + diff(delta(t),t)*e_y_L:
|
> |
F_T_L := -m*diff(r_L,t,t): F_T_R := -m*diff(r_R,t,t):
|
> |
J := Matrix([[19670, 0, 0], [0, 19390, 757], [0, 757, 750]]): omega_L_in_I_v:=Vector([omega_L_in_I.e_x,omega_L_in_I.e_y,omega_L_in_I.e_z]): omega_R_in_I_v:=Vector([omega_R_in_I.e_x,omega_R_in_I.e_y,omega_R_in_I.e_z]): H_L_v:= MatrixVectorMultiply(J,omega_L_in_I_v): H_R_v:= MatrixVectorMultiply(J,omega_R_in_I_v): H_L:=H_L_v(1)*e_x+H_L_v(2)*e_y+H_L_v(3)*e_z: H_R:=H_R_v(1)*e_x+H_R_v(2)*e_y+H_R_v(3)*e_z:
|
> |
M_T_L:=-diff(H_L,t): M_T_R:=-diff(H_R,t):
|
> |
G_L:=-m*g*e_z*evalf(cos(w_phi)): G_R:=-m*g*e_z*evalf(cos(w_phi)):
|
> |
F_S_L:=Fsx*e_x + Fsy*e_y + Fsz*e_z: F_S_R:=-F_S_L:
|
> |
M_S_L:=Msx*e_x_L + Msz*e_z_L: M_S_R:=-M_S_L:
|
> |
M_F_L:=-c_d*delta(t)*e_y_L: M_F_R:=-M_F_L:
|
> |
F_R_L:=Frlx*e_x+Frly*e_y+Frlz*e_z: F_R_R:=Frrx*e_x+Frry*e_y+Frrz*e_z: M_R_L:=Mrlx*e_x+Mrly*e_y+Mrlz*e_z: M_R_R:=Mrrx*e_x+Mrry*e_y+Mrrz*e_z:
|
> |
ForBil_L:=F_T_L+G_L+F_S_L+F_R_L=0: ForBil_R:=F_T_R+G_R+F_S_R+F_R_R=0:
|
> |
MomBil_L:=M_T_L+M_S_L+M_F_L+M_R_L=0: MomBil_R:=M_T_R+M_S_R+M_F_R+M_R_R=0:
|
> |
NeuBasisFR:=[e_x = e_x_neuFR, e_y = e_y_neuFR, e_z = e_z_neuFR]:
|
> |
BaseFR:= [e_x_2FR=subs(NeuBasisFR,e_x_II),e_y_2FR=subs(NeuBasisFR,e_y_II), e_z_2FR=subs(NeuBasisFR,e_z_II)]:
|
> |
solFR:=solve(BaseFR,{e_x_neuFR,e_y_neuFR,e_z_neuFR}):
|
> |
Neu_ForBil_R:=collect(collect(collect(simplify(subs(solFR,subs(NeuBasisFR,ForBil_R))),e_x_2FR),e_y_2FR),e_z_2FR):
|
> |
NeuBasisM:=[e_x = e_x_neuM, e_y = e_y_neuM, e_z = e_z_neuM]:
|
> |
BaseM:= [e_x_2M=subs(NeuBasisM,e_x_L),e_y_2M=subs(NeuBasisM,e_y_L), e_z_2M=subs(NeuBasisM,e_z_L)]:
|
> |
solM:=solve(BaseM,{e_x_neuM,e_y_neuM,e_z_neuM}):
|
> |
Neu_MomBil_L:=collect(collect(collect(simplify(subs(solM,subs(NeuBasisM,MomBil_L))),e_x_2M),e_y_2M),e_z_2M):
|
> |
Neu_ForBil_L:=ForBil_L: Neu_MomBil_R:=collect(collect(collect(simplify(MomBil_R),e_x),e_y),e_z):
|
> |
ForX_L:=coeff(lhs(Neu_ForBil_L),e_x)=0: ForX_R:=coeff(lhs(Neu_ForBil_R),e_x_2FR)=0: ForY_L:=coeff(lhs(Neu_ForBil_L),e_y)=0: ForY_R:=coeff(lhs(Neu_ForBil_R),e_y_2FR)=0: ForZ_L:=coeff(lhs(Neu_ForBil_L),e_z)=0: ForZ_R:=coeff(lhs(Neu_ForBil_R),e_z_2FR)=0: TorX_L:=coeff(lhs(Neu_MomBil_L),e_x_2M)=0: TorX_R:=coeff(lhs(Neu_MomBil_R),e_x)=0: TorY_L:=coeff(lhs(Neu_MomBil_L),e_y_2M)=0: TorY_R:=coeff(lhs(Neu_MomBil_R),e_y)=0: TorZ_L:=coeff(lhs(Neu_MomBil_L),e_z_2M)=0: TorZ_R:=coeff(lhs(Neu_MomBil_R),e_z)=0:
|
> |
Fsx:=solve(ForX_L,Fsx): Fsy:=solve(ForY_L,Fsy): Fsz:=solve(ForZ_L,Fsz): Msx:=solve(TorX_L,Msx): Msz:=solve(TorZ_L,Msz):
|
> |
Sys:=[ForX_R,ForY_R,ForZ_R,TorX_R,TorY_L,TorY_R,TorZ_R]:
|
> |
GenKo:={x(t),y(t),z(t),alpha(t),beta(t),eta(t),delta(t)}:
|
> |
res:=solve(Sys,diff(GenKo,t,t)):
|
> |
FirstOrderSys := convertsys(res, [], GenKo, t, y, dy ): Length(FirstOrderSys); Length(FirstOrderSys[1]);
|
![[17369013, 10628827, 3022100]](/view.aspx?sf=216948_Answer/8ef4e42a42236ee66f6482a3bff0ad4f.gif)
![[17368631, 10628589, 3022026]](/view.aspx?sf=216948_Answer/a7c00dafc0e0c40c84897f6b25ac4945.gif)
> |
# Don't assign floating-point values before simplification. # It can lead to several problems, including: # - prohibitively high time resources trying to simplify # - results with uncancelled float coefficients with both very large # and very small exponents # - failure to simplify productively #Lsy:=0.04685: #Lsz:=0.02108: #:-R:=0.122: #ra:=0.376: #m:=1.269: #g:=9.81: #w_phi:=Pi/180: #c:=10000: #d:=1000: #c_d:=10001: #mu:=0.2:
|
> |
# Instead of assigning float values up front, create a list of # equations at which the purely exact & symbolic simplification # results can be evaluated. In other words, when possible instantiate # at floats after simplification, rather than before. vals:=[Lsy=0.04685,Lsz=0.02108,:-R=0.122,ra=0.376,m=1.269, g=9.81,w_phi=Pi/180,c=10000,d=1000,c_d=10001,mu=0.2]:
|
![[17369013, 10628827, 3022100]](/view.aspx?sf=216948_Answer/56757bacff620845dd062c0b7c167c61.gif)
> |
# The expressions assigned to attempt[i] are like your original results.
|
> |
Length(FirstOrderSys[1][2]); attempt[2]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][2]),size),vals) ): Length(attempt[2]);
|
![[348758, 211475, 59706]](/view.aspx?sf=216948_Answer/0a73f833792b87548cf190838b015d56.gif)
memory used=1.35GiB, alloc change=0 bytes, cpu time=32.93s, real time=32.91s, gc time=1.65s
![[23064, 15987, 4230]](/view.aspx?sf=216948_Answer/ff3b09f465638a90fd749d9a850158c3.gif)
> |
# Actually your original instantiated at floats first, before simplifying. # But for the earlier terms like FirstOrderSys[1][2] it doesn't seem to make # such a difference. Length( simplify(combine(eval(FirstOrderSys[1][2],vals)),size) );
|
![[23064, 15987, 4230]](/view.aspx?sf=216948_Answer/b49fad1b2100bf9145baa852f820fc2c.gif)
> |
Length(FirstOrderSys[1][4]); attempt[4]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][4]),size),vals) ): Length(attempt[4]);
|
![[320581, 190662, 55457]](/view.aspx?sf=216948_Answer/255f3e242e039303bfdd886bbbd3db7e.gif)
memory used=0.85GiB, alloc change=0 bytes, cpu time=21.65s, real time=21.21s, gc time=1.72s
![[15652, 10059, 3115]](/view.aspx?sf=216948_Answer/355ba5b81e58c95391aca8018852a28f.gif)
> |
Length(FirstOrderSys[1][6]); attempt[6]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][6]),size),vals) ): Length(attempt[6]);
|
![[283665, 169216, 48990]](/view.aspx?sf=216948_Answer/f48aa97b85a2d169c6fdf484f9ec384b.gif)
memory used=478.32MiB, alloc change=1.08MiB, cpu time=11.23s, real time=11.16s, gc time=780.00ms
![[4482, 2545, 1043]](/view.aspx?sf=216948_Answer/830cf3151c8ca1d6450347ce525b16fe.gif)
> |
Length(FirstOrderSys[1][8]); attempt[8]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][8]),size),vals) ): Length(attempt[8]);
|
![[282660, 168429, 49389]](/view.aspx?sf=216948_Answer/a3aaa59ff4f8de8c7c5a8293d4226641.gif)
memory used=0.87GiB, alloc change=-1.08MiB, cpu time=23.37s, real time=23.03s, gc time=1.70s
![[17310, 11123, 3442]](/view.aspx?sf=216948_Answer/00f4ad406ad5ebba7f3b4be32043bd91.gif)
> |
Length(FirstOrderSys[1][10]); #simplify(combine(FirstOrderSys[1][10])): #simplify(combine(FirstOrderSys[1][10]),size):
|
![[2860090, 1731310, 499444]](/view.aspx?sf=216948_Answer/4d1de71ba657966d925a7835d3a3b021.gif)
> |
Length(FirstOrderSys[1][12]); #simplify(combine(FirstOrderSys[1][12])): #simplify(combine(FirstOrderSys[1][14]),size):
|
![[6579073, 4041715, 1144160]](/view.aspx?sf=216948_Answer/e0ef7e30605fe0a5af268a357d09e21c.gif)
> |
Length(FirstOrderSys[1][14]); #simplify(combine(FirstOrderSys[1][14])): #simplify(combine(FirstOrderSys[1][14]),size):
|
![[6693656, 4115730, 1164858]](/view.aspx?sf=216948_Answer/8926461d0fba7baabb5b9633ab32f04a.gif)
> |
S:=proc(ee, valeqns::list(`=`), unkns::set) local t1,t2,t3,t4,ft4; # Basic preliminary simplification. t1:=simplify(ee,':-size'); # This step seems to improve the step that follows it. t2:=simplify(t1,':-trig'); # Only combine a subset of the trig function calls. t3:=frontend(combine,[t2],[{`+`,`*`,`^`, specfunc(identical(unkns[]), {':-sin',':-cos'})},{}]); t4:=simplify(t3,size); # Evaluate at the float values you had for some of the unknowns. ft4:=eval(t4,valeqns); # Turn rational coefficients into floats, to get cancellation # with floats in `valeqns`. But leave alone integer coefficient # inside trig function calls. simplify(frontend(expand,[frontend(evalf,[ft4])]),':-size'); end proc:
|
> |
Length(FirstOrderSys[1][10]); smaller[10]:=CodeTools:-Usage( S(FirstOrderSys[1][10], vals, {y[1],y[7]}) ): Length(smaller[10]);
|
![[2860090, 1731310, 499444]](/view.aspx?sf=216948_Answer/46763ab22beaf0270e1abc0e7c502924.gif)
memory used=8.26GiB, alloc change=1.61MiB, cpu time=3.40m, real time=3.32m, gc time=16.22s
![[37250, 26612, 5085]](/view.aspx?sf=216948_Answer/049c44555c70072f9c25267c6212c66b.gif)
> |
Length(FirstOrderSys[1][12]); smaller[12]:=CodeTools:-Usage( S(FirstOrderSys[1][12], vals, {y[1],y[7]}) ): Length(smaller[12]);
|
![[6579073, 4041715, 1144160]](/view.aspx?sf=216948_Answer/cf370d4a9fb179742f2c329e276e1fa4.gif)
memory used=19.34GiB, alloc change=0 bytes, cpu time=8.24m, real time=7.92m, gc time=50.25s
![[59613, 42377, 8229]](/view.aspx?sf=216948_Answer/a0d8e2a4078790a941b8e7b96fadcc49.gif)
> |
Length(FirstOrderSys[1][14]); smaller[14]:=CodeTools:-Usage( S(FirstOrderSys[1][14], vals, {y[1],y[7]}) ): Length(smaller[14]);
|
![[6693656, 4115730, 1164858]](/view.aspx?sf=216948_Answer/c2a285ea817f216f15e0268bd3a64a16.gif)
memory used=21.55GiB, alloc change=0 bytes, cpu time=8.76m, real time=8.34m, gc time=57.28s
![[58252, 41483, 7994]](/view.aspx?sf=216948_Answer/a51ada912d1872806342b8a996e9f361.gif)
> |
Length(FirstOrderSys[1][2]); Length(attempt[2]); smaller[2]:=CodeTools:-Usage( S(FirstOrderSys[1][2], vals, {y[1],y[7]}) ): Length(smaller[2]);
|
![[348758, 211475, 59706]](/view.aspx?sf=216948_Answer/223708590ef0b2b1a3bca6c62af60232.gif)
![[23064, 15987, 4230]](/view.aspx?sf=216948_Answer/234858f00c1cf3b11f84213275758411.gif)
memory used=392.70MiB, alloc change=0 bytes, cpu time=10.56s, real time=10.30s, gc time=889.21ms
![[8479, 5978, 1191]](/view.aspx?sf=216948_Answer/b929e209dbe19b17a34d4fc422abb6df.gif)
> |
Length(FirstOrderSys[1][6]); Length(attempt[6]); smaller[6]:=CodeTools:-Usage( S(FirstOrderSys[1][6], vals, {y[1],y[7]}) ): Length(smaller[6]);
|
![[283665, 169216, 48990]](/view.aspx?sf=216948_Answer/ebee22499987fe0dcae734ef356d3c8f.gif)
![[4482, 2545, 1043]](/view.aspx?sf=216948_Answer/d5a39e82fb0ee03c280ffa9fbae540b4.gif)
memory used=332.69MiB, alloc change=0 bytes, cpu time=9.52s, real time=9.23s, gc time=951.61ms
![[2792, 1723, 441]](/view.aspx?sf=216948_Answer/eed01e57736adb7081fdc53cba718ae5.gif)
> |
Length(FirstOrderSys[1][8]); Length(attempt[8]); smaller[8]:=CodeTools:-Usage( S(FirstOrderSys[1][8], vals, {y[1],y[7]}) ): Length(smaller[8]);
|
![[282660, 168429, 49389]](/view.aspx?sf=216948_Answer/8ec6285135e9a9e0c7033dda14505c74.gif)
![[17310, 11123, 3442]](/view.aspx?sf=216948_Answer/d755178faef60ace2df411cd937a376b.gif)
memory used=245.90MiB, alloc change=0 bytes, cpu time=7.58s, real time=7.43s, gc time=858.01ms
![[6469, 4396, 954]](/view.aspx?sf=216948_Answer/0f17b945e588e12f14d2c1923dcef986.gif)
> |
# additional numeric values for sanity check othernames:=indets(FirstOrderSys,name) minus {constants} minus {undefined} minus {dy[1],dy[2],dy[3],dy[4],dy[5],dy[6],dy[7],dy[8], dy[9],dy[10],dy[11],dy[12],dy[13],dy[14]}: othervals:=Equate([othernames[]],[seq(0.5+0.01*i,i=0..nops(othernames)-1)]):
|
> |
evalf[500](eval([eval(FirstOrderSys[1][2],vals), smaller[2]], othervals)): evalf(%);
|
![[dy[2] = 1.324343197, dy[2] = 1.324343197]](/view.aspx?sf=216948_Answer/2c2b991fc7453c9a42d6c7a5d49e5044.gif)
> |
evalf[500](eval([eval(FirstOrderSys[1][8],vals), smaller[8]], othervals)): evalf(%);
|
![[dy[8] = -1.570269543, dy[8] = -1.570269543]](/view.aspx?sf=216948_Answer/4a53add9e7a1bea664a518f5f8d3f13b.gif)
> |
evalf[500](eval([eval(FirstOrderSys[1][10],vals), smaller[10]], othervals)): evalf(%);
|
![[dy[10] = .3065316635, dy[10] = .3065316633]](/view.aspx?sf=216948_Answer/cebe9ec10492edd0776b5de5bbd9ad14.gif)
> |
evalf[500](eval([eval(FirstOrderSys[1][12],vals), smaller[12]], othervals)): evalf(%);
|
![[dy[12] = .4532824238, dy[12] = .4532824238]](/view.aspx?sf=216948_Answer/144a2970d610d0b113c2148d9e7f4953.gif)
> |
evalf[500](eval([eval(FirstOrderSys[1][14],vals), smaller[14]], othervals)): evalf(%);
|
![[dy[14] = -9.305591553, dy[14] = -9.305591553]](/view.aspx?sf=216948_Answer/25fabc853c6d295698e842035f6b9960.gif)
|