acer

21438 Reputation

29 Badges

15 years, 116 days

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

Here are a few ideas for inspiration.

The computation of
    n_error[0]:=1.5-newt(1.5);
does not make much sense, if it is intended to mean a comparsion of x[0] and f(x[0]).

The computations of
   nerror[count]:=abs(x[count]-x[count-1]);
make sense, but aren't very meaningful as estimates of error in f.

If you have 10 x-values, then you only have 9 incremental changes. There is no first n_error[0] really (or no last one, depending how you want to write it down).

You might want to end the loop if the x-increment gets too small, or if the f(x) evaluation gets acceptably close to zero.

There's not much benefit in storing rt as well as x.

I'm not sure that DataFrames are the best for the task, or at least I'd suggest that the given Append use is a little awkweard. Here's one way with a DataFrame augmented within the loop, and one without.

restart;

newt := x -> evalf(x - f(x)/D(f)(x)):

f:= x -> x^6-2; # function to analyze

proc (x) options operator, arrow; x^6-2 end proc

x[0] := 1.5;

1.5

xtol,ftol := 1e-10, 1e-10;

0.1e-9, 0.1e-9

DF := DataFrame( <<x[0]|n_error|f_error>> ):
DF[1,2],DF[1,3] := ``, f(x[0]):

for count from 1 to 10 do
   x[count] := newt(x[count-1]);
   xincr := abs(x[count]-x[count-1]);
   fval := abs(f(x[count]));
   DF:=Append(DF,DataSeries(<x[count]|xincr|fval>),mode=row);
until xincr < xtol or fval < ftol:

DataFrame(DF, columns=[x, abs(x_incr), `f `(x)]);

"DataFrame([[[1.5,,9.390625],[1.293895748,0.206104252,2.692407434],[1.170160594,0.123735154,0.567277491],[1.127066579,0.043094015,0.049733650],[1.122508821,0.004557758,0.000500089],[1.122462053,0.000046768,5.0 10^(-8)],[1.122462048,5. 10^(-9),3. 10^(-9)],[1.122462048,0.,3. 10^(-9)]]],rows=[1,2,3,4,5,6,7,8],columns=[x,abs(x_incr),`f `(x)])"

restart;
newt := x -> evalf(x - f(x)/D(f)(x)):
f:= x -> x^6-2: # function to analyze
x[0] := 1.5:
xtol,ftol := 1e-10, 1e-10:
M := Array(0..0,1..3,[x[0],"",f(x[0])]):
for count from 1 to 10 do
   x[count] := newt(x[count-1]);
   xincr := abs(x[count]-x[count-1]);
   fval := abs(f(x[count]));
   M(count+1, ..) := <x[count]|xincr|fval>;
until xincr < xtol or fval < ftol:
#M;
W:=DataFrame(Matrix(M), columns=[x, abs(x_incr), ''f''(x)]);

DataFrame(Matrix(8, 3, {(1, 1) = 1.5, (1, 2) = "", (1, 3) = 9.390625, (2, 1) = 1.293895748, (2, 2) = .206104252, (2, 3) = 2.692407434, (3, 1) = 1.170160594, (3, 2) = .123735154, (3, 3) = .567277491, (4, 1) = 1.127066579, (4, 2) = 0.43094015e-1, (4, 3) = 0.49733650e-1, (5, 1) = 1.122508821, (5, 2) = 0.4557758e-2, (5, 3) = 0.500089e-3, (6, 1) = 1.122462053, (6, 2) = 0.46768e-4, (6, 3) = 0.5000000000e-7, (7, 1) = 1.122462048, (7, 2) = 0.5000000000e-8, (7, 3) = 0.3000000000e-8, (8, 1) = 1.122462048, (8, 2) = 0., (8, 3) = 0.3000000000e-8}), rows = [1, 2, 3, 4, 5, 6, 7, 8], columns = [x, abs(x_incr), f(x)])

Tabulate(W, width=600, widthmode=pixels,
            weights=[3, 15, 15, 15]):

 

Download newt_DF.mw

