acer

32303 Reputation

29 Badges

19 years, 309 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

The purpose of the special code about which your's asking is to have a call to procedure h return unevaluated (ie. as the same call, itself) in the case that both its arguments are not numeric.

Put briefly, it was done so that the call h(a,b) simply returns as itself, unevaluated, when a and b are merely unassigned, symbolic names rather than actual numeric values.

If h is not set up that way then the calls like h(a,b) -- that get invoked by the call BC2(a,b) -- would induce an error in dsolve/numeric.

Your call to implicitplot contains the call BC2(a,b) in the list that is passed as its first argument. That will get evaluated up-front -- in Maple's usual manner of handling calls to procedures/commands -- before implicitplot starts replacing the names a and b with actual numbers.

Below I show you that -- with your original variant -- the call BC2(a,b) returns with unevaluated calls like h(a,b) (ie. with a and b as symbolic names as its arguments).

Without the special code part of h (about which you're asking) that call BC2(a,b) would throw an error (because h(a,b) in turn would throw an error) before implicitplot even saw its arguments or had any opportunity to start replacing names a and b with actual numbers.

But first I'll show immediately below a variant in which that special construction of h is not needed. I get around the difficulties by using a so-called operator-form calling sequence to the implicitplot command. In this case implicitplot's argument consists of operators/procedures, and BC2 does not get called until its arguments are actually supplied by -- implicitplot's internals -- as numeric values.

restart;

 

ode := diff(y(x),x,x) + (y(x))^2*diff(y(x),x) + cos(y(x)) = x:

 

h:=proc(a,b)
  local F;
  Digits := 10;
    F:=dsolve({ode,y(0)=a,D(y)(0)=b},y(x),numeric);
    [rhs(F(1)[2]),rhs(F(1)[3])];
end proc:

 

BC1 := proc(a,b)
  (1 + a)*b - 3*a
end proc:
BC2 := proc(a,b)
  (1 + h(a,b)[1])*h(a,b)[2] - 3*h(a, b)[1] - h(a,b)[1]^2 + 0.5
end proc:

 

# This call produces an error.

h(a,b);

Warning, The use of global variables in numerical ODE problems is deprecated, and will be removed in a future release. Use the 'parameters' argument instead (see ?dsolve,numeric,parameters)

Error, (in F) parameter 'a' must be assigned a numeric value before obtaining a solution

#
# This call produces an error, because it calls h(a,b)
# which we just saw as throwing an error.
#
# But we can avoid using that call BC2(a,b) (with symbolic
# arguments `a` and `b`) by using an operator-form
# calling sequence in the first argument to `implicitplot`.
#

BC2(a,b);

Warning, The use of global variables in numerical ODE problems is deprecated, and will be removed in a future release. Use the 'parameters' argument instead (see ?dsolve,numeric,parameters)

Error, (in F) parameter 'a' must be assigned a numeric value before obtaining a solution

#
# In this variant we don't pass BC2(a,b) directly
# to implicitplot. Instead we wrap that in an operator.
#
# Notice that the 2nd and 3rd arguments are now pure ranges,
# and not a=range, b=range
#

plots:-implicitplot([(a,b)->BC1(a,b),(a,b)->BC2(a,b)], -5..1, -11..7,
  color=[black,red], legend=["BC1(a,b)=0","BC2(a,b)=0"], size=[300,300]);

#
# And now your original.
#
restart;

ode := diff(y(x),x,x) + (y(x))^2*diff(y(x),x) + cos(y(x)) = x:

h:=proc(a,b)
  local F;
  Digits := 10;
  if type(a,numeric) and type(b,numeric) then
    F:=dsolve({ode,y(0)=a,D(y)(0)=b},y(x),numeric);
    [rhs(F(1)[2]),rhs(F(1)[3])];
  else 'h'(a,b);
  end if;
end proc:

BC1 := proc(a,b)
  (1 + a)*b - 3*a
end proc:
BC2 := proc(a,b)
  (1 + h(a,b)[1])*h(a,b)[2] - 3*h(a, b)[1] - h(a,b)[1]^2 + 0.5
end proc:

#
# This call produces no error, since here a call to `h`
# is defined to return unevaluated when its arguments
# are not both numeric (eg. symbolic names).
#
# So we can use this expression directly in the first
# argument passed to implicitplot.
#

BC2(a,b);

(1+h(a, b)[1])*h(a, b)[2]-3*h(a, b)[1]-h(a, b)[1]^2+.5

plots:-implicitplot([BC1(a,b),BC2(a,b)], a=-5..1, b=-11..7,
  color=[black,red], legend=["BC1(a,b)=0","BC2(a,b)=0"], size=[300,300]);

Download Mikey_01.mw

The following attachment is as much as I believe can be recovered from your zipped .bak file.

STAT_131_Introduction_to_Prob_acc.mw

There might be two missing (unrecoverable) Inputs, in section "Day 14", between the line with "bivariate random vector" and the line with "Joint distribution functions".

I suggest that you upgrade to Maple 2023, if available to you. It seems to have fewer incidences of this kind of problem.

You can make the animation from your original "Test" collection, by passing the extra option insequence to your call to the plots:-display command.

Or you can account for the fact that plots:-animate passes a float value for i, by turning that into an integer prior to the indexing into your Matrix data.

Note. By default animate uses 25 frames. You could also supply the option frames=18 if you want animate to generate exactly 18 frames for i=1..18. That would make the two approaches produce the same animation. But it'd still pass i as a float, so you'd still need to trunc/floor/etc to get an integer. This option also provides an easy way to slow it down -- ie. duplicating frames by using a higher frames value, eg. multiples of 18 here, say.

restart

with(plottools, line); with(plots)

Trvl := Matrix(5, 18, {(1, 1) = 0, (1, 2) = 1, (1, 3) = 2, (1, 4) = 3, (1, 5) = 4, (1, 6) = 5, (1, 7) = 6, (1, 8) = 7, (1, 9) = 8, (1, 10) = 9, (1, 11) = 10, (1, 12) = 11, (1, 13) = 12, (1, 14) = 13, (1, 15) = 14, (1, 16) = 15, (1, 17) = 16, (1, 18) = 17, (2, 1) = 25.00000000, (2, 2) = 25.62348980, (2, 3) = 26.24697960, (2, 4) = 26.87046941, (2, 5) = 27.49395921, (2, 6) = 28.11744901, (2, 7) = 28.81795052, (2, 8) = 29.70796779, (2, 9) = 30.69482490, (2, 10) = 31.66997559, (2, 11) = 32.64189447, (2, 12) = 33.63901734, (2, 13) = 34.59934793, (2, 14) = 35.46317753, (2, 15) = 36.17679740, (2, 16) = 36.69583812, (2, 17) = 36.98802824, (2, 18) = 37.03520080, (3, 1) = 9.50000000, (3, 2) = 10.28183148, (3, 3) = 11.06366296, (3, 4) = 11.84549445, (3, 5) = 12.62732593, (3, 6) = 13.40915741, (3, 7) = 14.11748988, (3, 8) = 14.56318511, (3, 9) = 14.69313898, (3, 10) = 14.49329251, (3, 11) = 14.26929209, (3, 12) = 14.29271187, (3, 13) = 14.56209574, (3, 14) = 15.06069470, (3, 15) = 15.75750830, (3, 16) = 16.60921202, (3, 17) = 17.56285107, (3, 18) = 18.55913279, (4, 1) = 26.55872458, (4, 2) = 27.18221431, (4, 3) = 27.80570411, (4, 4) = 28.44091912, (4, 5) = 29.26771501, (4, 6) = 30.27132826, (4, 7) = 31.26754127, (4, 8) = 32.19797655, (4, 9) = 33.15526524, (4, 10) = 34.16855668, (4, 11) = 35.08363639, (4, 12) = 35.87210441, (4, 13) = 36.48493760, (4, 14) = 36.88403298, (4, 15) = 37.04457671, (4, 16) = 36.95658698, (4, 17) = 36.62553455, (4, 18) = 36.06730316, (5, 1) = 11.45457880, (5, 2) = 12.23641019, (5, 3) = 13.01824167, (5, 4) = 13.79066482, (5, 5) = 14.38908368, (5, 6) = 14.67833203, (5, 7) = 14.61699479, (5, 8) = 14.33989949, (5, 9) = 14.25015666, (5, 10) = 14.40907532, (5, 11) = 14.80585186, (5, 12) = 15.41668792, (5, 13) = 16.20360467, (5, 14) = 17.11767545, (5, 15) = 18.10206776, (5, 16) = 19.09557685, (5, 17) = 20.03643114, (5, 18) = 20.86416514})

display(seq(line([Trvl[2, i], Trvl[3, i]], [Trvl[4, i], Trvl[5, i]], color = green), i = 1 .. 18), insequence)

animate(proc (i) options operator, arrow; line([Trvl[2, i], Trvl[3, i]], [Trvl[4, i], Trvl[5, i]], color = green) end proc, [trunc(i)], i = 1 .. 18)

NULL

Download 23-11-03_Q_Animate_a_line_ac.mw

It's clear from your Questions picture, and the attachment's filename, that you are trying to Read an image stored in your Maple workbook.

With a short edit to your this URI the following zipped workbook succeeds for me using Maple 2023.1.

TestTrans_ac.zip

The following is as much as I believe can be recovered here. (Three input equations were corrupted; somewhat near the beginning.)

Hjemmeopgave_7_igen_ac.zip

I suggest that you upgrade to Maple 2023, if available to you. It seems to have fewer incidences of this problem.

You can use your desired ranges for x and t in the calls to contourplot, along with scaling by Mx and N respectively. And you can also use the desired ranges directly in the (density-plot) PLOT call.

 

restart;

 

Digits := 20:

 

with(plots): with(CurveFitting,ArrayInterpolation):
 

 

N := 20: Mx := 20: L := 1: `&Delta:x` := L/Mx: T := 2: `&Delta:t` := T/N:

 

for i from 0 while i <= Mx do u[i, 0] := 0 end do:
for n from 0 while n <= N do u[0, n] := 0: u[Mx, n] := 0 end do:
for n from 0 while n <= N-1 do
  for i while i <= Mx-1 do
    Ru[i, n] := eval((u[i, n+1]-u[i, n])/`&Delta:t` = (u[i+1, n+1]-2*u[i, n+1]+u[i-1, n+1])/`&Delta:x`^2-u[i, n+1]+.5);
  end do;
  Sol[n] := fsolve({seq(Ru[i, n], i = 1 .. Mx-1)});
  assign(op(Sol[n]));
end do:
 

 

Digits := 10: NP := 100:
XX := [seq(seq(i1/Mx, i1 = 0 .. Mx), i2 = 0 .. N)]:
TT := [seq(seq(i2, i1 = 0 .. Mx), i2 = 0 .. N)]:
ZZ := [seq(seq(u[i1, i2], i2 = 0 .. N), i1 = 0 .. Mx)]:

 

interfunc := subs(__M = Matrix(Matrix(Mx+1, N+1, ZZ), datatype = float[8]),
  (x, y) -> ArrayInterpolation([[`$`(i1, i1 = 0 .. Mx)], [`$`(i2, i2 = 0 .. N)]],
                               __M, [[x], [y]], method = cubic)[1, 1]):

 

newz := ArrayInterpolation([[`$`(i1, i1 = 0 .. Mx)], [`$`(i2, i2 = 0 .. N)]],
                           Matrix(Mx+1, N+1, ZZ, datatype = float[8]),
                           [[seq(Mx*(i3-1)/(NP-1), i3 = 1 .. NP)],
                            [seq(N*(i3-1)/(NP-1), i3 = 1 .. NP)]], method = cubic):

 

nminz, nmaxz := (simplify@fnormal@min, max)(newz):

 

C := .666*(1-ImageTools:-FitIntensity(newz)):

 

PC := PLOT(GRID(0 .. 2, 0 .. 2, newz, COLOR(HUE, C)), STYLE(PATCHNOGRID)):

 

numcontours := 15:

 

PP := display(PC,
   contourplot((x,t)->interfunc(Mx/2*x,N/2*t), 0 .. 2, 0 .. 2, grid=[101,101],
               thickness = 0, contours = [seq(nminz+(nmaxz-nminz)*(i3-1)/(numcontours+2-1),
                                              i3 = 1 .. numcontours+1)]),
   seq(plot(ZZ[1], nminz .. nminz, thickness = 15, color = COLOR(HUE, .666*(1-i3/(numcontours+1))),
   legend = sprintf(" %.3f", nminz+i3*(nmaxz-nminz)/(numcontours+1))), i3 = numcontours+1 .. 0, -1),
   legendstyle = [location = right, font = [Helvetica, 14]], font = [Helvetica, 16],
   labelfont = [Helvetica, bold, 16], labels = [x, t], labeldirections = [horizontal, vertical]):
 

 

display(PP, size = [500, 400]);

 

 

display(subs(THICKNESS(0)=THICKNESS(0.5),
             THICKNESS(15)=THICKNESS(35),PP), size = [720, 640]);

 


I also made a few changes for (IMO) a better look, such as getting rid of the minus sign on "-0.000", increasing the contourplot grid, etc.

Also, one way to get a nicer final export is to increase the plot size, since you can then scale down using a good external image processing program. I also made the contours thinner using subs, since IIRC Maple 2015's contourplot didn't allow that thickness (thinner than thickness=0 !) 

This direct approach can be more efficient than using plottools:-transform to re-scale/shift the original result (after the fact).

I think that it's often easier to manage in the long run if your calls/functions are properly scaled up front, instead of fudging the tickmarks later on. (I choose forced tickmarks only as last resort, usually. It doesn't work with all later transformations, as one reason.)

