acer

32405 Reputation

29 Badges

19 years, 351 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

The description from your instructor uses an expression for f. Your attempt calls Newton with an operator for f. Both methodologies can work, but unfortunately your attempt mixed up the two concepts.

Below I show both with modifications of your code, for f as operator or expression.

By the way, you only have to differentiate once. You don't have to repeat that part of the computation, for each iteration of the loop.

You might find these revisions to your code of some use. I corrected the differentiation and evaluation of the derivative, at a point. I also moved the conditional test into the loop so that it would return (possibly earlier) as soon as any iterate satisfied the criterion.

Download rf.mw

restart;
Newton_proc:=proc(f, a)
  local df,n,x1;
  df:=D(f);
  x1:=a;
  if abs(f(x1))<=10^(-8) then
    return x1;
  end if;
  for n to 9 do
    x1:=x1-f(x1)/df(x1);
    if abs(f(x1))<=10^(-8) then
      return x1;
    end if;
  end do;
  return FAIL;
end proc:

Newton_proc( x->x^3-3*x^2+2*exp(x)+5, -1.2 );
         -1.162515305

Newton_proc( x->arctan(x), 1.5 );
              FAIL

restart;
Newton_expr:=proc(f, a)
  local df,n,x,x1;
  x := indets(f,And(name,Not(constant),
                    satisfies(u->depends(f,u))));
  if nops(x)<>1 then
    error "expecting an expression depending on one name";
  else
    x := x[1];
  end if;
  df:=diff(f,x);
  x1:=a;
  if abs(eval(f,x=x1))<=10^(-8) then
    return x1;
  end if;
  for n to 9 do
    x1:=x1-eval(f,x=x1)/eval(df,x=x1);
    if abs(eval(f,x=x1))<=10^(-8) then
      return x1;
    end if;
  end do;
  return FAIL;
end proc:

Newton_expr( x^3-3*x^2+2*exp(x)+5, -1.2 );
          -1.162515305

Newton_expr( arctan(x), 1.5 );
              FAIL

