acer

31443 Reputation

29 Badges

19 years, 135 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

You can save all the assigned names (including just user-defined names), but there's no easy way to save&restore all internal state including memoized results in remember tables and caches.

That's not to say that it's generally impossible (for, say, an expert), especially for Library routines. However there's no stock command for it.

Saving/restoring all the remembered results in all kernel built-ins (eg. evalf, series, etc) might be much harder and perhaps not possible.

Since there are several flavors to this, it would be better if you supplied a concrete, representative example.

Your eval calls attempt to replace names like f__1, f__2, etc, but your expression M actually contains f[1], f[2], etc.

plot_a_ac.mw

Here are a few ways, which you might adjust with options, etc.

My "trick" is to create a system for each param1 value, instead of trying to use it as a DS TransferFunction `parameter` in that routine's technical sense.

[edit: I added plots:-animate]

simple_bode_ac.mw

Are any of these the kind of thing you might be after?

If not, could you provide an example, with input, output, and desired "order" answer? (A worksheet might be best.)

restart;

A := asympt(exp(x^2)*(1-erf(x)),x);

1/(Pi^(1/2)*x)-(1/2)/(Pi^(1/2)*x^3)+(3/4)/(Pi^(1/2)*x^5)+O(1/x^7)

order(series(A,x));

-7

degree(op(indets(A,specfunc(O))[1]),x);

-7

B := MultiSeries:-series(eval(exp(x^2)*(1-erf(x)),x=1/x),
                         x,5):
eval(B,x=1/x);

1/(Pi^(1/2)*x)-(1/2)/(Pi^(1/2)*x^3)+(3/4)/(Pi^(1/2)*x^5)+O(1/x^7)

order(B);

7

Download asympt_order_q.mw

Using the global (top-level) variable Metric is quite poor.

For one thing, since a Matrix is a mutable data structure then some entry in the toplevel :-Metric might change. And then any already computed invMetric would be wrong. So every call to your module's computational routines would sensibly need to compare entries between :-Metric and some copy (so as to know whether to recompute invMetric. Yikes.

A more usual approach (without "resorting" to Objects) is to have an exported utility which sets both the stored local Metric and its inverse. Then you would simply invoke that where you would previously have redefined :-Metric in your earlier scheme of things.

restart

``

TM := module () local Metric, invMetric; export foo, bar, SetMetric;  Metric := Matrix(3, shape = symmetric, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]); invMetric := LinearAlgebra:-MatrixInverse(Metric); SetMetric := proc (M::(Matrix(3))) if not EqualEntries(Metric, M) then print("updating Metric"); Metric := copy(M); invMetric := LinearAlgebra:-MatrixInverse(Metric) end if; return NULL end proc; foo := proc () print('Metric' = Metric) end proc; bar := proc () print('invMetric' = invMetric) end proc end module

TM:-foo()

Metric = Matrix(%id = 36893627907715573268)

TM:-bar()

invMetric = Matrix(%id = 36893627907715573868)

TM:-SetMetric(Matrix(3, 3, {(1, 1) = 1, (1, 2) = 0, (1, 3) = 0, (2, 1) = 0, (2, 2) = 1, (2, 3) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = -2}))

"updating Metric"

TM:-foo()

Metric = Matrix(%id = 36893627907715551708)

TM:-bar()

invMetric = Matrix(%id = 36893627907715552068)

Same Matrix, no need to update

TM:-SetMetric(Matrix(3, 3, {(1, 1) = 1, (1, 2) = 0, (1, 3) = 0, (2, 1) = 0, (2, 2) = 1, (2, 3) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = -2}))

TM:-foo()

Metric = Matrix(%id = 36893627907715551708)

TM:-bar()

invMetric = Matrix(%id = 36893627907715552068)

TM:-SetMetric(Matrix(3, 3, {(1, 1) = 1, (1, 2) = 0, (1, 3) = 0, (2, 1) = 0, (2, 2) = 1, (2, 3) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = -1/2}))

"updating Metric"

TM:-foo()

Metric = Matrix(%id = 36893627907715532428)

TM:-bar()

invMetric = Matrix(%id = 36893627907715532788)

NULL

Download 2024-12-30_Q_Module_Global_and_Local_ac.mw

Here's a somewhat crude use of dsolve's events option to get the points where the diff(x(t),t) hits zero. It could be made fancier.

If you reduce T then it finds less data point and runs faster.

restart

Settings(typesetdot = true)

