tomleslie

13876 Reputation

20 Badges

15 years, 175 days

MaplePrimes Activity


These are answers submitted by tomleslie

because you are assuming that when you 'assume n::integer', you expect this to be applied with the plot() command. Maple's plot commands are adaptive (ie spacing of points on the x-axis is not, by default, guaranteed). This adaptive mechanism in the plot() command and the assumption that 'n' is an integer are probably conflicting, and the former is winning!

I guess therefore that you are actually computing your function values for some floating point values of 'n'. You now have two 'slightly different' ways of calculating what you expect to be the same floating point number, and are getting some rounding errors.

To calculate this at 'guaranteed' integer values, consider the following

restart;
f:= n->2^(n+10);
g:= n->1024*2^n;
plot([seq( [ n, g(n)-f(n)], n = 1 .. 80)], axes = boxed);

Plotted values are all zero.

The attached defines a procedure which will accept a matrix, vector, or list as the first argument, and the number of difference operations as an (optional) second argument. If the second argument is not supplied, it is assumed to be 1: ie first differences are computed.

If the input is a matrix, then it computes differences between adjacent elements, along the first dimension whose size does not equal one, so for 1xn matrices, column differences will be produced and for nx1 matrices, row differences will be calculated. For mxn matrices where m,n != 1, row differences will be computed

Irrespective of the supplied value of the second argument, the process will stop when no further differences can be sensibly computed, thus returning either a single row/column if a matrix is supplied, or a Vector/list with a single element.

The return datatype will be the same as the datatype of the supplied first argument


 

restart;
doDiff:= proc( y, t:=1)
               local i,
                     b:=Matrix(y):
               if   op([1,1],b) > 1
               then for i from 1 to t while op([1,1], b) > 1 do
                        b:= Matrix( op([1,1], b)-1,
                                    op([1,2], b),
                                    (j,k) -> b[j+1,k]-b[j,k]
                                  );
                   od;
              elif op([1,2],b) > 1
              then for i from 1 to t while op([1,2], b) > 1 do
                       b:= Matrix( op([1,1], b),
                                   op([1,2], b)-1,
                                   (j,k) -> b[j,k+1]-b[j,k]
                                 );
                   od;
              fi;
              return convert(b, whattype(y));
         end proc:

with(LinearAlgebra):
a:= RandomMatrix(5,5);
b:= doDiff(a);
whattype(b);
a:= RandomMatrix(1,5);
b:= doDiff(a);
whattype(b);
a:= RandomMatrix(5,1);
b:= doDiff(a);
whattype(b);
a:= RandomMatrix(5,5);
b:= doDiff(a,4);
whattype(b);
a:= RandomMatrix(1,5);
b:= doDiff(a,4);
whattype(b);
a:= RandomMatrix(5,1);
b:= doDiff(a,4);
whattype(b);
a:= Vector[row]([1,2,3,4,5]);
b:= doDiff(a);
whattype(b);
a:= Vector[column]([1,2,3,4,5]);
b:= doDiff(a,3);
whattype(b);
a:= [1,2,3,4,5];
b:= doDiff(a,2);
whattype(b);

 


 

Download doDiff.mw

Only practical suggestion is to avoid the Maple Math and Maple Plot buttons in the MaplePrimes editor.

Write your problem in a Maple worksheet, then upload the worksheet using the big green up-arrow in the MaplePrimes toolbar. AFAIK this always works

Unlike Preben, I don't get an error, and I agree with your reported answers.

However (still on default Digits setting) the DirectSearch:-GlobalSearch() command returns the maximum as

-3.329027665176695e-8, with

Sw[1] = 0.9847445254860703,
Sw[2] = 0.9999968650136312,
Sw[3] = 0.6264619803987239,
Sw[4] = 0.9999999987708149,
Sw[5] = 2.5533896919439056e-12,
Sw[6] = 2.9409717894394274e-16

with Digits:=25, the inbuilt Optimization:-Maximize() command actually produces a (slightly) "better" maximum than before, as in

-1.076760000000000000000000*10^(-7), with

Sw[1] = 1.00000000000000000000000,
Sw[2] = 1.000000000000000000000000
Sw[3] = 0.
Sw[4] = .1000000000000000000000000
Sw[5] = 0.
Sw[6] = 0.

However the DirectSearch package produces a worse answer than before, although still better than that obtained from Optimization:-Maximize, as in

-0.7442678960250108011362341e-7, with

Sw[1] = .4481966092896601567515669,
Sw[2] = .7272949187954088004101089,
Sw[3] = 0.1200332516016855177981148e-5,
Sw[4] = .5162553582155816242864986,
Sw[5] = 0.1279788241709383165516787e-12,
Sw[6] = 0.4387729736277248352151486e-14

The DirectSearch:GlobalSearch() is returning 100 "local" maxima, of which I have only listed the largest, as being the "global" maximum. However the fact that so many local maxima are obtained with values from