By the way, I suggest exporting the plot from Maple in .PNG format. Well, almost anything except .JPG, for which the results are generally poor.

If you want advice about exporting the data then you should describe the required format in explicit and adequate detail. Merely mentioning a product's name, like "tecplot", is not adequate.

ps. Quite a bit of this code looks like bits that I'd written. Is that right? I'm curious now; where did you get it from?

Since you gave a followup example (elsewhere),
 

with(Statistics):

L1:=[2,4,6]:

ColumnGraph(L1,
            axis[1]=[tickmarks=[[.4="abe",1.4="banan",2.4="palme"],
                     rotation=Pi/4]]);

Download jestrup1.mw

This is as much as I could recover. I think it is most of it.

I left all the sections unexpanded.

prac_exam_ece4179_acc.mw

There may be a missing equation just after the line with "evaluate for the given points" in section 1, and another just after "This decision boundary will realize" in section 4.

I notice that you were using Maple 2022.2.  I suggest upgrading to Maple 2023 if available to you; it seems to have fewer occurences of this problem.

Your attempts at forming a Matrix try to use Ans[k], but your loop simply assigns (repeatedly) to just Ans. It looks like you meant to assign to Ans[j] and Ans1[j] in the loop.

In your systems of equations you have terms like Theta(eta,t), but in your attempts at forming a Matrix you have the arguments of Theta reversed, like Theta(0.5,eta). Did you mean Theta(eta,0.5) instead? The same happens for the other functions.