It has to be either a name or a string, since the literal input literal 46th by itself isn't valid Maple syntax for anything. That's also why your string won't parse.

It would be better if you explained clearly what you mean when you write that you want to "get 46th". What do you mean by "get"? Do you mean you want to be able to display something that looks like that, without the double-quotes showing? If so then in what context? Do you mean that you want a Maple expression? What do you mean by 46th, without the string quotes?

Here is one way to obtain it as a name.
   convert(46,compose,ordinal,name)
That prints in italics.

The following produces a name that prints in the Standard GUI in upright Roman.
  nprintf(`#mn(%a);`,convert(46,ordinal));

 

Here is one way. You can either call `k` with a single t-value, or with both a t-value and a new a-value.

After the plotting, you can call `k` at whatever t-values you wish.

restart;

xodephi := {diff(x(t), t) = 16250.25391*(1 - (487*x(t))/168 + 4*Pi*x(t)^(3/2) + (274229*x(t)^2)/72576 - (254*Pi*x(t)^(5/2))/21 + (119.6109573 - (856*ln(16*x(t)))/105)*x(t)^3 + (30295*Pi*x(t)^(7/2))/1728 + (7.617741607 - 23.53000000*ln(x(t)))*x(t)^4 + (535.2001594 - 102.446*ln(x(t)))*x(t)^(9/2) + (413.8828821 + 323.5521650*ln(x(t)))*x(t)^5 + (1533.899179 - 390.2690000*ln(x(t)))*x(t)^(11/2) + (2082.250556 + 423.6762500*ln(x(t)) + 33.2307*ln(x(t)^2))*x(t)^6)*x(t)^5, diff(xphi(t), t) = 5078.204347*x(t)^(3/2), x(0) = 0.03369973351, xphi(0) = a}:  #xphi(10.92469316) = 0}:

sol := dsolve(xodephi, parameters=[a], numeric):

#
# Calling k(anything, numeric) sets the `a` parameter
# to the second argument.
#
# Calling k(anything, nonnumeric)
#      or k(nonnumeric, anything)
#      or k(nonnumeric)
# returns unevaluated.
#
# Calling k(numeric) computes xphi at that value,
# using the current parameter setting.
#
# Calling k(numeric, numeric) computes xphi at the 1st argument,
# after setting the `a` parameter to the second argument.
#
k := proc(tt, A)
  if nargs=2 then
    if A::numeric and A<>eval(':-a',sol(parameters)) then
      sol(parameters=[A]);
    elif not A::numeric then
      return 'procname'(args);
    end if;
  end if;
  if not tt::numeric then return 'procname'(args); end if;
  eval(xphi(':-t'), sol(tt));
end:

 

k(10, 100);

HFloat(504.6805374189861)

k(10);

HFloat(504.6805374189861)

Afound := fsolve(k(10.92469316, A));

-467.1843838

sol(parameters=[Afound]);

[a = -467.1843838]

k(10);

HFloat(-62.503861924194105)

k(10, Afound);

HFloat(-62.503861924194105)

#plot( k(10, AA), AA=-500..100, size=[200,200] );

plots:-display(Array([
  plots:-odeplot(sol, [t,xphi(t)], t=0..11),
  plots:-odeplot(sol, [t,k(t)], t=0..11, labels=["",xphi]),
  plot(k(t), t=0..11, labels=["",xphi]) ]), size=[200,200]);

 

 

 

 

 

seq(k(i), i=0..11.0, 0.5);

-467.1843838, HFloat(-451.35284337254996), HFloat(-435.26421792219355), HFloat(-418.9024575949357), HFloat(-402.2497188915288), HFloat(-385.2857910231282), HFloat(-367.98805180702436), HFloat(-350.3311515250434), HFloat(-332.2859029154144), HFloat(-313.8186382395069), HFloat(-294.89050202705636), HFloat(-275.45611970121246), HFloat(-255.46131378253165), HFloat(-234.84114277829883), HFloat(-213.51691387261195), HFloat(-191.39019284210806), HFloat(-168.33651233737638), HFloat(-144.1927949775133), HFloat(-118.73944335663056), HFloat(-91.66557267955444), HFloat(-62.503861924194105), HFloat(-30.486336758748322), HFloat(5.845321943206811)