-0.7442678960250108011362341e-7 to -1.157640744594276503568598e-7 indicates that you are always going to have a problem determining a truly "global" maximum.

Always bear in mind that unless your objective function is convex, then no optimization program on the planet can guarantee to find a global optimum.

In the plot command, set 'adaptive=false', and then define the number of points using 'numpoints', as in

f := proc (x) options operator, arrow; sin(x) end proc;
plot(f(x), x = 0 .. 2*Pi, style=point, symbol=circle, adaptive=false, numpoints=10);

Your worksheet has

for i from 30 by 5 to -30 do
plot([eval(X(alpha1,i*Pi/180)),eval(Y(alpha1,i*Pi/180)),alpha1=-30*Pi/180..30*Pi/180],-1..1,-1..1);
end do;

Loop starts at index 30, index increases by 5 until you reach -30 - so won't execute. You either need to make the step value negative, or swap the start and stop values around.

So this works

for i from 30 by -5 to -30 do
     p[i]:=plot([eval(X(alpha1,i*Pi/180)),eval(Y(alpha1,i*Pi/180)),alpha1=-30*Pi/180..30*Pi/180],- 1..1,-1..1);
end do:
plots:-display( [seq(p[i], i=30..-30, -5)]);

And so does this

for i from -30 by 5 to 30 do
p[i]:=plot([eval(X(alpha1,i*Pi/180)),eval(Y(alpha1,i*Pi/180)),alpha1=-30*Pi/180..30*Pi/180],-1..1,-1..1);
end do:
plots:-display( [seq(p[i], i=-30..30, 5)]);

 

 

 

 

Althought the Statistics:-LinearFit() command does not accept model functions with an additive constant (surprised me!), the CurveFitting:-LeastSquares() does accept such functions.

As Rouben has pointed out the equation of the straight line that goes through the point [2, 21] is y=21+a*(x-2), so the following code returns exact and floating point results

 restart;
 with(CurveFitting):
 pts1 := Vector([0, 1, 2, 6, 12, 15]):
 pts2 := Vector([0, 13, 21, 45, 54, 77]):
 LeastSquares(pts1, pts2, x, curve = 21+a*(x-2));
 evalf(%);

(602/145)*x+1841/145

 

4.151724138*x+12.69655172

(1)

 

Download curveFit.mw

 

                         

 

Although the CodeTools package was introduced in Maple 9, the Usage() command was not introduced until Maple 14.

Maple's memory management changed substantially (ie got a lot better) in Maple 16, which led to several kernelopts() options being deprecated from that time (eg 'gcfreq' - the frequency of garbage collection).

So far as I can tell, the kernelopts(bytesalloc) and kernelopts(bytesused) options appear to have been around "forever", so Acer's code *ought* to be OK in Maple 12.

Note, however, that since the memory management was substantially improved in Maple 16, which Acer used for testing, it is very likely that you will see very different (ie worse) results running his example in Maple 12

but trivial numerically, with

   restart;
#
# Define curve
#
   f:=x->sin(cos(x+Pi));
#
# General formula for arc length
#
   L:=int(sqrt(1+diff(f(x),x)^2),x=a..b);
#
# Define end-points
#
   eval(L, [a=Pi, b=5*Pi/3]);
#
# No closed form solution? Get a numerical
# one
#
   evalf(%);

 

The amended file (attached) will return only those solutions where all parameters are non-zero: there are 3 (out of 22) such solutions

filterSols.mw

Add

plots:-semilogplot(p, alpha = 1 .. 1e100);
limit(p, alpha = infinity);

to the end of your worksheet

but the simple workaround would be to insert an unwith() command at the top of your worksheet to 'unload' the Standard Units package, as in

unwith(Units[Standard]);

Saves editing any of your initialization files

use the axes=boxed option in the plot() command as in

restart:
h:=z->1-(delta2/2)*(1 + cos(2*(Pi/L1)*(z - d1 - L1))):
K1:=((4/h(z)^4)-(sin(alpha)/F)-h(z)^2+Nb*h(z)^4):
lambda:=Int(K1,z=0..1):
F:=0.3:
L1:=0.2:
d1:=0.2:
alpha:=Pi/6:
plot( [seq(eval(lambda, Nb=j), j in [0.1,0.2,0.3])], delta2=0.02..0.1
, axes=boxed);

If you are typing the input (in either 1D or 2D input mode), just keep adding more cases, as in

piecewise( x<1, f(x), x<2, g(x), x<3, h(x))

where each 'pair' represents 'condition, value'

If you are inserting the 'piecewise template' from the Expression Palette, additional conditions can be added by right-clicking the inserted template and clicking on 'Insert Row' in the context menu

 

  1. The animation toolbar allows you to "single-step" the animation using the "left/right triangle icons with vertical bars"
  2. Since all the frames are contined within your table 'p', one can access/plot individual frames with the command

plots:-display(p[10], scaling=constrained); # for the 10-th frame in the animation

First 145 146 147 148 149 150 151 Last Page 147 of 207