You could simplify the system of equations by the constraint equations, prior to calling solve. (I did this a few hours ago. You can compare with vv's suggestion.)

Download Example_ac.mw

What properties do you know about the variables?

restart;

Given_eq_3_37 := M = k_ * sqrt(L__1 * L__2);

M = k_*(L__1*L__2)^(1/2)

Given_eq_3_30 := k_ = sqrt((L__m)^2 / (L__1 * L__2p));

k_ = (L__m^2/(L__1*L__2p))^(1/2)

combine(eval(Given_eq_3_37,Given_eq_3_30))
  assuming L__1*L__2>0;

M = (L__2*L__m^2/L__2p)^(1/2)

combine(eval(Given_eq_3_37,Given_eq_3_30))
  assuming L__m^2/(L__1*L__2p)>0;

M = (L__2*L__m^2/L__2p)^(1/2)

Download Q20201001_ac.mw

If I understand your goal properly, then one way is to first select all the terms which satisfy
the second condition, and then to select from that those terms which have the least degree.

restart;

F := proc(P)
  local temp, mindeg;
  temp := select(term->member(degree(term,[x])-degree(term,[y]),
                              [0,-1]), [op(P)]);
  mindeg := min(map(degree,temp,[x,y]));
  select(term->degree(term,[x,y])=mindeg, temp);
end proc:

F( 100*x^2*y^2 + 35*y*x + 45*x );

[35*y*x]

F( 13*x^2*y^2 + x*y^2 + 2*y*x^2 );

[x*y^2]

 

Download poly_deg_query.mw

A 3-D simplicial facet is a facet of a 3-D simplex, ie. a triangle.

restart:

A := ArrayTools:-RandomArray(35, 3):

xyz := convert(A,listlist):

ch := ComputationalGeometry:-ConvexHull(xyz);

[[1, 14, 7], [14, 5, 7], [5, 32, 7], [32, 5, 14], [31, 24, 11], [31, 16, 24], [4, 31, 11], [4, 6, 14], [35, 32, 14], [6, 35, 14], [35, 6, 4], [32, 34, 7], [1, 2, 14], [2, 4, 14], [24, 21, 11], [21, 4, 11], [21, 35, 4], [31, 3, 16], [3, 17, 16], [2, 3, 31], [17, 3, 1], [3, 2, 1], [17, 26, 16], [26, 34, 32], [16, 26, 24], [35, 27, 32], [27, 26, 32], [26, 27, 24], [27, 21, 24], [21, 27, 35], [4, 33, 31], [33, 2, 31], [2, 33, 4], [15, 26, 17], [15, 1, 7], [15, 17, 1], [34, 15, 7], [26, 15, 34]]

# Pick off the 7th element of xyz;
xyz[7];

[HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]

# select all members of ch which contain the ordinal
# reference 7
T7 := select(c->member(7,c), ch);

[[1, 14, 7], [14, 5, 7], [5, 32, 7], [32, 34, 7], [15, 1, 7], [34, 15, 7]]

# Notice that xyz[7] is present in each of these
# lists of three 3-dimensional points. Each represents
# a 3-dimensional simplicial facet (ie. a triangle).
map(pt->print(xyz[pt]),T7):

[[HFloat(0.16564872949978093), HFloat(0.25750825412373646), HFloat(0.09713178123584754)], [HFloat(0.05395011866660715), HFloat(0.9597439585160811), HFloat(0.035711678574189554)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

[[HFloat(0.05395011866660715), HFloat(0.9597439585160811), HFloat(0.035711678574189554)], [HFloat(0.16218230819324275), HFloat(0.9592914252054443), HFloat(0.7060460880196088)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

[[HFloat(0.16218230819324275), HFloat(0.9592914252054443), HFloat(0.7060460880196088)], [HFloat(0.2435249687249893), HFloat(0.9502220488383549), HFloat(0.9133758561390194)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

[[HFloat(0.2435249687249893), HFloat(0.9502220488383549), HFloat(0.9133758561390194)], [HFloat(0.25428217897153105), HFloat(0.694828622975817), HFloat(0.9057919370756192)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

[[HFloat(0.07585428956306361), HFloat(0.49836405198214295), HFloat(0.6557406991565868)], [HFloat(0.16564872949978093), HFloat(0.25750825412373646), HFloat(0.09713178123584754)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

[[HFloat(0.25428217897153105), HFloat(0.694828622975817), HFloat(0.9057919370756192)], [HFloat(0.07585428956306361), HFloat(0.49836405198214295), HFloat(0.6557406991565868)], [HFloat(0.011902069501241397), HFloat(0.699076722656686), HFloat(0.6554778901775566)]]

# Instead of just extracting them, form polygon plotting
# structures from them instead. Again, notice that xyz[7]
# is a vertex of each of these triangles.
map(pt->plottools:-polygon(xyz[pt],transparency=0.5),T7);

[POLYGONS(Matrix(3, 3, {(1, 1) = .16564872949978093, (1, 2) = .25750825412373646, (1, 3) = 0.9713178123584754e-1, (2, 1) = 0.5395011866660715e-1, (2, 2) = .9597439585160811, (2, 3) = 0.35711678574189554e-1, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5)), POLYGONS(Matrix(3, 3, {(1, 1) = 0.5395011866660715e-1, (1, 2) = .9597439585160811, (1, 3) = 0.35711678574189554e-1, (2, 1) = .16218230819324275, (2, 2) = .9592914252054443, (2, 3) = .7060460880196088, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5)), POLYGONS(Matrix(3, 3, {(1, 1) = .16218230819324275, (1, 2) = .9592914252054443, (1, 3) = .7060460880196088, (2, 1) = .2435249687249893, (2, 2) = .9502220488383549, (2, 3) = .9133758561390194, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5)), POLYGONS(Matrix(3, 3, {(1, 1) = .2435249687249893, (1, 2) = .9502220488383549, (1, 3) = .9133758561390194, (2, 1) = .25428217897153105, (2, 2) = .694828622975817, (2, 3) = .9057919370756192, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5)), POLYGONS(Matrix(3, 3, {(1, 1) = 0.7585428956306361e-1, (1, 2) = .49836405198214295, (1, 3) = .6557406991565868, (2, 1) = .16564872949978093, (2, 2) = .25750825412373646, (2, 3) = 0.9713178123584754e-1, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5)), POLYGONS(Matrix(3, 3, {(1, 1) = .25428217897153105, (1, 2) = .694828622975817, (1, 3) = .9057919370756192, (2, 1) = 0.7585428956306361e-1, (2, 2) = .49836405198214295, (2, 3) = .6557406991565868, (3, 1) = 0.11902069501241397e-1, (3, 2) = .699076722656686, (3, 3) = .6554778901775566}), TRANSPARENCY(.5))]

# plot the xyz[7] point, and all the
# triangles that have it as a vertex.
plots:-display(
  plots:-pointplot3d(xyz[7],color=red,symbolsize=20,symbol=solidsphere),
  map(x->plottools:-polygon(xyz[x],transparency=0.5),T7),
  labels=[x,y,z], view=[0..1,0..1,0..1]
);

# plot all the points, and all the triangles (which
# make up the convex hull.
plots:-display(
  plots:-pointplot3d(xyz,color=red,symbolsize=20,symbol=solidsphere),
  plots:-display(map(pt->plottools:-polygon(xyz[pt],transparency=0.5),ch)),
  labels=[x,y,z], view=[0..1,0..1,0..1]
);

 

 

Download hull3D.mw

I suppose that you have already searched the FAQs.

You could contact Maplesoft Technical Support.

Be sure to let them know your Maple version, Operating System and version, and whether you are using any Maple "language packs".

Using concatenated names (which are always global) within procedures is poor programming practice, in my opinion.

Having said that,

restart

x := 3

A || 1 := 3

P := proc () local x; global y, z; x := 'x'; y := fsolve([x+5 = -2], {x}); A || 1 := cat(A, 1); z := fsolve([A || 1+5 = -2], {A || 1}) end proc

P()

{A1 = -7.}

A || 1

A1

 

Download Concatenate_Question_ac.mw

You could also use  cat(':-A',1)  for additional safety.

You can pick off those introduced (free) parameters, evaluate expressions (C, Q, other...) at arbitrary values for them, and so on.

Forcing the stem of the parameter name with the free option makes it easier to pick them off programmtically in the more general case.

Another choice is to use LeastSquares instead of LinearSolve, which will produce a single instance of the solution rather than a parametrized solution.

(I changed the first two entries of B, to make the LeastSquares result more interesting than the zero-Vector.)

restart

A := Matrix(4, 4, {(1, 1) = 1, (1, 2) = 3, (1, 3) = 4.5, (1, 4) = 7, (2, 1) = 0, (2, 2) = 2, (2, 3) = 5, (2, 4) = 9, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0, (4, 1) = 0, (4, 2) = 0, (4, 3) = 0, (4, 4) = 0}); B := Vector(4, {(1) = 1, (2) = 2, (3) = 0, (4) = 0}); C := LinearAlgebra:-LinearSolve(A, B, free = F)

Vector[column](%id = 18446883819568535062)

params := [indets(C, 'specindex(posint, F)')[]]

[F[1], F[2]]

W1 := C[1]; W2 := C[2]; W3 := C[3]; W4 := C[4]; Q := W1*x^4+W2*x^3+W3*x^2+W1

(-HFloat(2.0)+HFloat(3.0)*F[2]+HFloat(6.5)*F[1])*x^4+(HFloat(1.0)-HFloat(2.5)*F[2]-HFloat(4.5)*F[1])*x^3+F[2]*x^2-HFloat(2.0)+HFloat(3.0)*F[2]+HFloat(6.5)*F[1]

map(`=`, params, 1)

[F[1] = 1, F[2] = 1]

eval(C, map(`=`, params, 1))

Vector[column](%id = 18446883819568524694)

eval(Q, map(`=`, params, 1))

HFloat(7.5)*x^4-HFloat(6.0)*x^3+x^2+HFloat(7.5)

A.C, B

Vector[column](%id = 18446883819568519038), Vector[column](%id = 18446883819672872470)

Alt := LinearAlgebra:-LeastSquares(A, B, method = SVD)

Vector[column](%id = 18446883819568516262)

A.Alt, B

Vector[column](%id = 18446883819568509646), Vector[column](%id = 18446883819672872470)

NULL

Download MultipleTCoefficients_ac.mw

restart;

local gamma: local phi:

sinc:=unapply(piecewise(x=0, 1, (sin(Pi*x))/(Pi*x)),x):
S:=unapply(sinc((x-k*h)/h),k,h,x):

a := -3: b := 4:
f:=unapply(x^2*t^3,x,t);

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

phi:=unapply(log((x-a)/(b-x)),x):
gamma:=unapply(log(t),t):

teta:=2: Omega:=1:

w:=unapply(simplify(1/(D(phi)(x))^((teta-1)/2)),x):
v:=unapply(simplify((D(gamma)(t))^(Omega/2)),t):

k:=2: l:=1:
#hx:=1.282549830:
ht:=hx:

G:=Int((Int(f(x,t)*S(k,hx,phi(x))*S(l,ht,gamma(t))*w(x)*v(t),x=a..b),t=0..infinity)):

GG:=Int(t^3*S(l,ht,gamma(t))*v(t),t=0..infinity)*Int(x^2*S(k,hx,phi(x))*w(x),x=a..b):

combine(G-GG);

0

op(2,GG);
evalf(eval(%,hx=1.282549830));

Int(x^2*piecewise((-2*hx+ln((x+3)/(4-x)))/hx = 0, 1, sin(Pi*(-2*hx+ln((x+3)/(4-x)))/hx)*hx/(Pi*(-2*hx+ln((x+3)/(4-x)))))*sqrt(7)/(7*sqrt(-1/((x+3)*(-4+x)))), x = -3 .. 4)

5.148697506

#Int(t^3*sin(Pi*(-hx+ln(t))/hx)/Pi/(-hx+ln(t))*hx*(1/t)^(1/2),t = 0 .. infinity);
op(1,GG);
simplify(value(%)) assuming hx::real;

Int(t^3*piecewise((-hx+ln(t))/hx = 0, 1, sin(Pi*(-hx+ln(t))/hx)*hx/(Pi*(-hx+ln(t))))*sqrt(1/t), t = 0 .. infinity)

undefined

 

Download doubleint1.mw

If I understand your goal correctly, the coordinates of the cells are present in the plot structure computed by the VoronoiDiagram command.

They are Matrices, but you could convert them to listlist.

Let us know if you have difficulty converting or manipulating them into some other form. Please be specific in details, though. If you want them merged, without repeats, into one big flat list of points then that could be done -- though if there are very many original data points it computed be computed more efficiently in a more direct manner (via Delaunay triangulation, say), without going through production of the full diagram/plot.

Here is an example, separating then according to the color.

restart;

with(ComputationalGeometry):

m:=LinearAlgebra:-RandomMatrix(7,2,generator=0.0..1.0):

P:=VoronoiDiagram(m,symbol=solidcircle,symbolsize=7,size=[300,300],axes=boxed):
P;

V:=[op(indets(P,specfunc(anything,POLYGONS)))]:

T:=ListTools:-Classify(u->op(2,u),V):

Tinds:=indices(T,nolist);

COLOUR(RGB, .47058824, 0., 0.54901961e-1), COLOUR(RGB, .29019608, .47058824, 0.), COLOUR(RGB, 0., 0.54901961e-1, .47058824)

ColorTools:-Color(Tinds[1]), map[2](op,1,T[Tinds[1]]);

"<RGB : 0.471 0 0.0549>,{[[[0.674755640087579,0.528416189867507],[0.774429137959799,0.760000722399843],[0.565091023500541,1.70390300233302],[0.278700624534252,0.573844297940794]]],[[[0.781700218780773,0.381772909439530],[4.66209503332205,0.337974064337378],[3.70924190390966,-0.608387011801836],[0.716780642385679,0.176311320824582]]]}"

ColorTools:-Color(Tinds[2]), map[2](op,1,T[Tinds[2]]);

"<RGB : 0.29 0.471 0>,{[[[0.278700624534252,0.573844297940794],[-2.20505390543617,-0.708421311778174],[-3.70554867069412,0.505458130569214],[0.564102931719082,4.77510973298241],[0.593861093166918,4.73834981392173],[0.565091023500541,1.70390300233302]]],[[[0.716780642385679,0.176311320824582],[-0.359151757230016,-2.62884825842281],[0.564102931719082,-3.76574686098490],[3.70924190390966,-0.608387011801836]]],[[[0.674755640087579,0.528416189867507],[0.781700218780773,0.381772909439530],[4.66209503332205,0.337974064337378],[4.85550306716113,0.505458130569214],[4.39299635377559,0.933178173190759],[0.774429137959799,0.760000722399843]]]}"

ColorTools:-Color(Tinds[3]), map[2](op,1,T[Tinds[3]]);

"<RGB : 0 0.0549 0.471>,{[[[0.674755640087579,0.528416189867507],[0.781700218780773,0.381772909439530],[0.716780642385679,0.176311320824582],[-0.359151757230016,-2.62884825842281],[-2.20505390543617,-0.708421311778174],[0.278700624534252,0.573844297940794]]],[[[0.774429137959799,0.760000722399843],[4.39299635377559,0.933178173190759],[0.593861093166918,4.73834981392173],[0.565091023500541,1.70390300233302]]]}"

 

Download Voronoi_cells.mw

The result coming out of symbolic int (from the meijerg method) is incorrect.

You can very quickly produce a plot as follows, however, using numeric integration. Note the use of inert (captalized) Int below, instead of active int.

Here I get good speed by forcing a numeric integration method which avoids (here unnecessary) expensive discontinuity checking.

plot(Int(exp(-(x^2-a)^2),x=0..infinity,method=_d01amc),
     a=-2..5);

Other quick approaches here include specifying a decent method other than _d01amc (eg. _Dexp) or utilizing operator form for the integrand within the Int call (slightly tricky since the value of parameter a has to be shoehorned in). These are not quite as quick as the approach given above.

plot(Int(exp(-(x^2-a)^2),x=0..infinity,method=_Dexp),
     a=-2..5);

plot(a->Int(unapply(exp(-(x^2-a)^2),x),0..infinity),
     -2..5);

Using Int (or int with its numeric option) alone for this example is considerably slower slower than the approaches mentioned above. These next approaches will also produce the better plot, but they are not nearly as fast (because of hybrid symbolic-numeric checking), taking about 60sec versus less than 1sec for the first approach above. The slowness is not due to the accuracy tolerance.

plot(Int(exp(-(x^2-a)^2),x=0..infinity),a=-2..5);

plot(a->int(exp(-(x^2-a)^2),x=0..infinity,numeric),-2..5);

I will submit a bug report against the symbolic int command, for the incorrect result coming out of the meijerg method.

If you read the Help page for Topic proc you can see a description of the fact that local variables only get 1-level evaluation.

You code does a rather unusual and roundabout trick of creating expressions with references to y, and then temporarily assigning specific values to y (eg. y=0, y=b), and then unassigning y. It does this while constructing the expressions that get assigned to EQ1,EQ2,EQ3, and EQ4.

When you do all that at the top-level then the expressions assigned to EQ1,etc get fully evaluated, and the references to y are replaced by the values temporarily assigned to y, and no references to y remain.

But when you do that within a procedure (where y, GenFunc, EQHelp, and EQHelp2 are local variables -- whether declared explicitly or not) the references to local y only get 1-level evaluation, and are not resolved to the temporary assigned values.

There are several ways around this:

A) You could wrap in a simple call to eval, each of the four constructions. For example, instead of,
          EQ1 := GenFunc = 0;
you could have,
          EQ1 := eval(GenFunc = 0);
while retaining you temp assignments to y, and unassignments of y (so that the diff calls were still valid). This is workable, but retains what I consider to be a very awkward way of doing something that should be simple. The fact that you have to repeatedly unassign y is a hint that is suboptimal. This is not the best direction in which to move. Test_GetDet_MaplePrimes_assign_eval_unassign.mw

B) You could declare EQHelp, EQHelp2, and GenFunc as a global at the top of the wrapping GetDet procedure, and keep all the other mechanisms as they are. That works, but retains all the awkwardness. It's actually more awkward and convoluted. (For other examples you may get lost trying to figure out what to declare global. Also, unnecessary global declarations are evil and wicked.) This is the wrong direction in which to move. Test_GetDet_MaplePrimes_globaldecl.mw

C) You could get rid of all the need for repeated assigning and unassigning to y, by instead using the so-called 2-argument calling sequence of the eval command to evaluate GenFunc, EQHelp, and EQHelp2 at the specific values of y. This is much more usual and convenient way to utilize a value for a variable without having to unassign it for subsequence symbolic use (the ensuing diff calls). This is the right way to accomplish this part of the task (IMO), regardless of whether inside a wrapping procedure or all done at the top-level. Test_GetDet_MaplePrimes_ac.mw

You keep submitting duplicates of earlier question threads. You spam this site with them. It is inconsiderate and unhelpful. I flag them as duplicates, and then delete them. I have given you messages about this -- as private messages and comments on earlier Questions.

Put your followup queries onto one of your related earlier Questions, instead.

If you have other kinds of example then you should show them.

restart;

expr := 4.22647099771398*exp(-0.0104163888375266*t)
        - 4.22647099771398*exp(-2.00033505979518*t);

4.22647099771398*exp(-0.104163888375266e-1*t)-4.22647099771398*exp(-2.00033505979518*t)

indets(expr,specfunc(anything,exp));

{exp(-2.00033505979518*t), exp(-0.104163888375266e-1*t)}

# note the order of the terms (we don't know
# whether that matters to you.
map(op,indets(expr,specfunc(anything,exp)));

{-2.00033505979518*t, -0.104163888375266e-1*t}

# This preserves the "ordering" of the terms in the original sum.
[seq([op(map(op,indets(term,specfunc(anything,exp))))], term=expr)];

[[-0.104163888375266e-1*t], [-2.00033505979518*t]]

# This preserves the "ordering" of the terms in the original sum.
# Here the operands of the exp calls are not separated, so
# only use this if you know that each term has only one such
# call.
[seq(op(map(op,indets(term,specfunc(anything,exp)))), term=expr)];

[-0.104163888375266e-1*t, -2.00033505979518*t]

 

Download indets_exp.mw

Some instructor has a sense of humour.

(I made no effort for efficiency.)

Guessing at how num2text might work was not difficult.

restart;

num2text := proc(n::posint) local C, i;
  C := convert(n,'base',100);
  cat(seq("abcdefghijklmnopqrstuvwxyz"[C[nops(C)-i+1]],
          i=1..nops(C)));
end proc:

eqs := {z =  4*x + 19*y + 2515211725275120,
        z = 52*x + 27*y + 2515210613496048,
        z = 36*x + 65*y + 2515210981587340,
        z =  6*x + 60*y + 2515211676449260}:

Q := eval([x,y,z], solve(eqs,{x,y,z}));

[23151811, 61518, 2515211819051206]

map(num2text, Q)[];

"work", "for", "yourself"

cat(num2text(Q[1])," ",num2text(Q[2])," ",num2text(Q[3]));
 

"work for yourself"

#cat(num2text(Q[1]), seq([" ",num2text(Q[i])][],i=2..nops(Q)));

 

Download hwQuestion.mw

First 108 109 110 111 112 113 114 Last Page 110 of 336