## 3068 Reputation

19 years, 300 days

Doug

`---------------------------------------------------------------------Douglas B. Meade  <><Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.eduPhone:  (803) 777-6183         URL:    http://www.math.sc.edu`

## 2 options: eval and assign...

This is a common question. In addition to Scott's suggestion to use rhs, let me suggest some more general and powerful approaches.

To get started, let's obtain the solution and expand the RootOf's in the second solution returned by solve:

```sol := solve({x^3+y+1 = 0, y^2+x+1 = 0}, [x, y]);

[                 [                                                 2
[                 [           /  5       3                         \
[[x = -1, y = 0], [x = -RootOf\_Z  + 3 _Z  + 3 _Z - 1, label = _L57/  - 1,

]]
/  5       3                         \]]
y = RootOf\_Z  + 3 _Z  + 3 _Z - 1, label = _L57/]]

Sol := subsop( 2=allvalues(sol[2]), sol ):   # expand the RootOf's in the second solution```

and create a matrix to take the place of the matrix operations you wish to perform for each solution:

`M := << 1, x > | < y, x+y > >;`

The first think many users will think of is to turn the equations into assignments. This can be done with the assign command as follows:

```unassign( 'x', 'y' );
for s in Sol do
assign( s );
M;
unassign( 'x', 'y' );
end do;
```

Note that it is important to unassign any values before attempting to go on to the next solution. Assigning values to x and y can have unintended consequences in other parts of your work.

The approach I would take to this problem is to use the eval command to evaluate the matrix for certain values of x and y:

```for s in Sol do
eval( M, evalf(s) );
end do;

```

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## mvcoeff - coefficient of a term in a mul...

Does this come close to providing the functionality you are seeking?

```mvcoeff := proc( expr, term  )
local a, t;
a := expand( expr );
if type(term,`^`) or type(term,name) then
return coeff( a, term );
end if;
for t in [op(term)] do
a := coeff( a, t )
end do;
return a
end proc:
```

Here is how mvcoeff would be used

```f := x - x*y - 3*x*y^2 + q*x^2*y*z^3 - y^3*z;
2      2    3    3
x - x y - 3 x y  + q x  y z  - y  z
T := x^2*y*z^3;
2    3
x  y z

mvcoeff( f, x^2*y*z^3 );
q
mvcoeff( f, x*y*z );
0
mvcoeff( f, y );
2  3
-x + q x  z
mvcoeff( f, y^2 );
-3 x
```

This procedure does not have any parameter checking or error processing, so it's not really ready for widespread use. But, I think it could be a useful tool. The key is to make use of Maple's coeff command to iteratively extract the coefficient of each term in a multivariate polynomial.

The same idea can be implemented recursively, as follows:

```mvcoeff2 := proc( expr::polynom, term )
if type(expr,expanded) then
q:=expr
else
q:=expand(expr)
end if;
if type(term,`^`) or type(term,name) then
return coeff( q, term );
end if;
mvcoeff2( coeff( q, op(1,term) ), subsop(1=1,term) );
end proc:
```

In this implementation the first argument is automatically checked to be a polynomial and, if it is not already expanded, it is expanded prior to extracting any coefficient. (This is different from the way in which coeff is written.)

I hope I have not overlooked too many special cases. It might be nice to augment this so that you could request the constant term with mvcoeff( f, 1 ). But, I note that not even coeff allows this.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## export plot via the GUI...

If you are working interactively, you can always "export" the plot in the format of your choice.

Once you have the plot looking the way you want it to look, move the cursor over the plot and press the right mouse button. In the "context menu" that appears, select "Export" (the last entry), select the file format you desire, and release the mouse button. Use the file browser to give the file a name, and to put it where you will find it.

This can be easier than using plotsetup, unless you are attempting to automate the creation of a large set of plots.

Doug

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

Oh, how I wish I had time to work on this right now. Unfortunately, I won't have time until tonight, at the earliest.

Others will probably beat me to it, but if I get to working on this I will need some more information.

How are users entering A and B? Are you using a popup maplet? embedded component? readstat?

One idea that comes to mind is to use the automatic type checking in Maplets:-Tools:-Get . The key here would be to come up with an appropriate "type" . If this can be done, then setting corrections=true and update=true could handle all of this in one step.

I'll be check back later tonight to see what others have suggested, and to offer my own contribution (I hope).

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## QuestionDialog maplet...

Are you thinking of something like what is provided via the QuestionDialog maplet?

