acer

32333 Reputation

29 Badges

19 years, 325 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

@Petra Heijnen Yes, in Maple 18 you can insert a PlotComponent from the palette, and then in your looping code update that component by using SetProperty to give it a new value.

For example, insert a PlotComponent from the palette. Right-click on it and discover that its name/identity is, say, PlotComponent0. Then, in your looping code, whenever you wish to update the component to show a new plot you can use the SetProperty command. Your code might look something like this,

for i from something to whatever do

  if some_condition_of_yours then
    P := plot(...);
    DocumentTools:-SetProperty("PlotComponent0", ':-value', P, ':-refresh'=true);
  end if;

end do:

Now, it's up to you where you insert the component. You might want it close to the place from which you execute your code, but that's up to you.

Apply the evalf command to obtain floating-point approximations.

Eg,

evalf( ln(3) )

evalf( exp(5) )

acer

I'm supposing that this is homework of some sort, and that the goal is for you to figure out how and why (and whether) it can be done.

Create column Vectors of the coefficients in g1 and g2, both with respect to all of the terms present, ie., [x,x^2,y,y^2]. Concatenate those as columns of a Matrix M. Create a similar Vector V, for f. Then call LinearSolve(M,V) and the result if it exists will be a 2-element Vector whose entries are the coefficients of the linear combination.

If you have any constant terms then make the Vectors one row taller, and use them in the extra row. 

acer

Here is a hack for 2D display. I expect that someone could also do similarly for 3D.

Of course, it's not ideal to try and turn boxes into disks. The ideal would be support for customized appearance in the GraphTheory package itself.

Improvements welcome, but please remember that it was just written as a quick hack. I used a disk, as it was simplest to code. It could look better with an ellipse, depending on the length/height ratio of a vertex label.

 

restart:

quad2disk := proc(P::specfunc(anything,:-PLOT))
  subsindets(P,
             And(specfunc(':-POLYGONS'),
                 satisfies(p->nops(p)>0
                           and op(1,p)::[[numeric,numeric],[numeric,numeric],
                                         [numeric,numeric],[numeric,numeric]])),
             proc(p) local t,r;
               (t,r):=selectremove(type,[op(p)],list(list(numeric)));
               ':-POLYGONS'(seq(op(plottools:-disk([(op([1,1],pp)+op([3,1],pp))/2,
                                    (op([1,2],pp)+op([2,2],pp))/2],
                                   (op([1,1],pp)-op([3,1],pp))/2)),
                                pp in t), op(r)); end proc);
end proc:

with(GraphTheory):
W := Graph(undirected, {{0, 1}, {0, 2}, {0, 3}}):
HighlightEdges(W, W, black):
HighlightVertex(W, [1, 2, 3, 0], gray):
DrawGraph(W);

quad2disk(DrawGraph(W));

with(SpecialGraphs):
G := SoccerBallGraph():
HighlightVertex(G, Vertices(G), cyan):
HighlightEdges(G, G, gray):

DrawGraph(G);

quad2disk(DrawGraph(G));

with(RandomGraphs):
T := RandomTree(10):
HighlightVertex(T, Vertices(T), gold):
HighlightEdges(T, T, COLOR(RGB,0.2,0.2,0.7)):
DrawGraph(T);

quad2disk(DrawGraph(T));

with(GraphTheory):
G := Graph({{1,2},{2,3},{3,1},{3,4},{4,5},{5,6},{6,4}}):
A := ArticulationPoints(G):
HighlightVertex(G,A,magenta):
DrawGraph(G);

quad2disk(DrawGraph(G));

acer

Sorry this took a while. Hopefully it is still of some use to you.

I was able to compute smaller/shorter/simplified results for your problematic expressions, for which simplify(...) and simplify(...,size) was failing you. And using the same technique I was also able to compute smaller/shorter results for your earlier expressions which had previously been simplified. See the attached worksheet. I get similar results in Maple 18.02 and Maple 2015.1, but notice that timings are higher in 64bit Windows than 64bit Linux on comparable hardware.

The problems seem to boil down to a few things. Don't evaluate some of the unknowns to floating-point values before simplifying, if you can avoid it. It can lead to a variety of severe problems. Also, simplification of huge expressions containing trig function calls is tricky. In this case it seems that restraining the combining of trig terms to just a few of the relevant unknowns may be key.

I also tried to avoid final results with this kind of ugly and suspicious form, which you might normally hope would produce saner coefficients but which in fact gets returned by some simplification call:

(1.2349876*10^(-9)) * ( ...+ (5.6781234*10^11)*subexpr ... + anotherexpr/(3.4568762*10^(-8)) ... )

This particular run was in 64bit Maple 18.02 on Windows 7.


restart:

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]

[17368631, 10628589, 3022026]

# 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]:

Length(FirstOrderSys);

[17369013, 10628827, 3022100]


# 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]

memory used=1.35GiB, alloc change=0 bytes, cpu time=32.93s, real time=32.91s, gc time=1.65s

[23064, 15987, 4230]

# 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]

Length(FirstOrderSys[1][4]);
attempt[4]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][4]),size),vals) ):
Length(attempt[4]);

[320581, 190662, 55457]

memory used=0.85GiB, alloc change=0 bytes, cpu time=21.65s, real time=21.21s, gc time=1.72s

[15652, 10059, 3115]

Length(FirstOrderSys[1][6]);
attempt[6]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][6]),size),vals) ):
Length(attempt[6]);

[283665, 169216, 48990]

memory used=478.32MiB, alloc change=1.08MiB, cpu time=11.23s, real time=11.16s, gc time=780.00ms

[4482, 2545, 1043]

Length(FirstOrderSys[1][8]);
attempt[8]:=CodeTools:-Usage( eval(simplify(combine(FirstOrderSys[1][8]),size),vals) ):
Length(attempt[8]);

[282660, 168429, 49389]

memory used=0.87GiB, alloc change=-1.08MiB, cpu time=23.37s, real time=23.03s, gc time=1.70s

[17310, 11123, 3442]

Length(FirstOrderSys[1][10]);
#simplify(combine(FirstOrderSys[1][10])):
#simplify(combine(FirstOrderSys[1][10]),size):

[2860090, 1731310, 499444]

Length(FirstOrderSys[1][12]);
#simplify(combine(FirstOrderSys[1][12])):
#simplify(combine(FirstOrderSys[1][14]),size):

[6579073, 4041715, 1144160]

Length(FirstOrderSys[1][14]);
#simplify(combine(FirstOrderSys[1][14])):
#simplify(combine(FirstOrderSys[1][14]),size):

[6693656, 4115730, 1164858]

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]

memory used=8.26GiB, alloc change=1.61MiB, cpu time=3.40m, real time=3.32m, gc time=16.22s

[37250, 26612, 5085]

Length(FirstOrderSys[1][12]);
smaller[12]:=CodeTools:-Usage( S(FirstOrderSys[1][12], vals, {y[1],y[7]}) ):
Length(smaller[12]);

[6579073, 4041715, 1144160]

memory used=19.34GiB, alloc change=0 bytes, cpu time=8.24m, real time=7.92m, gc time=50.25s

[59613, 42377, 8229]

Length(FirstOrderSys[1][14]);
smaller[14]:=CodeTools:-Usage( S(FirstOrderSys[1][14], vals, {y[1],y[7]}) ):
Length(smaller[14]);

[6693656, 4115730, 1164858]

memory used=21.55GiB, alloc change=0 bytes, cpu time=8.76m, real time=8.34m, gc time=57.28s

[58252, 41483, 7994]

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]

[23064, 15987, 4230]

memory used=392.70MiB, alloc change=0 bytes, cpu time=10.56s, real time=10.30s, gc time=889.21ms

[8479, 5978, 1191]

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]

[4482, 2545, 1043]

memory used=332.69MiB, alloc change=0 bytes, cpu time=9.52s, real time=9.23s, gc time=951.61ms

[2792, 1723, 441]

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]

[17310, 11123, 3442]

memory used=245.90MiB, alloc change=0 bytes, cpu time=7.58s, real time=7.43s, gc time=858.01ms

[6469, 4396, 954]

# 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]

evalf[500](eval([eval(FirstOrderSys[1][8],vals), smaller[8]], othervals)): evalf(%);

[dy[8] = -1.570269543, dy[8] = -1.570269543]

evalf[500](eval([eval(FirstOrderSys[1][10],vals), smaller[10]], othervals)): evalf(%);

[dy[10] = .3065316635, dy[10] = .3065316633]

evalf[500](eval([eval(FirstOrderSys[1][12],vals), smaller[12]], othervals)): evalf(%);

[dy[12] = .4532824238, dy[12] = .4532824238]

evalf[500](eval([eval(FirstOrderSys[1][14],vals), smaller[14]], othervals)): evalf(%);

[dy[14] = -9.305591553, dy[14] = -9.305591553]

 


Download Example_modif2_1802.mw