When you write,
   eval(diff(Theta(0.5,eta),eta),Ans[k])(0)
are you wanting instead to take from Ans[k] the derivative of Theta(eta,0.5) w.r.t. eta, and then evaluate that very close to eta=0? If so then this is essentially the same as your query in your previous Question. You are mistakenly using technique appropriate for a result from dsolve(...,numeric) rather than from pdsolve(...,numeric). Have a look at this: error_in_table_value_ac.mw

You could avoid duplication of conputation by computing the Matrix entries (involving the other terms that I omitted) in a loop.

Are you trying to get something like the following?

restart

randomize()

M := proc () local A, B, operation, str; A := RandomTools:-Generate(integer(range = -5 .. 5, exclude = {0})); B := RandomTools:-Generate(integer(range = -5 .. 5, exclude = {0, A})); operation := combinat:-randcomb([`%+`, `%-`, `%*`], 1)[]; str := sprintf("%a(%a,%a)", operation, A, B); InertForm:-Display(InertForm:-Parse(str), 'inert' = false) = value(operation(A, B)) end proc

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

M()

0, "%1 is not a command in the %2 package", _Hold, Typesetting

Download Qop2_ac.mw

Those are actually equations, and you could use or access their lhs and rhs separately.

Note that formation of the string may not be strictly necessary, compared with simply calling the operation on the arguments directly to get the inert expression. (But there seemed to be a few corner cases, where using the inert operators with prefix form helped.) Did you really need the string representation, using infix form, or is the above result from InertForm:-Display enough?