Here's something I threw together to show what might be possible.

```P := proc( )
local i:=1, continue:=true;
uses Maplets;
for i while continue do
continue := parse(
Display(
Maplet(
QuestionDialog( sprintf("Step %a:\n   Do you wish to continue ?",i),
'onapprove'=Shutdown("true"),
'ondecline'=Shutdown("false")
) ) ) );
end do;
printf("Stopping after %a steps.",i-1);
end proc:
P();
```

Clicking yes continues the loop, no terminates it.  You could certainly do something like this in other situations.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## GenerateRandomOrderedSequence...

Nice question. Here's a short proc I just created to do what you request. The defintion could be significantly shortened if I assumed (or required) the first argument is smaller than the second argument. But, this implementation works with both increasing and decreasing sequences.

```restart;
GenerateRandomOrderedSequence := proc( LO::integer := 0, HI::integer := 10 )
local lo, hi, S, reverse;
uses RandomTools, ListTools;
reverse := evalb( LO>HI );
if reverse then
lo,hi := HI,LO
else
lo,hi := LO,HI;
end if;
S := lo;
while [S][-1]<>hi do
S := S, Generate(integer(range=[S][-1]+1..hi));
end do;
if reverse then
return Reverse( [S] )
else
return [S]
end if;
end proc;
```

Here are some trial runs to show it in action.

```GenerateRandomOrderedSequence();
[0, 1, 7, 10]
GenerateRandomOrderedSequence();
[0, 1, 8, 10]
GenerateRandomOrderedSequence();
[0, 1, 6, 8, 9, 10]
GenerateRandomOrderedSequence();
[0, 10]
GenerateRandomOrderedSequence(10,20);
[10, 14, 20]
GenerateRandomOrderedSequence(20,10);
[20, 18, 10]
GenerateRandomOrderedSequence(-10,10);
[-10, -1, 5, 6, 10]
GenerateRandomOrderedSequence(-10,10);
[-10, -8, 1, 6, 9, 10]

```

I hope this does what you need, or is close enough for you to modify on your own. Let us know if you have any questions or further requests.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## First Thoughts, but Clarification Reques...

Hmm. Where should I start.

You have one equation with 3 variables (x, y, r). If r = sqrt(x^2+y^2) then there are still 2 variables.

You indicate that x is between 0 and 500,000. Your function is real-valued only when  x&le;y and x&le;y/r^2.

In Maple, the Bessel function is written BesselJ.

Did you really expect an explicit solution to this equation? There appears to be a trivial solution when y=x. Otherwise, you are probably going to have to settle for a numerical solution (which is probably OK given that you are working in MATLAB).

Here's a start for exploring this function in Maple:

```restart;
eq := ((2*y*(y/r^2-x)^0.5*BesselJ(1,(y/r^2-x)^0.5)*BesselJ(1,(y-x)^0.5))
-((y-2*x)^2*BesselJ(0,(y/r^2-x)^0.5)*BesselJ(1,(y-x)^0.5))
-(4*x*(y/r^2-x)^0.5*(y-x)^0.5*BesselJ(1,(y/r^2-x)^0.5)*BesselJ(0,(y-x)^0.5)))=0;
eq2 := eval( eq, r=sqrt(x^2+y^2) );
```

To begin to understand this function, let's look at it for a few values of x:

```plot( eval(lhs(eq2),x=     0), y=     0..  1000 );
plot( eval(lhs(eq2),x=100000), y=100000..101000 );
plot( eval(lhs(eq2),x=500000), y=500000..501000 );```

These plots show that there are many solutions for each value of x. When x=0, Maple is able to give us some "solutions"

```solve( eval(eq2,x=000000), y );
-15
0., -0.9418862802 + 6.665312468 10    I
```

The second solution is not really complex-valued (look how small the imaginary part is).

```fsolve( eval(eq2,x=000000), y );

-0.9418862802```

but a negative solution is not realistic (unless we are, in fact, working in the complex field).

Maple has less success finding solutions (exact or numerical) for larger values of x.

You might have some success with some sort of an expansion (series, ...) of the lhs of your equation.

But, first, I think it would be helpful if you could clarify your problem and check that my assumption (about r) is correct.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## on hex conversion and output with user-s...

X:="18DBD3547552C73BE4DE87731C500":
p(X);
"00C53177E84DBE732C554735BD8D0100"
convert(123,hex);
7B
convert(X,hex);
Error, invalid input: convert/hex expects its 1st argument, n, to be of type nonnegint, but received 18DBD3547552C73BE4DE87731C500
convert( 18DBD3547552C73BE4DE87731C500, hex );
Error, missing operator or `;`