eqn1 := (M+m)*(diff(x(t), `$`(t, 2)))+c__b*(diff(x(t), t))+m*l*(diff(varphi(t), `$`(t, 2))-(diff(varphi(t), t))^2*varphi(t))+K*x(t) = f*cos(phi)

eqn2 := m*l^2*(diff(varphi(t), `$`(t, 2)))+c__h*(diff(varphi(t), t))+m*l*(diff(x(t), `$`(t, 2)))+m*g*l*varphi(t) = 0

couple := [eqn1, eqn2]

M := 300; m := 200; l := 1; c__b := 300; K := 2000; f := 200; c__h := 0; g := 9.8

couple

[500*(diff(diff(x(t), t), t))+300*(diff(x(t), t))+200*(diff(diff(varphi(t), t), t))-200*(diff(varphi(t), t))^2*varphi(t)+2000*x(t) = 200*cos(phi), 200*(diff(diff(varphi(t), t), t))+200*(diff(diff(x(t), t), t))+1960.0*varphi(t) = 0]

T := 50000; `ω__n` := 10

50000

10

`ω__v` := `ω__n`*t/T; phi := int(%, t)

ics5 := x(0) = 0.1e-3, (D(x))(0) = 0, varphi(0) = 0.1e-3, (D(varphi))(0) = 0

solu12:=dsolve({couple[],ics5},[x(t),varphi(t)],output = listprocedure, numeric,method=rkf45,
               events=[[diff(x(t),t), halt]],
               maxfun =-1):

interface(warnlevel=0):
str := time[real]():
TT := 'TT': ii_up:=100000:
for ii from 1 to ii_up do
  solu12(T):
  TT[ii] := eval([1*omega__n*t(last)/T,x(t)(last)],solu12(last));
  try solu12(eventclear); catch: end try;
  if abs(TT[ii][1] - omega__n) < 1e-5 then
    high_ii:=ii; printf("%ld points", high_ii); ii:=ii_up; next;
  end if;
end do:
(time[real]()-str) * 'seconds';

79632 points

103.777*seconds

data := convert(TT,list): numelems(data);
plots:-pointplot(data, symbol=solidcircle, symbolsize=5);

79632

plots:-display(
  plot([seq(data[2*i], i=1..numelems(data)/2)], color=red),
  plot([seq(data[2*i-1], i=1..numelems(data)/2)], color=red)
);

 

 

Download saopinjifen1230_ac.mw

We can also overlay that with the odeplot. Here it is for N=10000 (similar effect for larger N, but it all takes longer). Continuing in that code,

solu12(eventdisable={1}):
p1 := plots:-odeplot(solu12, [omega__v, x(t)], 0 .. T, numpoints=50000, color="Niagara Azure"):
plots:-display(
  p1,
  plot([seq(data[2*i], i=1..numelems(data)/2)], color=red),
  plot([seq(data[2*i-1], i=1..numelems(data)/2)], color=red),
  size=[700,400]
);


saopinjifen1230_ac2.mw

Perhaps you could use WorksheetToMapleText from the Worksheet package.

As an example,

currentdir("/home/harpo/skits");
read "slapstick/boguscalculus.mw";

In other words, if you've set currentdir(...) then you don't have to take the trouble to form the fully qualified location (by, say, concatenation).

That's the main point of being able to set the current working directory; it allows you to use relative paths, without having to form them into absolute paths (wrt the root directory).

ps. As a side note, if also you happen to want to obtain the home directory programmatically, you could issue the Maple command kernelopts("homedir") or if it's set as an environment variable in your OS shell, getenv("HOME") .

Does the command currentdir() return a location which is writable by you, when executed by your script approach?

If not, you could set it to a location writable by you, or try passing the full location in the first argument passed to Export. Eg,
   "C:/Users/JoyDivisionMan/LineDensity.jpg"
or what have you, depending on your OS and account details.

nb. We don't know what Maple version and OS you have.

I changed your Post into a Question.

Here is one way of correcting the several mistakes in use of the dsolve result and printf.

I retained your dsolve usage and odeplots.

 

 

restart; with(plots); _local(gamma)

sys := {diff(i(t), t) = lambda*s(t)-gamma*i(t), diff(r(t), t) = gamma*i(t), diff(s(t), t) = -lambda*s(t)}

ic := {i(0) = 1, r(0) = 0, s(0) = 999999}

gamma := .1

lambda := .2

NULL

sol := dsolve(`union`(sys, ic), numeric)

``

display([odeplot(sol, [t, s(t)], 0 .. 60, color = red), odeplot(sol, [t, i(t)], 0 .. 60, color = blue), odeplot(sol, [t, r(t)], 0 .. 60, color = green)], labels = ["Time", "Number of People"], legend = ["Susceptible", "Infected", "Recovered"])