seq([i, k(i)], i=0..11.0, 0.5);

[0, -467.1843838], [.5, HFloat(-451.35284337254996)], [1.0, HFloat(-435.26421792219355)], [1.5, HFloat(-418.9024575949357)], [2.0, HFloat(-402.2497188915288)], [2.5, HFloat(-385.2857910231282)], [3.0, HFloat(-367.98805180702436)], [3.5, HFloat(-350.3311515250434)], [4.0, HFloat(-332.2859029154144)], [4.5, HFloat(-313.8186382395069)], [5.0, HFloat(-294.89050202705636)], [5.5, HFloat(-275.45611970121246)], [6.0, HFloat(-255.46131378253165)], [6.5, HFloat(-234.84114277829883)], [7.0, HFloat(-213.51691387261195)], [7.5, HFloat(-191.39019284210806)], [8.0, HFloat(-168.33651233737638)], [8.5, HFloat(-144.1927949775133)], [9.0, HFloat(-118.73944335663056)], [9.5, HFloat(-91.66557267955444)], [10.0, HFloat(-62.503861924194105)], [10.5, HFloat(-30.486336758748322)], [11.0, HFloat(5.845321943206811)]

 

Download ds_param_easy.mw

Using Maple 2020.1

restart;

url := "https://www.gw-openscience.org/GW150914data/P150914/fig1-observed-H.txt":


And it can be done simply.

M := ImportMatrix(url,source=Matlab,mode=ascii,skiplines=1):

plot(M);

Download import_url.mw

There are a few different effects.

There's the collecting of terms in the denominator, which I'm guessing is important for you.

And then there's the ordering of terms in products and sums. I'm supposing that you care more about the order of terms in sums, as that can affect whether there are unwanted leading minus-signs.

This is close, except for the order of the multiplicands (X__1-I__11) and (X__2-I__22).

restart;

a:=-(I__22-X__2)/(I__11*I__22-I__11*X__2-I__12*I__21-I__22*X__1+X__1*X__2);

-(I__22-X__2)/(I__11*I__22-I__11*X__2-I__12*I__21-I__22*X__1+X__1*X__2)

# Note that we didn't input `b` explicitly.
# If we had it would have helped and made some steps
# unnecessary. But we want the `sort` effects without
# that, hence `b` form was not entered explicitly.

sort(numer(a)/collect(denom(a),[I__12],simplify),
     [I__21,I__22],'ascending');

(X__2-I__22)/((X__2-I__22)*(X__1-I__11)-I__12*I__21)

 

Download simplif_sort_ex.mw

restart

with(Units:-Simple):

I__RMS__T := int((18*Unit('A')+9*t*Unit('A')/(.9*Unit('s')))^2, t = 0.1e-1*Unit('s') .. 10*Unit('s'))

54570.07529*Units:-Unit(A^2*s)

restart

I__RMS__T := int((18*Unit('A')+9*t*Unit('A')/(.9*Unit('s')))^2, t = 0.1e-1*Unit('s') .. 10*Unit('s'))

54570.07530*Units:-Unit(A^2*s)

Download test_units_int_ac.mw

Change the evalf(int(...)) calls to evalf(Int(...)) with capitalized Int.

[edit] On my machine that change alone is enough to bring the timing for the cited problematic example down to under 2 seconds. [end of edit]

The lowercase int attempts symbolic integration, and if that fails then wrapping evalf call induce numeric integration. But you may not wish to expend unnecessary effort on the symbolic attempt.

The capitalized Int is an inert placeholder, and evalf(Int(...)) goes directly to an attempt at numeric integration.

You could normalize the vector magnitude (according to the max over the given ranges, say), to get hue values between 0 and 1, and then scale by a factor to avoid the roll-over in the upper end of the hue.

Or you could cook up a similar thing for RGB instead of HSV.

There are other ways, but this could give you some ideas to play with.

restart;