It appears to me that conversion to hex wants a nonnegative integer as its argument. This makes sense.

The procedure p in acer's response assumes the input is a string. If you want to ensure the results have a fixed length, here's one way to modify p:

p2 := proc(x::string,N::posint:=32)
local xlength, n, paddedx, i, r, d;
n := ceil( max(length(x),N) / 4 );
i=1..n*2));
end proc:

Note that the second argument, N, is optional with a default value of 32.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## string surgery, leading to a matrix...

This request can be fulfilled by some simple string surgery.

The pre-operation preparations are taken care of by the StockImport code in the original post:

```restart;

StockImport := proc (ticker, Start, End, Step)
local startYear, startMonth, startDay, endYear, endMonth, endDay, timeStep, sid, str, b, Ap;  uses Sockets ;
startYear := parse(Start[1 .. 4]); startMonth := parse(Start[5 .. 6]); startDay := parse(Start[7 .. 8]);
endYear := parse(End[1 .. 4]); endMonth := parse(End[5 .. 6]); endDay := parse(End[7 .. 8]); timeStep := Step;
sid := Sockets:-Open("ichart.yahoo.com", 80);
Sockets:-Write(sid, cat("GET /table.csv?s=", ticker, "&a=",
startMonth-1, "&b=", startDay, "&c=", startYear, "&d=",
endMonth-1, "&e=", endDay, "&f=", endYear, "&g=", timeStep,
"&ignore=.csv HTTP/1.0 \n\n"));
str := ""; b := Sockets:-Read(sid);
while b <> false do str := cat(str, b); b := Sockets:-Read(sid) end do;
Sockets:-Close(sid); str
end proc:```

Next we move into the actual surgery. We want to discard everything before the second occurrence of Date. Rows of the matrix are detected by new lines and the elements of each row are separated by commas. The first row consists of headers and the first column is a id tag for that row. The remaining entries should be treated as numbers. Here's how all of this can be done in Maple:

```toMatrix := proc(S::string)
local s, m, hdr, id, body;
uses StringTools;
s := SubString( S, SearchAll( "Date", S )[2]..-2 );  # discard up to second Date
s := Split( s, "\n\f" );                             # split into rows
s := map( Split, s, "," );                           # split rows into columns
m := Matrix( s );                                    # make matrix
hdr  := map( convert, m[1,..],   name );             # pull off headers
id   := map( convert, m[2..,1],  name );             # pull off row identifiers
body := map( parse,   m[2..,2..] );                  # extract body of matrix (as numbers)
return < hdr , < id | body > >                       # re-assembly
end proc:
interface( rtablesize=25 ):                            #  needed to be able to see matrices up to 25x25

toMatrix( StockImport( "^gspc", "20080101", "20090821", "m") );
```

If this is not how you want to see the results, I hope this coding gives you a foundation to perform the exact surgery that you need.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## labeling circle by arclength...

I now see a fairly easy way to produce the plot you are seeking. The key is that arclength for the unit circle is just radian measure of the angle with the positive x axis.

Here's how I would produce the plot you describe:

```with( plots ):
P := plot( [cos(t),sin(t), t=0..2*Pi] ):
P2 := plot( [seq([cos(k),sin(k)], k=1..6)],
style=point, symbol=solidcircle, symbolsize=18, color=blue ):
T := textplot( [seq([cos(k),sin(k),"t="||k], k=1..6)],
align=[right,above] ):
display( [P,P2,T] );
```

If you want to show points other than those with t=1,2,3,4,5,6, replace the 1..6 with a list of t-values, e.g., k=[0,0.8,1]

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## reading off points from a Maple plot...

If I understand your question, you want to have tickmarks on the unit circle showing the (arc)length of the part of the curve from the x-axis (counterclockwise). I do not know an easy way to do this in Maple.

While I can see how you would use this to answer the questions you outlined, it is not the way I would expect someone to answer this question. (But, maybe this is part of the instructions for your problem; if so, ignore my suggestion.) What I would do is plot the sine and cosine functions on [0,2*Pi] and then use Maple's point probe to get the approximate values. Here's how I would plot the two finctions (in one picture):

```plot( [sin(t),cos(t)], t=0..2*Pi );
```