results := map(proc (u) options operator, arrow; map(proc (uu) options operator, arrow; lhs(uu) = round(rhs(uu)) end proc, u) end proc, [seq(sol(tval), tval = 0 .. 50)])

printf("%-10s %-15s %-15s %-15s\n", "Day", "Susceptible", "Infected", "Recovered"); printf("---------------------------------------------\n"); for entry in results do printf("%-10d %-15d %-15d %-15d\n", eval(t, entry), eval(s(t), entry), eval(i(t), entry), eval(r(t), entry)) end do

Day        Susceptible     Infected        Recovered      
---------------------------------------------
0          999999          1               0              
1          818730          172214          9056           
2          670319          296822          32859          
3          548811          384014          67175          
4          449329          441982          108689         
5          367879          477303          154818         
6          301194          495235          203571         
7          246597          499977          253427         
8          201896          494865          303239         
9          165299          482542          352160         
10         135335          465088          399577         
11         110803          444136          445061         
12         90718           420952          488330         
13         74273           396516          529210         
14         60810           371574          567616         
15         49787           346686          603527         
16         40762           322269          636969         
17         33373           298620          668006         
18         27324           275950          696726         
19         22371           254396          723234         
20         18316           234039          747645         
21         14996           214922          770083         
22         12277           197052          790671         
23         10052           180414          809534         
24         8230            164976          826794         
25         6738            150694          842568         
26         5517            137514          856969         
27         4517            125378          870106         
28         3698            114224          882078         
29         3028            103991          892981         
30         2479            94617           902905         
31         2029            86040           911931         
32         1662            78201           920137         
33         1360            71046           927594         
34         1114            64519           934367         
35         912             58571           940517         
36         747             53154           946099         
37         611             48225           951164         
38         500             43741           955759         
39         410             39664           959926         
40         335             35960           963704         
41         275             32596           967129         
42         225             29541           970234         
43         184             26769           973047         
44         151             24253           975596         
45         123             21971           977905         
46         101             19902           979997         
47         83              18025           981892         
48         68              16324           983608         
49         55              14782           985162         
50         45              13385           986570         

 

Download sir_model_ac.mw

ps. It's potentially confusing to people if you use the Maple 2025.0 Beta release for your attachments here./

You have loaded the VectorCalculus package, which has its own way of displaying Vectors.

If you want to have that package be loaded, but display Vectors in their "usual" (non-VectorCalculus) fashon then you could turn them into "usual" Vectors by using the global :-Vector command. There are also other ways to do what I've done below.

If this is not what you want them could you please explain it in more detail? Thanks.

 

 

restart

with(VectorCalculus)

NULL

V_D2 := `<,>`(-8, -5, 4); V_D3 := `<,>`(-8, 5, 4); V_S2 := `<,>`(6, 0, 5)

Vector(3, {(1) = -8, (2) = -5, (3) = 4})

Vector(3, {(1) = -8, (2) = 5, (3) = 4})

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

:-Vector(V_D2), :-Vector(V_D3), :-Vector(V_S2)

Vector[column](%id = 36893627834423408268), Vector[column](%id = 36893627834423408388), Vector[column](%id = 36893627834423408508)

`<|>`(V_D2, V_D3, V_S2)

Matrix(%id = 36893627834423405012)

`<|>`(V_D2, V_D3, V_S2)^%T

Matrix(%id = 36893627834423400676)

`<,>`(:-Vector[row](V_D2), :-Vector[row](V_D3), :-Vector[row](V_S2))

Matrix(%id = 36893627834423391284)

NULL

Download question_vector_ac.mw

Alternatively, you might not load the VectorCalculus package, and just use individual commands from it to get your Vector-plots, etc.

Your 2D Input call to solve had the following in the second set, even though it appeared to be just A[0] ,

  `__ `*A[0]

If I delete it and retype it as just A[0] then it works fine.

I don't know how you managed to get it entered that way.

NULL

restart

eq0 := -4*alpha*k^2*m^2*n^2*A[0]^2+4*beta*k*m*n^2*A[0]^3-4*gamma*k*m*n^2*A[0]^3+4*delta^2*m*n^2*A[0]^2-4*n^2*sigma*A[0]^4-4*m*n^2*w*A[0]^2 = 0