I notice that you had B set up to avoid being A. Is that because you didn't want A-B to produce 0? If so perhaps you'd also want to avoid A+B=0 when B=-A? And perhaps you'd prefer neither A nor B to be -1 or 1 when multiplying?  Qop2_ac2.mw

It sounds as if your preferences were not migrated to your new installation. (There's usually a dialogue that asks about that, during installation.)

You can set the preference by using the main menubar,

   Tools->Options->Display

and then set the drop-menu box "Input display" to "Maple Notation".  Then press the "Apply Globally" button to set it as default.

@zenterix You could use the terser,

   m::{Matrix,list(Matrix)}

instead of using `Or` in the type specification.

The LinearSolve command in the LinearAlgebra package can handle this.

Let's write out the coefficients (of x) for your polynomials, as column Vectors. Form a Matrix of the ones from S.

Then call LinearSolve.

restart;

with(LinearAlgebra,LinearSolve):

 

S:={x+4, 3*x-7}: f:=-5*x+10:

T := max(degree~(S,x))+1:

A:=Matrix(T,nops(S),(i,j)->coeff(S[j],x,i-1));

Matrix(2, 2, {(1, 1) = 4, (1, 2) = -7, (2, 1) = 1, (2, 2) = 3})

B:=Vector(T,(i)->coeff(f,x,i-1));

Vector(2, {(1) = 10, (2) = -5})

LinearSolve(A,B);

Vector(2, {(1) = -5/19, (2) = -30/19})

 

SC := {x+4, 2*x+8}:

T := max(degree~(SC,x))+1:

AC:=Matrix(T,nops(SC),(i,j)->coeff(SC[j],x,i-1));

Matrix(2, 2, {(1, 1) = 4, (1, 2) = 8, (2, 1) = 1, (2, 2) = 2})

LinearSolve(AC,B);

Error, (in LinearAlgebra:-LinearSolve) inconsistent system

 

S:={x^2+x+4, 7*x^2+3*x, x+1}: f:=-5*x+10:

T := max(degree~(S,x))+1:

A:=Matrix(T,nops(S),(i,j)->coeff(S[j],x,i-1));

Matrix(3, 3, {(1, 1) = 1, (1, 2) = 0, (1, 3) = 4, (2, 1) = 1, (2, 2) = 3, (2, 3) = 1, (3, 1) = 0, (3, 2) = 7, (3, 3) = 1})

B:=Vector(T,(i)->coeff(f,x,i-1));

Vector(3, {(1) = 10, (2) = -5, (3) = 0})

LinearSolve(A,B);

Vector(3, {(1) = -15/2, (2) = -5/8, (3) = 35/8})

Download LS_ex2.mw

note: You wrote, "corresponding linear system", which indicates that you recognize that this problem can be expressed as a linear system. Using LinearSolve might then not be unexpected. Reformulating the polynomial relationships as a linear system, programmatically, might then be useful for you.

Notice that you were using the %d format code for this item, in printf. That's for integers. But you mentioned wanting them as 1.0,1.2,...2.0, which are floats. So you could use the %2.1f format, in printf.

Now, you already have the float values you mentioned wanting, available directly via t[i] .

If you didn't already have them using t[i] then you could also get n values evenly spaced between a and b by using the formula,
   a + (b-a)*(i-1)/(n-1)
which for this example would be,
   1 + (2-1)*(i-1)/(n-1)
which can be shortened to,
   1 + (i-1)/(n-1)

restart;

actual:=<2,3.5136,5.8016,9.1136,13.7376,20>:
t:=<1,1.2,1.4,1.6,1.8,2>:
n:=6:

P:=Statistics:-LinearFit([1,x],t,actual,x):

printf("t    Actual P  Model P  Error\n");
for i from 1 to n do

   model:=subs(x=t[i],P):
   err:=actual[i]-model:

   #printf("%2.1f %7.4f% 10.4f% 9.4f\n",
   #        1+(i-1)/(n-1),actual[i],model,err);

   printf("%2.1f %7.4f% 10.4f% 9.4f\n",
          t[i],actual[i],model,err);

end do:

t    Actual P  Model P  Error
1.0  2.0000    0.1717   1.8283
1.2  3.5136    3.7141  -0.2005
1.4  5.8016    7.2565  -1.4549
1.6  9.1136   10.7989  -1.6853
1.8 13.7376   14.3413  -0.6037
2.0 20.0000   17.8837   2.1163

Download frm.mw


For future programming, you might also care to think about how the following work. These kinds of construction occur pretty often in programming. It can be a useful thing to get familiar with.

<seq(1.0..2.0,0.2)>;
n:=6:
<seq(1.0..2.0,numelems=n)>;
<seq(1.0..2.0,(2-1)/(n-1))>;
<seq(1.0+(i-1)/(n-1),i=1..n)>;
Vector(n,i->1.0+(i-1)/(n-1));

You are using module locals like Chevron2A as the keys of some of your tables.

That will prevent you from accessing table entries if you subsequently try and use (say) the global names like Chevron2A as the key, trying that access outside the module.

You could, instead, use global name :-Chevron2A as the table key when creating the entry. Then you can use that as key, for access elsewhere. [edit] I would prefer this, over a module export. Using strings is also a good possibility, as Carl has mentioned after I posted this Answer. If you use the global name as key then you ought to quote it (and protect from evaluation), in case it is also assigned some value, eg. ':-Chevon2A'.

Or you could instead declare Chevron2A as an export of the module. Then, later, you could use it as key in its long-form (fully qualified name),  calculateblahcubitblah:-Chevron2A. Or you could make the module a package, so after doing with(calculateblahcubitblah) its exports become the new bindings for reference via the short-form Chevron2A. [edit] This is not my favorite choice, and I can only think of convoluted scenarios in which it'd provide extra contextual usefulness.

The code snippets that you showed above indicate that this problem that I've described occurs in your code. I cannot tell whether you also have other issues, eg. last-name-eval of tables assigned to names. I suggest using eval(nameoftable) when putting into lists or if returning them straight from procedures.

First 34 35 36 37 38 39 40 Last Page 36 of 336