Once the plot appears, click the cursor on the plot. A text box will appear immediately above the top left corner of the main Maple work area; the coordinates of the cursor will be shown in this text box. Move the cursor to a location close the the appropriate point on the appropriate curve, when you click the left mouse button (assuming a right-handed mouse setup) the coordinates of this point will appear. You can verify that the x-coordinate is close to what you want, and read off the corresponding y-value. That's how I would answer these questions.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## maplet help (w/action)...

Maple is complaining that it does not see the AC action. This is because your definition of this action is not done inside the definition of the maplet. With this one change the maplet does run, but I am not in a position to decide if it is working as you expect it to.

Personally, I do not like to put the definition of the maplet inside the call to display the maplet. Here's how I would structure your code:

```restart :
with(Statistics) :
with(plots) :
with(LinearAlgebra) :
with(Maplets) :
with(Maplets[Elements]) :
with(Maplets[Tools]):
with(ArrayTools) :
X := proc (x) global pap, mam, LL, LLL, Nr, Nc, g;
if not assigned(LL) then
pap := Maplet(FileDialog['FD1']('filefilter' = "csv", 'filterdescription' = "csv", 'onapprove' = Shutdown(['FD1'])));
mam := Display(pap)[1];
LL := ImportMatrix(mam, source = csv) :
LLL := LL[2 .. -1, 2 .. -1] :
Nr, Nc := Size(LLL) :
g[1] := ""
end if;
if assigned(LL) then
g[3] := Set(('DD')(itemlist) = sort([seq(Row(LL, 1)[i], i = 2 .. Nc)], lexorder));
g[x]
end if :
end proc :
drop1 := DropDownBox[DD](["Time Series"], width = 200):    #  onchange=AC
tx1 := TextField[TF1](width = 15, background = green, editable = false) :
p1 := Plotter[P](width = 600) :
x1 := Button['PP']("End Plot", onclick = Shutdown()) :
tool := "Select a CSV file with time series organized in columns where the first column \n contain the dates/years etc and the first row contains the names" :
maplet := Maplet(
[ [Button['Bu']("Select a csv file with data",
tooltip = tool,
onclick = Action(Evaluate('Bu' = 'X(1)'),
Evaluate('TF1' = 'X(2)'),
Evaluate('DD' = 'X(3)'))),
tx1,
drop1
],
[p1]
],
Action[AC](Evaluate(P = 'plot(x^2, x = 0 .. 10, color = red, thickness = 3)'))
):
Display(maplet) ;

```

I do not see that you do any processing of the data, in particular I do not see that you ever put anything in the plotter P. I'm guessing that will be added once you see that this initial step is working. Let us know if you have more problems and need more of our assistance.

Here's how I would be tempted to create LLL and define Nr and Nc:

```LLL := LL[2..-1,2..-1];
Nr, Nc := Size(LLL);
```

Note that these 2 lines replace 3 lines in  your code, and eliminate a call to Size.

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## solve...

You need a way to refer to your equations. You can use the equation labels on the right side of the Maple window, but this is difficult to mimic in a MaplePrimes post. So, I'll resort to giving them names:

```eq1 := x+y=60;
eq2 := x=y-12;

Now, you want Maple to find the solve equations eq1 and eq2 for the variables x and y. The command follows this description:
```
```solve( {eq1,eq2}, {x,y} );
```

Try it!

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## infolevel...

The traditional way of getting some idea about what Maple is doing is to use the userinfo and infolevel commands.

userinfo is used by programmers to provide intermediate information about a command.

Users use the infolevel to control how much information they see, and which procedures information they want to see. The easiest use is to turn on the reporting from all commands:

```infolevel[all] := 100;   # it's not unusual to set this to be much higher
100
```

Then, when you try to simplify an expression you might see something like this:

```simplify( sin(x)^2+cos(2*x)-3*sin(x)^4 );
simplify/do: applying  commonpow  function to expression
simplify/do: applying  power  function to expression
2           4
-3 + 7 cos(x)  - 3 cos(x)

```

Doug

```---------------------------------------------------------------------
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu```

## use plots:-animatecurve...

Your p is not an animation. Try using the animatecurve command from the plots package, as follows:

p := plots:-animatecurve( [80*t*sin(2.35), -9.81/2*t^2-80*t*cos(2.35), t=0..15], frames=200 );

You might need to tweak some of the other buttons to get all of the desired functionality, but this will at least give you an animation.

Good luck,

Doug

```---------------------------------------------------------------------