acer

32747 Reputation

29 Badges

20 years, 109 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

Some of the expressions in list S contain the name u[10]. That is, they are polynomials with u[10] appearing a name and not a function call.

But you previously made the assignment

u[10]:=unapply(1,x):

Thus the call to fsolve does not make sense.

Did you intend to unassign the name 'u[10]' right before calling fsolve (which could result in a solution where u[10] were not 1, eg. u[10] = -.5220762901)?

Or did you intend on substituting the value of 1 for u[10] in S, and passing fsolve one less expression (eg, by removing, say, one of those containing u[10])?

It's not really clear what your general term is, from your description.

I have guessed a general term, where the summation can be done to produce an explicit closed form (last example below). But even when the terms have to be added individually I don't see it as taking "a long time".

restart;

f := (r,t,n) -> add(BesselJ(0, t*r)*r^(2*(i-1)), i=1..n):

CodeTools:-Usage(
  evalf(Int(unapply(f(r,151.5793716314014,100),r), 0..1))
);

memory used=0.65MiB, alloc change=0 bytes, cpu time=43.00ms, real time=43.00ms, gc time=0ns

0.2274316632e-1

CodeTools:-Usage(
plot( t -> evalf(Int(unapply(f(r,t,100),r), 0..1)),
      100.00 .. 200 )
):

memory used=52.01MiB, alloc change=38.01MiB, cpu time=8.09s, real time=8.10s, gc time=18.81ms

restart;

f := (r,t,n) -> add(BesselJ(0, t*r)*r^(2*(i-1)), i=1..n):

CodeTools:-Usage(
  evalf(Int(f(r,151.5793716314014,100), r=0..1, method=_d01ajc))
);

memory used=0.79MiB, alloc change=0 bytes, cpu time=48.00ms, real time=48.00ms, gc time=0ns

0.2274316632e-1

CodeTools:-Usage(
plot( t -> evalf(Int(f(r,t,100), r=0..1, method=_d01ajc)),
      100.00 .. 200 )
):

memory used=105.31MiB, alloc change=36.00MiB, cpu time=8.38s, real time=8.38s, gc time=40.20ms

restart;

f := (r,t,n) -> Sum(BesselJ(0, t*r)*r^(2*(i-1)), i=1..n):

CodeTools:-Usage(
  evalf(Int(f(r,151.5793716314014,100), r=0..1, method=_d01ajc))
);

memory used=1.13MiB, alloc change=0 bytes, cpu time=27.00ms, real time=27.00ms, gc time=0ns

0.2274316632e-1

CodeTools:-Usage(
plot( t -> evalf(Int(f(r,t,100), r=0..1, method=_d01ajc)),
      100.00 .. 200 )
):

memory used=237.17MiB, alloc change=70.01MiB, cpu time=8.83s, real time=8.84s, gc time=84.62ms

 

If active sum can produce a closed form then it can be even faster.

 

restart;

f := unapply(sum(BesselJ(0, t*r)*r^(2*(i-1)), i=1..n), [r,t,n]);

proc (r, t, n) options operator, arrow; BesselJ(0, t*r)*(r^2)^(n+1)/(r^2*(r^2-1))-BesselJ(0, t*r)/(r^2-1) end proc

CodeTools:-Usage(
  evalf(Int(f(r,151.5793716314014,100), r=0..1, method=_d01ajc))
);

memory used=347.12KiB, alloc change=0 bytes, cpu time=8.00ms, real time=8.00ms, gc time=0ns

0.2274316632e-1

CodeTools:-Usage(
plot( 'evalf'(Int(f(r,t,100), r=0..1, method=_d01ajc)),
      t = 100.00 .. 200 )
);

memory used=21.45MiB, alloc change=2.00MiB, cpu time=440.00ms, real time=441.00ms, gc time=23.34ms

 

Download evalfInt_example.mw

It is pretty fast also with Digits=15 (and reduced accuracy requested for plotting, since you cannot see the difference).

Perhaps you could provide an actual worksheet that illustrates the slow behavior.

evalfInt_example_15.mw

You incorrectly have square brackets around the first argument passed to NonlinearFit, which is not valid syntax. I got rid of them in the procedure k__plot .

Also, I suspect that the very large coefficient in the exp terms is unnecessary and serves only to make it harder for an optimizer (because the corresponding factor k is then needlessly small, with loss of working precision).

restart 

with(Statistics) 

  X1 := Vector([0, 1, 2, 3, 4, 5, 6, 7, 8], datatype = float)

Y1 := Vector([0, -0.18e-1, -0.36e-1, -0.44e-1, -0.49e-1, -0.51e-1, -0.52e-1, -0.54e-1, -0.54e-1], datatype = float)

