Carl Love

Carl Love

28095 Reputation

25 Badges

13 years, 101 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

Zooming animations are possible in a 3d plot by using the viewpoint option (see ?plot3d,viewpoint ). A 2d plot can be represented in a 3d plot by plotting only in the xy plane and setting the "camera" locations to look straight down the z axis or a line parallel to the z axis. Here's an example:

plots:-spacecurve(
     [x, sin(x), 0], x= -Pi..Pi, scaling= constrained,
     color= red,
     viewpoint= [location= [seq([0,0,10*k], k= 100..10,-1)]],
     axes= normal
);

Note that the viewpoint option is implemented in the "plot renderer" (i.e., in the GUI) rather than in Maple language itself. As such, it cannot be accessed with either of the regular animation commands: plots:-animate or plots:-display(..., insequence). It is not possible to zoom with either of these older animation commands for the reason that you noticed: The view for the whole animation is fixed to the maximal view that occurs in any of the frames.

The weight matrix completely determines the graph, so this is particularly easy.

G3:= GraphTheory:-Graph(LinearAlgebra:-Zip(max, W1, W2));

or, if you need further access to W3 (seems likely), do

W3:= LinearAlgebra:-Zip(max, W1, W2);
G3:= GraphTheory:-Graph(W3);

Try changing "times" to TIMES (all capitals, no quotes) and "roman" to ROMAN. I think that that will fix your problem, but I don't know why you are having the problem. Your original code works for me in Maple 16 on Windows 7.

The RandomMatrix is generating integers in the range -99..99 with a uniform distribution. The Generate is generating floats in the range 0..1 with a logarithmic distribution. The former is a much easier task, so the two can't really be compared. Furthermore, time[real](...) is irrelevant; you want plain time(...).

One thing you can do is use simplify with side relations. For example,

relations:= {n1+n2=1}:
expression:= n1+expand((n1+n2)^2);

simplify(expression, relations);

 

Pull down the Tools menu, and open the Options dialog. Click on the Display tab. The fifth check box is "Always insert new execution group after executing". Make sure that box is not checked. Then click Apply Globally. Let me know if that fixes your problem.

Remove the option y= 0..100.

ODE2:=(Diff(T(t), t) = 1/59*(18-T(t))):
DEtools[DEplot]( ODE2, T(t), t=-4..4, {[T(0)=88]});

Your problems are caused by your combining several with commands into one statement. You have

with(plots), with(ColorTools), with(LinearAlgebra), with(RandomTools), with(ExcelTools);

You should change that to

with(plots); with(ColorTools); with(LinearAlgebra); with(RandomTools); with(ExcelTools);

The results of chaining with statements seems unpredictable. Note the following proviso from ?with :

The with command is effective only at the top level, and intended primarily for interactive use. Because with operates by using lexical scoping, it does not work inside the bodies of procedures, module definitions, or within statements. [emphasis added]

Although I can't tell what's wrong with your code until you upload it, here's an example of correctly doing what you want to do. Maybe you'll be able to correct your code from this.

restart:
with(VectorCalculus):
SetCoordinates(cartesian[x,y]):
V:= VectorField(< x+y, x-y >):
F:= (X,Y)-> VectorCalculus:-Norm(VectorCalculus:-evalVF(V, < X,Y >)):
F(1,2);

Here's how to apply the weights from your main graphs to your subgraphs constructed from the paths.

ApplyWeights:= proc(G1::GraphTheory:-Graph, G2::GraphTheory:-Graph)
#Apply the weights from G1 to G2.
uses GT= GraphTheory;
local W:= GT:-WeightMatrix(G1, copy= false);
     GT:-Graph(
          map(e-> [e, W[e[]]], GT:-Edges(G2, weights= false))
     )
end proc:          

G4:= ApplyWeights(G1, G3);

G5:= ApplyWeights(G2, G3);

Here's how to do it. I used essentially the same example as I did for your previous question, except that I made it a directed graph instead of undirected, because I think that's more relevant to your application.


restart:

randomize(2): #Needed to get nontrivial minimal paths.

N:= 100:  #Number of vertices

E:= 2300: #Number if directed edges

CartProdSeq:= proc(L::seq(list))

local Seq,i,j;

option `Copyright (C) 2007, Joseph Riel. All rights reserved.`;

    eval({subs(Seq=seq, foldl(Seq

                              , [cat(i,1..nargs)]

                              , seq(cat(i,j)=L[j],j=nargs..1,-1)

                             ))});

end proc:


AllPossibleEdges:= [
     (CartProdSeq([$1..N], [$1..N]) minus
           {seq([k,k], k= 1..N)}
      )[]
]:

Edges:= combinat:-randcomb(AllPossibleEdges, E):

Weights1:= RandomTools:-Generate(list(float(method= uniform), E)):

WeightedEdges1:= {zip(`[]`, Edges, Weights1)[]}:

G1:= GraphTheory:-Graph(WeightedEdges1);

GRAPHLN(directed, weighted, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], Array(%id = 18446744074187374710), `GRAPHLN/table/1`, Matrix(%id = 18446744074216096014))

Weights2:= RandomTools:-Generate(list(float(method= uniform), E)):

WeightedEdges2:= {zip(`[]`, Edges, Weights2)[]}:

G2:= GraphTheory:-Graph(WeightedEdges2);

GRAPHLN(directed, weighted, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], Array(%id = 18446744074187375070), `GRAPHLN/table/2`, Matrix(%id = 18446744074198802974))

(Path1,Cost1):= GraphTheory:-DijkstrasAlgorithm(G1, 1, N)[];

[1, 31, 52, 58, 54, 14, 100], .199037223686419

(Path2,Cost2):= GraphTheory:-DijkstrasAlgorithm(G2, 1, N)[];

[1, 49, 24, 5, 14, 100], .418737682605255

Path_to_Edges:= proc(P::list)
local k;
     {seq([P[k], P[k+1]], k= 1..nops(P)-1)}
end proc:


PPath1:= Path_to_Edges(Path1);

{[1, 31], [14, 100], [31, 52], [52, 58], [54, 14], [58, 54]}

PPath2:= Path_to_Edges(Path2);

{[1, 49], [5, 14], [14, 100], [24, 5], [49, 24]}

G3:= GraphTheory:-Graph(PPath1 union PPath2);

GRAPHLN(directed, unweighted, [1, 5, 14, 24, 31, 49, 52, 54, 58, 100], Array(%id = 18446744074187376150), `GRAPHLN/table/3`, 0)

WeightOfPath:= proc(G::GraphTheory:-Graph, P::list)
local
     e,
     W:= GraphTheory:-WeightMatrix(G, copy= false)
;
     add(W[e[]], e in Path_to_Edges(P))
end proc:


WeightOfPath(G1, Path1);

.199037223686419

WeightOfPath(G2, Path2);

.418737682605255

 


Download Two_Paths.mw

You have a line

F[total] = F[C4H10](W) + ...

which should be changed to

F[total]:= F[C4H10](W) + ....

After that, you cannot use F[total](0) as a variable. So change the three occurrences of F[total](0) to something else, such as F_total_0.

I made these changes, and the dsolve and plots come out perfectly.

Here's an example based on an example found on the help page ?DynamicSystems,TransferFunction .

ss_a:= Matrix([[1, 2], [0, 4]]):
ss_b:= Matrix([[3, 7], [9, 6]]):
ss_c:= Matrix([[5, 6], [5, 2]]):
ss_d:= Matrix([[0, 0], [0, 0]]):
sys:= DynamicSystems:-TransferFunction(
     ss_a, ss_b, ss_c, ss_d,
     discrete, sampletime=0.001, systemname="MIMO system"
);

sys:-tf[1,1];

sys:-tf[1,2];

And likewise for sys:-tf[2,1] and sys:-tf[2,2]. In order for me to give you more details, you'll need to post the code that led to the RTABLEs you showed.

 

The command to find the minimal-weight path (assuming the weights are nonnegative) is GraphTheory:-DijkstrasAlgorithm, not GraphTheory:-ShortestPath.

Here is a complete solution to your Questions 1 and 2:

1. Construction of the random weighted graph:

restart:
randomize(3): #Needed to get a nontrivial shortest path.
N:= 100: #Number of vertices.
E:= 2300: #Number of randomly chosen edges.
Edges:= map(`{}`@op, combinat:-randcomb(combinat:-choose(N,2), E)):
Weights:= RandomTools:-Generate(list(float(method= uniform), E)):
WeightedEdges:= {zip(`[]`, Edges, Weights)[]}:
G:= GraphTheory:-Graph(WeightedEdges);

2. Find the minimal-cost path and its cost:

Sol:= GraphTheory:-DijkstrasAlgorithm(G, 1, N);

(Path,Cost):= Sol[];

Path:= [seq({Path[k], Path[k+1]}, k= 1..nops(Path)-1)]:

3. Find the maximal-cost edge on that path:

W:= GraphTheory:-WeightMatrix(G, copy= false):
MaxWeight:= -infinity:
for e in Path do
     if W[e[]] > MaxWeight then
          MaxWeight:= W[e[]];
          MaxEdge:= e
     end if
end do:

4. Construct the subgraph that has that edge removed:

G1:= GraphTheory:-Graph(WeightedEdges minus {[MaxEdge, MaxWeight]});

 

 

Here's how to solve your followup request---using larger initial sets. This will handle the case in the first Answer also.

#Step 0:
B1:= A1+A2:  B2:= A1+A3:  B3:= A3+A4:  B4:= A2+A4:
C1:= `+`(A||(1..4)):
R:= {A||(1..4), B||(1..4), C1}:

#Step 1:

The procedure X3 takes any set and produces a set with three times as many elements, using the process that you described.

X3:= proc(R::set)
local
     Names:= indets(R,name),
     S0:= [R[]],
     A,
     S01:= subs(seq(A= cat(A,1), A in Names), S0),
     S02:= subs(seq(A= cat(A,2), A in Names), S0)
;
     {S01[], S02[], zip(`+`, S01, S02)[]}
end proc:

S1:= X3(R):  T1:= X3(S1):  #etc.
nops(T1); #Confirmation
     81

#Step 2:

This is exactly the same as in the first answer, with S1 replaced with T1, etc. Is that enough detail for you to finish it?

First 299 300 301 302 303 304 305 Last Page 301 of 395