Carl Love

Carl Love

28070 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Here's a worksheet that summarizes all that I've said so far, and also works out some of the finer details and nuisances of implementation.


 

restart:

Digits:= 15: #15 is usually best (fastest, most robust) value.

f:= (x::numeric, a::numeric, b::numeric, c::numeric)->
   a*I + b*x*exp(c*I):

g:= (x::numeric)->
   7*I + 5*x*exp(3*I):

So, we expect the result a = 7, b = 5, c = 3. But note that f(a, -b, c-Pi) = f(a, b, c), so there are equivalent results that are just as valid.

(A,B):= (-2,2): #interval of x-values (arbitrarily chosen)

H:= (a::numeric, b::numeric, c::numeric)->
   Re(int(
     (x-> abs(f(x,a,b,c) - g(x))^2), A..B,
     numeric, digits= iquo(Digits,2)
   ))
:

#In H:
#1. Re is needed because the result is sometimes returned as x + 0.*I.
#2. We must use operator form to avoid evaluations with symbolic x.
#3. The squared norm seems to work better.
#4. Results will be computed to 7 digits, and there'll be 8 guard digits (15-7 = 8).

infolevel[Optimization]:= 5:
#Ask the minimizer to reveal some of its internal process.

CodeTools:-Usage(Optimization:-Minimize(H));
#Find (a,b,c) that minimize H, and get timing stats.

NLPSolve: calling NLP solver

NLPSolve: using method=pcg
NLPSolve: number of problem variables 3
NLPSolve: optimality tolerance set to 0.3256082241e-11
NLPSolve: iteration limit set to 50
NLPSolve: trying evalhf mode
NLPSolve: trying evalf mode

attemptsolution: number of major iterations taken 10
memory used=0.52GiB, alloc change=428.01MiB, cpu time=5.56s, real time=5.66s, gc time=234.38ms

 

[3.66534600000000022*10^(-16), Vector(3, {(1) = 7.00000000054163, (2) = -5.00000000065577, (3) = -.141592653649165})]

(1)

This says that a = 7, b = -5, c = -.14159 (3 - Pi). The value on the left is the minimal value of the norm.

 


 

Download Complex_norm_minimization.mw

If you want the number to display with the number of decimal places with which it was entered, but you don't want to explicitly specify that number of places, then use

FloatString:= (x::float)-> sprintf("%.*f", -op(2,x), x);
FloatString(0.0141);

To either convert command, append the clause assuming t1 > 0.

If your expression for drag force is df, then all that you need is

Cd:= V-> df *~ V;

If you have more trouble, please post a Maple worksheet (not a .docx).

Whether you use a colon or a semicolon to terminate a statement in a loop makes no difference. The only one that matters is the one after the final end do.

The loop depth for which output is displayed is controlled by the environment variable printlevel. By default, this is set to 1, so only the output of unnested loops is displayed. If you increase it:

printlevel:= 2;

then the output of every command in your doubly nested loops will be shown. I strongly advise that you only use this technique for debugging because, in general, the volume of output will be overwhelming.

The proper way to display the output of commands within loops or other control structures is to use print. That is, for any X that you want displayed, use print(X).

To return the entry of minimal modulus in a single line of code, use

rts[min[index](abs~(rts))];

It is also possible (in a very short single line of code) to sort the entries by modulus. This is especially useful for lists of eigenvalues.

This can also be done with simplify with side relations:

sol:= dsolve({diff(y(t),t$2) + w^2*y(t) = p/m*exp(-a*t), y(0) = 0, D(y)(0) = 0}):
simplify(sol, {a= n*w, p= m*w^2*u});

 

Suppose that p is a univariate polynomial, and that you want to plot the complex roots of p of modulus less than 1. Then do

plot(
   [Re,Im]~(select(x-> abs(x) < 1, [fsolve(p, complex)])),
   style= point, symbol= solidcircle, symbolsize= 18, scaling= constrained
);

If p is something other than a univariate polynomial, then more-sophisticated steps are required for the solving, but the selecting and plotting are the same.

I don't know why you're having trouble implementing Kitonum's Answer. Here's a different way using eval instead of subs. Create a list (not a vector) of the symbolic variables in the matrix:

X:= [x1, x2];

That part only needs to be done once. Then for each evaluation that you want using the values from vector x, do

eval(hf, X =~ seq(a, a= x));

Thank for providing the extra infomation that the error does not occur when you initialize N[i] to []. Now I am sure that you're dealing with a bug in Maple's 2D Input. The bug is that when certain expressions such as N[i] evaluate to NULL in 2D Input, it's as if they weren't there at all. So your statement

N[i]:= N[i], Data[i,j][4];

is passed to the parser as

N[i]:= , Data[i,j][4];

which, of course, has an unexpected comma.

The best thing that you can do is simply not use 2D input. Maple's 2D input is a completely unredeemable piece of garbage that's riddled with bugs like this.

But you can also change it to this, which is a much more efficient way to write it anyway:

for i to 2 do 
   N[i]:= seq(Data[i,j][4], j= 1..8) 
end do:

Now you don't need to initialize N[i].

The other Answers are great, but to Answer your Question directly:

You need to use = rather than := to set up equations. Like this

Eq1:=  x = y/p:
Eq1:=  z = x*a+y*(1-b):
Eq1:=  x = 100*z/(p*r+r);

There is never a situation where one would put a free symbolic variable (such as a variable which is to be "solved for") on both the left side and right sides of assignment operators (:=). Doing so causes "recursive assignment".

The following method is similar to Kitonum's, but it'll work in 1D input, so that you can easily edit the results. Like Kitonum's method, you must be prepared to follow Maple's own indentation and spacing style. Here's my procedure:

Indent:= (P::procedure)-> 
   map2(
      printf,
      "%s\n",
      map(
         s-> `if`(s[1]= " ", s[5..], s),
         StringTools:-Split(debugopts(procdump= P), `\n`)[..-2]
      )
   )[]
:

Now apply that to your procedure; for example, using Kitonum's example procedure,

Indent(PrimeTwins);

PrimeTwins := proc(N)
local n, a, b, L;
   n := 0;
   a := 2;
   do
     b := nextprime(a);
     if b-a <= 2 then
       n := 1+n;
       L[n] := [a, b];
       a := b
     else
       a := b
     end if;
     if n = N then
       break
     end if
   end do;
   convert(L,list)
end proc

Now copy-and-paste the output to a 1D Input (aka Maple Input) region, a Code Edit Region, or an external text editor.

All that being said, I strongly prefer to simply indent my code with the space bar as I am entering it.

Put your cursor in the cell (or paragraph or execution group or output region) that you want to delete and press Ctrl-Delete. It  doesn't matter whether or not the cell is empty.

Actually, in Maple you can usually do away with do-nothing or pass-along parameters such as the u and v in your Question. Then the issue raised by Tom is less of an issue. The following produces identical final results to all of the previous Answers:

g:= alpha*beta:
h:= alpha+beta:
f:= D[1](g)*h:
f(u,v);

The u and v in the final line are arguments, not parameters. (Roughly speaking, they are "global" and not "local"---but arguments and parameters are the precise terms.)

The lines where you defined m and n have not been executed because you didn't press Enter after entering them.

Your PLOT commands work in this case, but like Rouben says, it'd be better to use display or plots:-display. PLOT (all uppercase letters) is an expert-level command. This is not to be confused with plot (all lowercase letters), which is a regular command.

First 188 189 190 191 192 193 194 Last Page 190 of 395