k__plot := proc (beta, k, t) options operator, arrow; .1544730161*beta*(Sum((-1)^n*exp(-Pi^2*(1+2*n)^2*k*t)*(-(1/2)/sqrt(Pi)+(1/2)*cos((1/2)*(1+2*n)*Pi)/sqrt(Pi)+(1/4)*sqrt(Pi)*(1+2*n)*sin((1/2)*(1+2*n)*Pi))/((2*Pi*n+Pi)*(1+2*n)^2), n = 0 .. infinity))+(-1)*0.1343994407e-1*beta+(-1)*0.6807477066e-1*beta*(9.869604401+8.*(Sum(-exp(-Pi^2*(1+2*n)^2*k*t)/(1+2*n)^2, n = 0 .. infinity))) end proc

plot0 := plot(k__plot(0.78e-1, 3*10^(-2), t), t = 0 .. 8); plots:-display(plot0, plot(`<|>`(X1, Y1), style = point, symbol = solidcircle, color = blue))

Digits := 24; ans := NonlinearFit('k__plot(beta, k, t)', X1, Y1, t, parameterranges = [beta = 0.8e-1 .. 0.89e-1, k = 0.3e-3 .. 0.369e-1], output = solutionmodule)

24

ans:-Results(parametervalues); ans:-Results(residualsumofsquares); new := ans:-Results(leastsquaresfunction)

0.623533880327568262975628e-4

plots:-display(plot(new, t = 0 .. 8), plot(k__plot(0.78e-1, 0.6e-1, t), t = 0 .. 8, style = point, symbol = diagonalcross, adaptive = false, numpoints = 20), plot(k__plot(0.78e-1, 0.3e-1, t), t = 0 .. 8, style = point, symbol = diagonalcross, color = green, adaptive = false, numpoints = 20), plot(`<|>`(X1, Y1), style = point, symbol = solidcircle, color = blue))

ans := NonlinearFit('k__plot(beta, k, t)', X1, Y1, t, parameterranges = [beta = 0.78e-1 .. 0.781e-1, k = 0.3e-1 .. 0.6e-1], output = solutionmodule); ans:-Results(parametervalues); ans:-Results(residualsumofsquares)

0.864031736453623208649908e-4

````

``

Download Nonlinear_Fit_Complex_Equation_acc.mw

restart;

f := (z, n) -> (1-z)^(n-1/2)/(z^(1/2)*Beta(0.5, 0.5+n)):

nJ := (p,c,hi) -> fsolve(n -> Int(unapply(f(z,n),z), 0..p) - c, 0..hi):

ans := nJ(10^(-5), 0.95, 10^6);

                                          5
                     ans := 1.920717307 10 

evalf(Int( unapply(f(z,ans),z), 0..10^(-5) ));

                          0.9499999999

Another way, using the symbolic integration result, and fsolve with a finite range as option.

restart;

f := (z, n) -> (1-z)^(n-1/2)/(z^(1/2)*Beta(1/2, 1/2+n)):

K := int(f(z,n), z=0..p) assuming n>1, p>0;

2*p^(1/2)*hypergeom([1/2, -n+1/2], [3/2], p)/Beta(1/2, 1/2+n)

alt := subs(_dummy=K, (p,c,rng) -> fsolve(_dummy-c, n=rng) ):

CodeTools:-Usage(  alt(10^(-5), 0.95, 0..10^6) );

memory used=12.43MiB, alloc change=4.00MiB, cpu time=93.00ms, real time=94.00ms, gc time=0ns

192071.7307

alt(10^(-2), 0.95, 0..10^3);
alt(10^(-2), 0.15, 0..10^3)

190.8605948

1.512092764

plots:-setoptions(numpoints=20):
plots:-display(
  plot(c->alt(10^(-5), c, 0..10^6), 0..1, adaptive=false)
  ,plot(c->alt(10^(-5), c, 0..10^5), 0..1, adaptive=false)
  ,plot(c->alt(10^(-5), c, 0..10^4), 0..0.3, adaptive=false)
  ,plot(c->alt(10^(-5), c, 0..10^3), 0..0.2, adaptive=false)
  ,plot(c->alt(10^(-5), c, 0..10^2), 0..0.03, adaptive=false)
  , gridlines=false
);

 

Download fsolve_example.mw

And, slightly simpler and more robust,

restart;

f := (z, n) -> (1-z)^(n-1/2)/(z^(1/2)*Beta(1/2, 1/2+n)):

K := int(f(z,n), z=0..p) assuming n>1, p>0;

2*p^(1/2)*hypergeom([1/2, -n+1/2], [3/2], p)/Beta(1/2, 1/2+n)

