Carl Love

Carl Love

27599 Reputation

25 Badges

12 years, 65 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

Use display, or display3d--- it doesn't matter which. No capital D though. I needed to put a colon at the end of your first line and correct the spelling of "implicit" in your last line.

with(plots):
f:=(x,y,z)-> z=x^2+y^2;
implicitplot3d(f(x,y,z),x=-5..5,y=-5..5,z=-5..5);

If you're happy with the plot, save it to a variable:

p1:= %:

f:=(x,y,z)->z=2-x^2-y^2;
implicitplot3d(f(x,y,z),x=-5..5,y=-5..5,z=-5..5);

Again, save it to a variable:

p2:= %:

display([p1,p2]);


All that being said, an implicit plot is not a great choice for this problem, seeing as how your equations are already solved for z. You could just do this:

plot3d([x^2+y^2, 2-x^2-y^2], x= -5..5, y= -5..5);



 

Your computed answers are correct. It would just be nice to put them togther in a clear way. Indeed, we can do that in such a way that the solution reads almost directly as the statement of the problem.

I see from your "(9 C 3)" that you like to put C, the choose-combinations function, between its arguments. We can do that, but the Maple syntax requires that we make it &C if the function name goes between its arguments. The "choose" function in Maple is called binomial, which we'll rename &C. (Don't confuse this with the binomial probability distribution that was used for your heads-and-tails problem, which is essentially a "with replacement" problem. This problem uses what is technically called the hypergeometric distribution. They both use make use of the "choose" function.)