If you want to see the module definition "shell" then you can still get that printed by querying for the 2nd operand of the module.

op(2,eval(sqrt));

      module() local ModuleApply, Nested, Product, Primes; end module

The above was done in Maple 2015.1, and is also what Maple 18.02's Standard GUI prints when eval(sqrt) is called.

acer

You seem to have used "basis" in two ways. At the end you mention a basis {C}, apparently in the mathematic sense. At the beginning you mentioned the A,B,C,D for a "basis" for your computations, apparently using the term in a non-technical sense. The inconsistency may confuse some people.

I'll use DD rather than D which is a protect Maple name with special meaning as the differential operator.

If you want to express the Matrix A+B in terms of the "basis" of Matrices {C,DD} then you can turn everything into column Vectors, concatenate the new column-Vector elements of your "basis" into a Matrix, and use LinearSolve.

Note that {C} or {C,DD} aren't enough to form a basis for the 4-dimensional space that spans the space. So for some candidate Matrix other that the Matrix A+B your set {C,DD} might not suffice.

Taking your trivial example,

restart:                                                       

DD:=Matrix(2,symbol=d):                                        
C:=Matrix([[i,i],[-i,i]]):                                     

A:=Matrix([[1,0],[0,1]]):                                      
B:=Matrix([[0,1],[-1,0]]):                                     

with(LinearAlgebra):                                           

f:=m->convert(m,Vector[column]):                               

sol:=LinearSolve(<f(C)|f(DD)>,f(A+B)):                         

A+B=add(`if`(sol[j]=0, 0, sol[j].[C,DD][j]), j=1..Dimension(sol));

                         [ 1    1]           [i     i]
                         [       ] = (1/i) . [       ]
                         [-1    1]           [-i    i]

In other words the problem solved above is to express A+B as a linear combination of the entries of {C,DD}. (...and the solution of that might be done using 4x1 column vectors rather than 2x2 Matrices.) Is that close to what you want? For other examples you might need to use values rather than just symbols for the entries of Matrix DD. In your trivial example is turns out not to have mattered...

You can change the above example to have C = Matrix([[0,i],[-i,0]]) instead, and then use f(A.B) instead of f(A+B) in the call to LinearSolve. The result of coefficients, sol, would happen to be the same.

acer

Fun := Diff(f(x), x):           

int(Fun, x = 2 .. 7, continuous);

                                 -f(2) + f(7)

acer

If you assign that expression to a name, say ee, then try,

simplify(ee, size)

acer

A call to fsolve with multivariate input returns unevaluated if it cannot find a solution. But as a numeric approach it may not be attempting to prove analytically that there is no solution, so the unevaluated return means only that it could not find one. In the general (including the nonpolynomial case) it may be that there is a solution which was not found, and thus it would be wrong for a routine that merely searches to return a result (such as NULL) and convey the impression that there definitely is no solution.

Checking whether the result is of type 'specfunc(anything,fsolve)' is an ok way to ascertain whether a solution was found. But note that if done at the top-level (outside of a procedure) then the assigned result should be wrapped in eval(...,1) as the first argument to type, if you wish to avoid a recomputation. See here for a post about that, and note Joe Riel's comment.

For such a system of polynomials with real, numeric coefficients you might consider using RootFinding:-Isolate if you wish to find all real roots and also to get an empty list in the case that it ascertains that none exist.

acer

If m1 is a Matrix then,

F,C := LinearAlgebra:-Dimensions(m1)

or,

F,C := op(1,m1)

acer

If I've understood you properly,

restart:
with(plots):
animate(plot3d,[sqrt(h),theta=-t..0,h=0..4,coords=cylindrical],t=0..2*Pi,
                orientation=[-63,-24,-51],style=surface,title="");

restart:
with(plots):
display(seq(plot3d(sqrt(h),theta=-t*2*Pi..0,h=0..4,coords=cylindrical),t=0..1,1/25),
        orientation=[-63,-24,-51],style=surface,insequence=true);

acer

Digits set to 4 means that floating-point computations are performed with 4 digits of working precision. It does not mean (either with or without rounding) that results in general will attain 4 correct places.

acer

T := table(["green" = "gruen", "red" = "rot", "blue" = "blau"]):

assigned(T["asdf"]);

                                    false

assigned(T["green"]);

                                    true

acer

If you have assigned your original inert integral of a piecewise to the name K, then you could do,

value(K) assuming continuous:
simplify(%) assuming T2>T1;

acer

First 224 225 226 227 228 229 230 Last Page 226 of 336