eq1 := -8*alpha*k^2*m^2*n^2*A[0]*A[1]+12*beta*k*m*n^2*A[0]^2*A[1]-12*gamma*k*m*n^2*A[0]^2*A[1]+8*delta^2*m*n^2*A[0]*A[1]-16*n^2*sigma*A[0]^3*A[1]+2*a*alpha*m*n*A[0]*A[1]-8*m*n^2*w*A[0]*A[1] = 0

eq2 := -4*alpha*k^2*m^2*n^2*A[1]^2+12*beta*k*m*n^2*A[0]*A[1]^2-12*gamma*k*m*n^2*A[0]*A[1]^2+4*delta^2*m*n^2*A[1]^2-24*n^2*sigma*A[0]^2*A[1]^2+a*alpha*m^2*A[1]^2+3*alpha*b*m*n*A[0]*A[1]-4*m*n^2*w*A[1]^2 = 0

eq3 := 4*beta*k*m*n^2*A[1]^3-4*gamma*k*m*n^2*A[1]^3-16*n^2*sigma*A[0]*A[1]^3+alpha*b*m^2*A[1]^2+alpha*b*m*n*A[1]^2+4*alpha*c*m*n*A[0]*A[1] = 0

eq4 := -4*n^2*sigma*A[1]^4+alpha*c*m^2*A[1]^2+2*alpha*c*m*n*A[1]^2 = 0

C := solve({eq0, eq1, eq2, eq3, eq4}, {a, b, c, A[0]})

{a = 4*n^2*(alpha*k^2*m-delta^2+w)/(m*alpha), b = 4*(gamma-beta)*k*n^2*A[1]/((m+n)*alpha), c = 4*n^2*sigma*A[1]^2/(alpha*m*(m+2*n)), A[0] = 0}

NULL

NULL

Download problem_4_ac.mw

You can do it in 2D Input, just not by using the -> keystrokes.


We can in fact enter the procedure f in 2D Input and avoid the
warning message and declare the variable as local.

But it take more typing to do that in 2D Input than in 1D plaintext code.

We can do it in two ways in 2D Input. The second, which prints the
procedure as an "arrow" operator, is longer to type in.

f := proc (x) local j; seq(x, j = 1 .. 5) end proc

proc (x) local j; seq(x, j = 1 .. 5) end proc

f := proc (x) local j; options operator, arrow; seq(x, j = 1 .. 5) end proc

proc (x) local j; options operator, arrow; seq(x, j = 1 .. 5) end proc


It's unfortunate that the above printing of the procedure doesn't
show the local declaration.

(That makes that documentation webpage less clear and helpful.
I will submit a bug report.)

showstat(f)


f := proc(x)
local j;
   1   seq(x,j = 1 .. 5)
end proc
 

Download local_proc.mw

This isn't very significantly different in nature from your earlier Question from Dec 9.

Since the Maple commands which deal with external files accept strings to denote the file name, these can be viewed as queries about programmatic string construction. The following code is more about the construction of the string than it is about the save command.

You can use cat, but sprintf (modeled after that in C/C++) is more flexible.

restart;

Variable := 10;

10

fname := sprintf("Result(%a).txt", Variable);

"Result(10).txt"

M := 4;

4

save M, fname;

 

restart;

read "Result(10).txt";

4

Download sprintf_again.mw

Naturally, if you intend on doing this repeatedly and in a repeating pattern then you could also make a (possibly even 1-liner) re-usable procedure/operator which makes that usage terser.

By default the imaginary unit is captical I in Maple. You can change this to lowercase i with the interface command.

Also, some of the problem inputs had a semicolon terminator before the context-action, resulting in double output.

Also, you don't need to apply the factor command. These products expand and evaluate automatically.

NULLComplex Numbers

interface(imaginaryunit = I)

 

Key Skills 11-48

NULLc11 := 2-3*I+(6+8*I)"(=)"8+5*I

c12 := 4+5*I-8+2*I"(=)"-4+7*I

c13 := -7+2*I+4*I"(=)"-7+6*I

c14 := 3-4*I+(3+4*I) = 6NULL

c15 := -6-5*I-6*I"(=)"-6-11*I

c16 := -10+4*I+2*I"(=)"-10+6*I

c17 := 3*(2-6*I) =

6-18*I

c18 := -4*(2+8*I) =

-8-32*I

NULL

c19 := (2*I)*(2-3*I) =

6+4*I

c20 := (3*I)*(-3+4*I) = -12-9*INULL

c21 := (2+I)*(3-4*I) = 10-5*INULL

c22 := (2-I)*(5+3*I) = 13+INULL

``

 

NULL

Download 1.3-Complex_Numbers_bad_ac.mw

1 2 3 4 5 6 7 Last Page 3 of 327