plots:-fieldplot([1, y^2 + x], x = -10 .. 10, y = -6 .. 6,
                 fieldstrength = fixed,
                 color = COLOR(HSV, 0.833*abs(y^2+x+1)/(36+10+1),
                                    1.0, 1.0));

plots:-fieldplot([1, y^2 + x], x = -10 .. 10, y = -6 .. 6,
                 fieldstrength = fixed,
                 color = COLOR(HSV, 0.833*sqrt(((y^2+x)^2+1)/((6^2+10)^2+1)),
                                    1.0, 1.0));

huemin:=0.0: huemax:=0.5:

plots:-fieldplot([1, y^2 + x], x = -10 .. 10, y = -6 .. 6,
                 fieldstrength = fixed,
                 color = COLOR(HSV, huemin+(huemax-huemin)*sqrt(((y^2+x)^2+1)/((6^2+10)^2+1)),
                                    1.0, 1.0));

plot3d(0.833*sqrt(((y^2+x)^2+1)/((6^2+10)^2+1)), x = -10 .. 10, y = -6 .. 6,
       color = COLOR(HSV, 0.833*sqrt(((y^2+x)^2+1)/((6^2+10)^2+1)),
                                    1.0, 1.0),
       labels=[x,y,HUE]);

 

Download color_formula.mw

I included that surface plot3d to illustrate that one of those formulas make it behave something like this:

plot3d(LinearAlgebra:-Norm(<[1,y^2+x]>,2),x=-10..10,y=-6..6,
       colorscheme=["zgradient",[Red,Magenta],colorspace="HSV"]);

And I chose 0.833 for the scaling because that approximates magenta as an upper value for hue.

ColorTools:-Color("HSV", "Magenta")[];
     0.8333333333, 1.000000000, 1.00000000

Here is one way,

V1 := Vector[column](8, [1,2,2,1,3,A,B,1/(A + B)^2]):
V2 := Vector[column](8, [1,2,2,1,3,A,B,1/(A^2 + 2*A*B + B^2)]):

verify( V1, V2, 'Vector(simplify)' );

                 true

How about,

foo:=proc(ode::{`=`, set(`=`), list(`=`)},func::`function`,$)
   print("ode=",ode);
   print("func=",func);
end proc:

Or, shorter,

foo:=proc(ode::{`=`, {set,list}(`=`)},func::`function`,$)
   print("ode=",ode);
   print("func=",func);
end proc:

Or, excluding the empty cases,

foo:=proc(ode::{`=`, And({set,list}(`=`),Not(identical([],{})))},
          func::`function`,$)
   print("ode=",ode);
   print("func=",func);
end proc:

There is no direct LaTeX parser in Maple.

But there is support for mixed plaintext and marked up (typeset) 2D Math in plot titles, captions, legends, etc.

You can enter the 2D Math directly, manually, by operating in 2D Math mode (Ctl-R) and selecting special characters, accents, etc, from the left palettes.

Or you can enter it all in plaintext code using a sort of XML/HTML markup (undocumented, but not overly difficult). That can be accomplished programatically. For example,

plot(sin(x), x=5..20, view=-2..2, size=[500,300],
     axis[1]=[gridlines=[seq(i,i=5..20,5)]],
     axis[2]=[gridlines=[$-2..2]], axes=boxed,
     title=typeset("Verl", `#mo(\"&auml;\");`,
                   "ufe von ", e(t), " und ",
                   `#msub(mover(mi("&varepsilon;"),mi("&period;")),mo("p"));`(t)));
 

Download plot_typemk.mw

 

For fun, a few more ways that ought to run in Maple 2017.2 (the second is rather slower).

restart;
f := x->18*log10(x):
g := x->1/2*x^3-8*x^2+69/2*x-27:

[fsolve(f-g, 0..12, maxsols=5)];

     [0.03721465484, 1.000000000, 4.506164928, 10.00000000]

sort(remove(type,[solve(f(x)-1.0*g(x))], nonreal));

         [0.03721465485, 1., 4.506164928, 10.00000000]

Here is one way. There are others. I think that it's useful that these render in upright Roman rather than as italics.