alt := subs(_dummy=K,
     proc(p,c,endpt::And(numeric,positive):=10^6)
       RootFinding:-NextZero(unapply(_dummy-c,n), 0, 'maxdistance'=endpt);
     end proc ):

CodeTools:-Usage(  alt(10^(-5), 0.95) );

memory used=24.82MiB, alloc change=4.00MiB, cpu time=233.00ms, real time=204.00ms, gc time=63.78ms

192071.7306

alt(10^(-5), 0.99999, 10^7);

975565.7618

alt(10^(-2), 0.95);
alt(10^(-2), 0.15)

190.8605948

1.512092763

plot(c->alt(10^(-5), c), 0..1, adaptive=false, gridlines=false);

 

Download RF_example.mw

Here are a few choices of format involving RealRange, if you don't just want to get the easy representation of {b<>2} by calling solve(det<>0) .

A := <1, 1, -2, -3|-1, -9, b, 11|-1, 3, 2, -1|b, -10, -4, 6>:

det := LinearAlgebra:-Determinant(A);

-8*b^2+32*b-32

b :: `union`( solve( Or(det>0, det<0), b ) );

b::(`union`(RealRange(-infinity, Open(2)), RealRange(Open(2), infinity)))

S := {solve( {Or(det>0, det<0)} )};

{{2 < b}, {b < 2}}

T := (Or@op)( map( And@op, S ) );

Or(And(2 < b), And(b < 2))

convert( T, RealRange);

Or(b::(RealRange(Open(2), infinity)), b::(RealRange(-infinity, Open(2))))

 

Download solve_example.mw

There is no such options as size for 3D plotting commands, and hence it is not accepted by the plots:-setoptions3d command.

However, the ROOT substructure which is used for 2D plots can in fact be inserted into a PLOT3D structure, and the GUI will repsect it.

Below is a command which will insert such a ROOT substructure into a PLOT3D structure, and resize it.

Since the Library commands don't expect to encounter such a substructure within a PLOT3D structure, you may need to ensure that this is the last operation done prior to displaying the plot. Eg, you may wish to apply it only after combining multiple plots using plots:-display, or applying plottools:-transform and friends to the original 3D plot.

You can always remove such a substructure with subsindets, since that substructure is a ROOT function call.

Note that the ROOT substructure modifies the plotting window's size, but does not rescale or zoom the plot. The routine I've provided allows width and height to be different, but in practice you'll find that only square dimensions (ie, m=n) produces something useful.

This functionality to specify the size of 3D plots has been on my wishlist for quite a while now. But I feel that a true solution would also consist of functionality to set the scaling factor (zoom) as well as custom aspect ratios for constrained 3D plots (which would need entirely new items allowed in the PLOT3D sustructure). For 2D plots all that is naturally obtained by just the dimensions, but for 3D plots all those features are related but distinct.

restart;

SetSize3D:=proc(P3D::specfunc(anything,{PLOT3D}),
                m::And(positive,numeric),
                n::And(positive,numeric))
  local R, P, P3Dnew;
  P := plot([[0,0]], ':-size'=[m,n]);
  R := indets(P, 'specfunc(anything,:-ROOT)')[1];
  P3Dnew := subsindets(P3D, 'specfunc(anything,:-ROOT)', ()->NULL);
  op(0,P3Dnew)(op(P3Dnew), R);
end proc:

Q := plot3d(sin(x)*y^2, x=-Pi .. Pi, y=-1 .. 1):
Q;

Qnew := SetSize3D(Q, 200,200);

SetSize3D(Qnew, 700, 700);

SetSize3D(Qnew, 300, 450);

SetSize3D(Qnew, 700, 300); # left-click on it to see that the box is wider

 

Download SetSize3D.mw

Sometimes it is convenient to pass multiple arguments via one or more Arrays (or Vectors), if you are going to be calling it several times, with manual typing.

This can make calling the procedure shorter to type, and less work if only a few entries get changed between calls. Eg,

Params[45] := newvalue:
Params[46] := anothervalue:
Func( Params );

Of course it's becomes necessary to document adequately the role of each Array entry. Stay organized, as that will pay off.

And you can use a few such Array parameters, separating them in various ways: by type (integers vs floats vs others), or by purpose. You can also have some additional parameters still be passed as scalar, or via positional or keyword options.

This approach can become awkward if you want to program a sequence of calls to the procedure, and one of the Array entries is varying.

You mentioned that your worksheet has 100 or more parameters. I would not be surprised if there would be benefits to doing the computations using several procedures, and those might not all require all the parameters to be passed. A reasonable amount of focus on an effective design of the computational flow usually pays off.

The background option for 2D plots was introduced in Maple 18.