(Lines in bold are the input you give to Maple. Lines in italics are Maple's respones. (Usually, I suppress the response by ending a Maple input line with a colon.) Lines in normal typeface are my comments.)

restart;
`&C`:= binomial:

The backquotes (``) are required when a name with special characters is being assigned to, or when it is otherwise being used as a name.

Input the problem data:

wht, tn, pnk, pur, yel, orn, gr:= 19, 10, 7, 3, 5, 2, 6:

Check the total to make sure:

total:= `+`(%);

total := 52

(Note the backquotes around the +.) The percent sign % is used to represent the output of the last command.

Now here's your problem A. Read the command aloud to yourself like this: "From the whites, choose 3; from the nonwhites, choose the rest of the 9. In all, we've chosen 9 from the total."

(wht &C 3) * ((total-wht) &C (9-3)) / (total &C 9);

102486/351325

Get a decimal answer with the evalf command.

evalf(%);

.291712801537038

Which agrees with the answer that you got. (Notice that % again.)

Now for your problem B: In the command below, the lengthy factor with all the used colors being subtracted from the total is just 1. I only include it for didactic completeness.

(wht &C 3) * (tn &C 2) * (pnk &C 1) * (yel &C 1) * (gr &C 2)
* ((total-(wht+tn+pnk+yel+grn)) &C (9-(3+2+1+1+2))) / (total &C 9);

7695/1236664

evalf(%);

0.622238538519760e-2

which agrees with the answer that you got. Note that the number is presented in "scientific notation".

It's a second-order recurrence, so we need a second boundary condition. The problem's solution posted in your attached HTML file does not contain a clear statement of the original problem; it is just the solution. However, I infer from that solution that we can use a(30) = 1 as a boundary condition and that the ultimate goal is to find a(20).

restart;
BCs:= a(0)=0, a(30)=1:
eqn:= a(n) = 9/19*a(n+1) + 10/19*a(n-1):
A:= rsolve({eqn, BCs}, a(n), makeproc):
A(20);

47025509469056928801/147025509469056928801

evalf(%);

.319845920880512

...which agrees with the answer in your attached solution.

We can also easily get a closed-form solution.

I took a look at your polynomial (actually a rational expression) C2. Unfortunately, your worksheet was output only, so I couldn't take apart and analyze the expression. I'd be willing to look at your .m file. My advice:

  1. Use unapply to make each polynomial into a procedure.
  2. Use codegen:-optimize with the tryhard option to compress each procedure. This command is amazing in its ability to compress and simplify very lengthy expressions. I wouldn't be at all surprised if it compressed one of these expressions down to 100 lines of code.
  3. Use fsolve with the list-of-procedures input option.

(< < C|D >, < E|F > >^6 . < 0,1 >)[1];

Be careful with your use of `^`. I can tell that by A^j you mean A_j (i.e. A-subscript-j), because if you really meant exponentiation (the usual meaning of `^`), then your question is nonsensical.

Your code refers to a function fp, the derivative of f, but you never defined fp. Under the line where you define f, you can put a line

fp:= D(f);

Your assume(Rmin < Rmax) needs to be replaced with additionally(Rmin < Rmax). Multiple assume commands on the same variable are not cumulative. I tested this on your code snippet, and with the additionally it integrates fine. So, no need to upload tomorrow.

But, In the future, please cut-and-paste at least a snippet of code into your original posts, or upload a worksheet. Even a poorly written question with a code example is easier to answer than a well-written question with no code example.

How are you making the assumptions, with assume or assuming? And does the variable upon which the assumptions are made come from a function call, but other than as a parameter to that function? If you're using assuming, then that's the problem.  For example, the following works:

restart;
assume(k<0);
f:= x-> exp(k*x): # k in function, but is not a parameter
int(f(x), x= 0..infinity);

-1/k

But in the following, the assumption is ignored:

restart;
f:= x-> exp(k*x): # k in function, but is not a parameter
int(f(x), x= 0..infinity) assuming k < 0;

limit((exp(k*x)-1)/k, x = infinity)

The assumptions made with assuming  are only applied to variables which appear in the expression before it is evaluated.

To expand on what Preben said, what plot said is only a warning, not an error, and it turns out to be unrelated to why you didn't get a plot. If you apply argument or abs after taking the derivative, it plots fine:

f:= HeunG(3,-9/2+3/4*I*k-3/4*k^2,-1+1/2*I*k,-1/2+1/2*I*k,1/2,1+I*k,1/2):
f1:=diff(f,k):
plot([argument,abs](f1), k= 0..2);

Edit: I realize that this plot is not the same as the function you want. I'm just illustrating that the failure to plot is unrelated to the warning message.

Now you have three unknown functions, X(x), R(x), and T(x), but only two equations. We need more information about T(x) in order to solve or plot this.

First off, please do not use the old lowercase constructor commands matrix, array, vector. The new constructors are capitalized---Matrix, Array, Vector---and they are much more efficient and versatile. I will not help you anymore if you continue to use the lowercase constructors, as I'd rather just forget about them. The rest of this post will deal strictly with the newer versions. Also, anything that I say below about Arrays could equally well be said about Matrices or Vectors.

You wrote:

for l to 5 do: for i to 6 do: for j to 6 do: L[i,j] := P[i,j,l]: od: od: V[l]:=L: od:
The code fails in that V[1]=V[2]=...v[5] = P[i,j,5].

The immediate problem is that V[l]:= L needs to be V[l]:= copy(L); however, assigning individual entries in a loop is an inefficient way to do this (see below for a more efficient way). The reason the copy is needed is a somewhat arcane topic. You can read some about it at ?copy, but the topic requires a moderate amount of computer programming knowledge to understand. The take-away is Don't directly assign one Array to another (unless you understand the consequences); it does not make an independent copy of the Array.

There are many commands in the ?ArrayTools package for efficiently handling things like changing dimensions, copying parts of Arrays, etc.

You wrote:

Also, given a matrix, is there a way to extract a column vector from that matrix? I.e. in FORTAN is M is a matrix, M(:,3) would return the third column of M. Is there a similar command in Maple?

Yes, and the syntax is almost identical: M(.., 3) or M[.., 3]. The available combinations for selecting and rearranging subparts of Arrays is vast, and discussed at ?rtable_indexing.

So, applying this subpart selecting to your original loop, you could do the whole job as

V:= Array(1..5, i-> P[.., .., i]);


 

 

Please cut-and-paste an example, or upload a worksheet.

My guess (and it's only a guess until I see the example) is that you have a parameter with the same name as a global variable and that you're unintentionally using both in the main function. I'd be able to tell in 5 seconds if I saw an example. Making it a procedure will not help. You should never have to cut-and-paste to fix these things; almost everything can be handled programmatically/algebraically in Maple.

Sometimes---more often than I would expect---solving a more general problem is easier yet still gives a complete solution to the original problem. Such is the case here. The problem at hand is equivalent to finding all directed paths of a given length in a graph. What you call routes are formally called paths: a sequence of distinct vertices, each adjacent to the preceding.

Once the data structure is defined as I have done in the procedure below, finding the paths is essentially a one-liner. There's no need to consider special cases like vertices on corners, sides, etc.

AllPaths:= proc(
     G::GraphTheory:-Graph
    ,maxlength::nonnegint:= GraphTheory:-NumberOfVertices(G)
)
#Returns a table R such that R[k] is a list of all directed paths of length k in G,
#where k ranges from 0 to maxlength. G can be directed or undirected.
#Each path is represented as a list of vertices.
#The algorithm is breadth-first search.
    uses GT= GraphTheory;
    local
         k  #current length
        ,p  #current path
        ,v  #current vertex
        ,V:= GT:-Vertices(G)  #constant
        ,E:= table(V =~ (`{}`@op)~ (GT:-Departures(G)))  #constant table of edges
        ,R:= table([0= `[]`~ (V)])  #Initialize with zero-length paths.
    ;
    for k to maxlength do
         #For each path of length k-1, make all possible 1-vx extensions.
        R[k]:= [seq](seq([p[], v], v in E[p[-1]] minus {p[]}), p in R[k-1])
    end do;
    eval(R)
end proc:

Now apply it to the problem at hand:

squares:= [seq](seq([i,j], i= 1..4), j= 1..4):

Name the vertices conveniently.

V:= (v-> cat(['a','b','c','d'][v[1]], v[2]))~ (squares);

V := [a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4]

Vsq:= table(V =~ squares):

Define the edges of the graph. Horizontal, vertical, and diagonal adjacency are all covered by simply saying that the infinity norm of the difference of the points is 1.

Sq44:= GraphTheory:-Graph(
   select(e-> LinearAlgebra:-Norm(<Vsq[e[1]] - Vsq[e[2]]>) = 1, combinat:-choose({V[]}, 2))
):

Generate the paths and time it.

CodeTools:-Usage(assign(Pths, AllPaths(Sq44, 6))):

memory used=11.31MiB, alloc change=4.27MiB, cpu time=219.00ms, real time=210.00ms

Explore the results a bit.

nops(Pths[6]);

68272

Get a well-distributed sample of the paths.

Samp:= [seq](Pths[6][2^k], k= 1..16);

Samp := [[a1, a2, a3, a4, b3, b2, c1], [a1, a2, a3, a4, b3, b2, c3], [a1, a2, a3, a4, b3, c2, b2], [a1, a2, a3, a4, b3, c3, c2], [a1, a2, a3, a4, b4, c3, c4], [a1, a2, a3, b2, b3, c3, d4], [a1, a2, a3, b2, c3, d3, c4], [a1, a2, a3, b4, c3, b3, c4], [a1, a2, b1, c2, c3, b4, a3], [a1, a2, b3, b4, a3, b2, c2], [a1, b1, c1, c2, d3, c3, b3], [a2, a1, b2, a3, b3, c2, d1], [a2, b3, c3, d2, c1, c2, b2], [a4, b4, c3, c2, b1, c1, b2], [b4, c3, d2, c1, b1, a2, a1], [d4, c3, d2, c2, b3, b4, a3]]

Develop a few tools for visualizing the paths.

To center a vertex in its square:

pt:= v-> Vsq[v] -~ 1/2:

Background to plot on:

grid:= plot(0, view= [0..4, 0..4], gridlines, scaling= constrained):

pathplot:= p->
   plots:-display(
      grid
     ,plot(
         [[pt](p[1]), [pt](p[-1]), pt~(p)]
        ,style= [point, point, line], color= [green, red, yellow]
        ,symbol= diamond, symbolsize= 30
      )
   )
:

And finally here are the 16 sample paths:

plots:-display(Matrix(4,4, pathplot~ (Samp)));

(Sorry, I don't know how to upload an array of plots. If you execute the above code yourself, you will get a 4x4 array of plots---one for each of the sample paths.)

Correct, the shell method is easier in this case.

Give Maple the following command to plot the region in question:

plots:-implicitplot([y= x^2+4*x+6, x=1, y=2, x=3], x= 0..4, y= 0..30);

Suppose you took just one arbitrary point (x,y) inside the region and rotated that point in the manner suggested by the problem. What would be the radius of the circle made by that rotation?

Which method do you think will work better for this problem: the shell method, or the disc method?

First 384 385 386 387 388 389 390 Page 386 of 392