restart:

interface(rtablesize=20):

f:=(x,t)->x*t;
g:=(x,t)->x^2*t;

proc (x, t) options operator, arrow; x*t end proc

proc (x, t) options operator, arrow; x^2*t end proc

B:=Matrix([[x,t,"f(x,t)","g(x,t)"],
         seq(seq([i,j,f(i,j),g(i,j)],
                 j=0.125..0.875, 0.25),
             i=0.125..0.875,0.25)]):

B[1,..]:=map(p->nprintf(`#mi(%a);`,p),B[1,..]):
B[2..,4]:=map(p->nprintf(`#mn(\"%1.3e\");`,p),B[2..,4]):
B;

Matrix(17, 4, {(1, 1) = `#mi("x")`, (1, 2) = `#mi("t")`, (1, 3) = `#mi("f(x,t)");`, (1, 4) = `#mi("g(x,t)");`, (2, 1) = .125, (2, 2) = .125, (2, 3) = 0.15625e-1, (2, 4) = `#mn("1.953e-03")`, (3, 1) = .125, (3, 2) = .375, (3, 3) = 0.46875e-1, (3, 4) = `#mn("5.859e-03")`, (4, 1) = .125, (4, 2) = .625, (4, 3) = 0.78125e-1, (4, 4) = `#mn("9.766e-03")`, (5, 1) = .125, (5, 2) = .875, (5, 3) = .109375, (5, 4) = `#mn("1.367e-02")`, (6, 1) = .375, (6, 2) = .125, (6, 3) = 0.46875e-1, (6, 4) = `#mn("1.758e-02")`, (7, 1) = .375, (7, 2) = .375, (7, 3) = .140625, (7, 4) = `#mn("5.273e-02")`, (8, 1) = .375, (8, 2) = .625, (8, 3) = .234375, (8, 4) = `#mn("8.789e-02")`, (9, 1) = .375, (9, 2) = .875, (9, 3) = .328125, (9, 4) = `#mn("1.230e-01")`, (10, 1) = .625, (10, 2) = .125, (10, 3) = 0.78125e-1, (10, 4) = `#mn("4.883e-02")`, (11, 1) = .625, (11, 2) = .375, (11, 3) = .234375, (11, 4) = `#mn("1.465e-01")`, (12, 1) = .625, (12, 2) = .625, (12, 3) = .390625, (12, 4) = `#mn("2.441e-01")`, (13, 1) = .625, (13, 2) = .875, (13, 3) = .546875, (13, 4) = `#mn("3.418e-01")`, (14, 1) = .875, (14, 2) = .125, (14, 3) = .109375, (14, 4) = `#mn("9.570e-02")`, (15, 1) = .875, (15, 2) = .375, (15, 3) = .328125, (15, 4) = `#mn("2.871e-01")`, (16, 1) = .875, (16, 2) = .625, (16, 3) = .546875, (16, 4) = `#mn("4.785e-01")`, (17, 1) = .875, (17, 2) = .875, (17, 3) = .765625, (17, 4) = `#mn("6.699e-01")`})

 

Download sci_not.mw

The command assigned provides functionality that can be used to test whether a value is being used as index for a given table (ie, that the table reference evaluates to something else).

restart;                                                                           

L["hello"]:=funny:

assigned(L["hello"]);

                true

You can use the ExcelTools:-Export command.

If you export a list to .xls file then the entries should appear as a row within your spreadsheet application (eg. Excel).

If you export a column Vector to .xls file then the entries should appear as a column within your spreadsheet application. When you call the Vector command in Maple the default for it to act like the Vector[column] command and to produce a column Vector.

For example,

L := [seq(fsolve(x*y=1),x=1..2,0.2)];

V := Vector(L);

ExcelTools:-Export(L, "f1.xls");

ExcelTools:-Export(V, "f2.xls");

You will find that even the basics commands of the Maple programming language allow for much more power and flexibility than the right-click or context-menu actions ever can. It's worthwhile figuring out how to do such things purely programmatically.

4 5 6 7 8 9 10 Last Page 6 of 241