So if really do have Maple 16 (and not Maple 2016, which later) then one way to get a curve plotted over an image involves first turning the image into a densityplot.

You didn't say whether you wanted to re-scale the sine curve, vertically, if the image was not in the same proportions as the x- and y-ranges. Or vice versa. So I chose the former. And it wasn't clear whether you wanted to display axes, whose tickmarks might match the sine values and x-y data.

restart:

kernelopts(version);

`Maple 16.01, X86 64 LINUX, May 6 2012, Build ID 744592`

(1)

fn:=cat(kernelopts(mapledir),"/data/tree.jpg"):

P:=ImageTools:-Read(fn):
P:=ImageTools:-Scale(P, 0.5):

m,n := ImageTools:-Width(P),ImageTools:-Height(P);

C:=ArrayTools:-Alias(P,[m*n,3]):

ArrayTools:-DataTranspose(C,n,m,1):

img:=ArrayTools:-Alias(C,[m,n,3]):

img:=ArrayTools:-FlipDimension(img,2):

# Create a dummy density plot, whose data we'll replace with that from the image.

Pimage:=plots:-densityplot(1,x=1..m,y=1..n,axes=none,scaling=constrained,
                           restricttoranges=true,

                           style=patchnogrid,gridlines=false,grid=[m,n]):

Pimage:=subsindets(Pimage,specfunc(anything,COLOR),z->COLOR(RGB,img)):

200, 133

(2)

a,b := 0, 2*Pi;
Psin := plot(sin(x), x=a..b):

0, 2*Pi

(3)

hi,lo := [max,min](op([1,1],Psin)[..,2])[]: # or just use -1 and 1

 

Newsin:=plottools:-transform((x,y)->[(x-a)*(m-1)/(b-a)+1,(y-lo)*(n-1)/(hi-lo)+1])(Psin):

plots:-display( Pimage, Newsin );

 

 

Download background_maple16.mw

Look inside the Startup Code Region of this first attachment.  From the main menubar choose items Edit -> Startup Code (Ctrl-Shift-E).

MaplePlayerSlideShowTest_ac.mw

Alternatively, you could make the calls to DocumentTools:-SetProperty be full and explicit, in the action code of the Buttons. And you could also form their action strings with Plot1 explicitly replacing id. In this next attachment there is no need for Startup Code.

MaplePlayerSlideShowTest_ac_2.mw

You forgot to give numeric values for A and `&vartheta;` .

Are you trying to get y-values for the curves at a common set of sigma values?

That is possible for the upper four curves, since they start (or finish) at the same value of sigma.

It's not clear what meaning it would have to conjoin the lower curves, since they start at different values of sigma. Please explain what you hope to represent by the 1001x4 Matrix, for those lower curves.

fig10_ac.mw

f := (x, y) -> sin(x)*cos(y):

plot3d(f, -Pi .. Pi, -2*Pi .. 2*Pi,
       style = surfacecontour,
       labels = ["x","y","f"]);

Or, with your own custom choice of contour values (heights),

plot3d(f, -Pi .. Pi, -2*Pi .. 2*Pi,
       style = surfacecontour,
       contours = [-0.5, 0.0, 0.5]);

I consider setting UseHardwareFloats to true as a top-level initialization to be highly dubious.

The default setting is deduced, which means that if Digits<=15 then most Library procedures which take into account such things will already treat deduced like true. You're not getting a whole whack more computations being done with hardware floats with true than you would with deduced, provided that Digits<=15.

Setting it to true will prevent some procedures from working with higher precision software floats, even when they decide either that they should or that they need to for correctness.

If you want scalar floats to be HFloats for some computations, or to always be HFloats when pulled out of datatype=float[8] rtables, then restrict the forced UseHardwareFloats setting to within a procedure (as Joe mentioned, environment variables don't affect the higher scope, by design). Or put option hfloat on such procedures.

If the only big effect of setting UseHardwareFloats:=true was to speed up calculations by doing it all in hardware double precision then it would be Maplesoft's default. But it's not. That setting hobbles the system, understandably IMO.

 

restart;

dsys := {diff(y(t),t)=-y(t-1),y(0)=2}:

dsn := dsolve(dsys, numeric, output=listprocedure):

yfun := eval(y(t), dsn):

yfun(0.0);

HFloat(2.0)

yfun(1.4);

HFloat(-0.6399999629131263)

plot([D[1](yfun)(t), -yfun(t-1)], t=0 .. 6,
     style=[line,point], gridlines=false,
     symbolsize=15, symbol=solidcircle,
     adaptive=false, numpoints=21);

 

Download dde.mw

r := 1.9;
                            r := 1.9
i := trunc(r);
                             i := 1
First 169 170 171 172 173 174 175 Last Page 171 of 340