Carl Love

Carl Love

28065 Reputation

25 Badges

13 years, 21 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

How about this? With the data in your worksheet, this took exactly 200 iterations. The resulting plot is a straight line (as well as can be seen with the naked eye). Note the the default for Norm is the infinity norm, which is equivalent to checking the absolute values of all of the entries. In other words, the convergence criterion is that the distance for both coordinates of all n-1 points is less than epsilon.

ErrX:= Vector(n, [0,1]):
ErrY:= Vector(n, [0,1]):
rho:= 1.2:  #relaxation factor
epsilon:= 10.^(-5):
while LinearAlgebra:-Norm(< ErrX, ErrY >) >= epsilon do
    for k from 2 to n do
        Xk:= X[k] - rho*GX(X, Y, k)/GXX(X, Y, k);
        ErrX[k]:= Xk - X[k];
        X[k] := Xk;
        Yk:= Y[k] - rho*GY(X, Y, k)/GYY(X, Y, k);
        ErrY[k]:= Yk - Y[k];
        Y[k]:= Yk
    end do
end do;

restart;
m:= proc(r::nonnegint, c::nonnegint)
   option remember;
   if r*c=0 then 0 else c*thisproc(r-1,c)+a*thisproc(r-1,c-1) end if
end proc:
m(0,0):= 1:

M:= Matrix(5,5, m);

Matrix indices start at 1, not 0, so the (trivial) 0th row and column are not shown. If you want indices that start somewhere other thn 1, you need to use an Array. But then you won't get the neat tabular layout in the GUI.

Your procedure myp can be reduced to

myp:= (a,b,c)-> zip(`+`, a, b)*c;

Is it possible to use something like
#map2(myp,[$1..4],{[x,y,z,w],[x2,y2,z2,w2],[x3,y3,z3,w3]},[4,10,0.5]);
to achieve this?

seq(map2(myp, [$1..4], {[x,y,z,w],[x2,y2,z2,w2],[x3,y3,z3,w3]}, s), s= [4,10,0.5]);

(I don't know why the line above breaks bad. It appears as one line in the editor, and I've tried several times to fix it.)

Is there a document I can read apart from the ?map to look for some general tips to 'simutanous' computing both symbolically and numerically in Maple?

See ?operators,elementwise and ?curry.

And is there any documentation on using multicore CPUs?

See ?Threads.

When applied to an rtable (Matrix, Vector, Array), nops is not the number of elements; rather, it is the number of operands in the internal structure. You can see the internal structure of A via op(A). For the number of elements, use rtable_num_elems(A, 'All').

I'd like to show you the relevant commands, but I also want you to do your homework. So I'll compromise by posting the following without comments, most output elided, and without an attached file.

restart;
X:= t-> 3*cos(t):  Y:= t-> 2*sin(t):  t0:= 0.5:
kappa:= eval(VectorCalculus:-Curvature(<X(t),Y(t)>), t= t0):
dist:= (X1,X2)-> sqrt((X1[1]-X2[1])^2+(X1[2]-X2[2])^2):
slope:= (D(Y)/D(X))(t0):
x0:= X(t0):  y0:= Y(t0):
eq1:= (Cy-y0)/(Cx-x0) = -1/slope:
eq2:= dist([Cx,Cy],[x0,y0]) = 1/kappa:
fsolve({eq1,eq2}, {Cy,Cx});
                        {Cx = 4.13904333495107, Cy = 2.19319067267216}
fsolve({eq1,eq2}, {Cx,Cy}, avoid= %);
                        {Cx = 1.1264520363911743, Cy = -0.27548851825535093}
assign(%);
tan_eq:= slope*(x-x0)+y0:
Ellipse:= plot([X,Y,0..2*Pi], color= blue, scaling= constrained, thickness= 2):
Circle:= plot([Cx+cos(t)/kappa, Cy+sin(t)/kappa, t= 0..2*Pi], color= red):
Center:= plot([[Cx,Cy]], style= point, symbolsize= 25, symbol= cross, color= red):
Tangent:= plot(tan_eq, x= 0..4, color= green, thickness= 2):
plots:-display([Ellipse, Circle, Center, Tangent]);

There are some problems with your terminology. You say gg is a vector. It doesn't make sense to raise a vector to a power. Perhaps g is a vector and gg represents the vector's dot product with itself, which would be a scalar. What you call the "gradient of psi" is the derivative of V with respect to psi. You can get this as

diff(V, psi);

But that will not express the derivative in terms of V.

You expression GVpsi is off by a factor of V. In other words, the numerator of the coefficient in front of the expression should be V instead of 1. It is difficult to get Maple to produce this expression as the derivative of V. However, you can verify it by subtracting diff(V, psi) and simplifying, obtaining 0. The expression can be derived "by hand" by a process called "logarithmic differentiation", which you can look up in a calculus textbook, which is often used to take the derivative of complicated expressions of the form f(x)^g(x).

restart;

V1:= (1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi));

(1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi))

dV:= diff(V1, psi);

(1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi))*(-ln(1-alpha+alpha*g^(1-1/psi))/((1-1/psi)^2*psi^2)+alpha*g^(1-1/psi)*ln(g)/((1-1/psi)*psi^2*(1-alpha+alpha*g^(1-1/psi))))

dV2:= V*(alpha*V^(1/psi-1)*g^(1-1/psi)*ln(g)-ln(V))/psi^2/(1-1/psi);

V*(alpha*V^(1/psi-1)*g^(1-1/psi)*ln(g)-ln(V))/(psi^2*(1-1/psi))

eval(dV2, V= V1);

(1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi))*(alpha*((1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi)))^(1/psi-1)*g^(1-1/psi)*ln(g)-ln((1-alpha+alpha*g^(1-1/psi))^(1/(1-1/psi))))/(psi^2*(1-1/psi))

simplify(% - dV, symbolic);

0

 

 

Download Psi.mw

All algebraic grouping in Maple, no matter how deeply nested, is done with ordinary round parentheses (), not square brackets [] or curly braces {}.

Trying to separate assigned and unassigned names can get very confusing, especially if you try to generalize it by putting the names in a set and then iterating over that. Here's a much simpler method to set certain SpecialNames to in an expression ex if they are not assigned.

SpecialNames:= {h||(251..256)}:
evalindets(ex, name, x-> `if`(x in SpecialNames, 0, x));

You can make up weird situations where this won't work, like if one name in the set is assigned to another name in the set. But you're not going to do that weird thing, right?

Let me know how that works for you.

chrem([3, 3, 10, 0], [7, 11, 13, 7+11+13]);
                              465

P1:= plots:-polarplot([1,t,t=3*Pi/16..7*Pi/16]):
plots:-display([
     P1,
     plots:-polygonplot(
          < < 0 | 0 >, plottools:-getdata(P1)[-1], < 0 | 0 > >,
          colour= violet
     )
]);

See command ?assigned.

See ?LinearAlgebra,QRDecomposition.

If you like the way code is entered in Classic Worksheet (I do), then you can select the style "Maple Input" from Standard Worksheet. I also like Code Edit Regions (select from the Insert menu) for sections of code between 5 and 100 lines.

Expanding on my earlier reply, you could take all the usused elements of set X and assign them to NULL or some other default value like the empty set {}.

T:= table([op(2, eval(T))[], (X minus {indices}(T,'nolist') =~ ''{}'')[]]);

Note that that's two nested pairs of single quotes, not one pair of double quotes.

This is a good question. I'm suprised noone has answered it yet.

There is no need to take the derivative inside the procedure. Symbolically, the steps for doing so are the same for any j. By putting it in a procedure, you are forcing it to go through the steps of taking essentially the same derivative every time the procedure is called.

G := (1/2)*(YY[j]-YY[j-1])^2/(XX[j]-XX[j-1])+(1/2)*(YY[j+1]-YY[j])^2/(XX[j+1]-XX[j]);

Gp:= diff(G, XX[j]);

(1/2)*(YY[j]-YY[j-1])^2/(XX[j]-XX[j-1])+(1/2)*(YY[j+1]-YY[j])^2/(XX[j+1]-XX[j])

-(1/2)*(YY[j]-YY[j-1])^2/(XX[j]-XX[j-1])^2+(1/2)*(YY[j+1]-YY[j])^2/(XX[j+1]-XX[j])^2

Now here I evaluate this for specific lists XX and YY, and specific j. I used lists of symbols so that you can see what's going on. But you can use lists (or vectors) of numbers.

X:= [a,b,c,d]:  Y:= [w,x,y,z]:

eval(Gp, [XX= X, YY= Y, j= 2]);

-(1/2)*(x-w)^2/(b-a)^2+(1/2)*(y-x)^2/(c-b)^2

If you want to make this into a procedure, now is the time, and do it with unapply.

GX:= unapply(Gp, [XX,YY,j]);

proc (XX, YY, j) options operator, arrow; -(1/2)*(YY[j]-YY[j-1])^2/(XX[j]-XX[j-1])^2+(1/2)*(YY[j+1]-YY[j])^2/(XX[j+1]-XX[j])^2 end proc

GX(X,Y,2);

-(1/2)*(x-w)^2/(b-a)^2+(1/2)*(y-x)^2/(c-b)^2

 

 

Download FinDiff.mw

By transforming the problem to spherical coordinates, we can reduce the number of independent variables from three to two; this also makes plotting the milieu much easier. A well-constructed plot will lead us directly to the absolute minimum without any ambiguity about local minima.



 

restart;

Digits:= 15:

The objective function:

A:= x^2*y^2/2 + y^2*z^2 + z^2*x^2 + 96/(x+y+z+1);

(1/2)*x^2*y^2+y^2*z^2+z^2*x^2+96/(x+y+z+1)

Note that this is symmetric in x and y.

 

The constraints (or domain) is the sphere of radius sqrt(5) centered at the origin, in the first octant. Knowing that, the constraint equation is not used for the ret of this problem.

 

Set up spherical coordinate transformation.

r:= sqrt(5):

Trans:= [x,y,z] =~ r*~[(cos(theta),sin(theta))*~sin(phi), cos(phi)];

[x = 5^(1/2)*cos(theta)*sin(phi), y = 5^(1/2)*sin(theta)*sin(phi), z = 5^(1/2)*cos(phi)]

Convert the constrained objective to spherical coordinates.

AS:= eval(A, Trans);

(25/2)*cos(theta)^2*sin(phi)^4*sin(theta)^2+25*sin(theta)^2*sin(phi)^2*cos(phi)^2+25*cos(phi)^2*cos(theta)^2*sin(phi)^2+96/(5^(1/2)*cos(theta)*sin(phi)+5^(1/2)*sin(theta)*sin(phi)+5^(1/2)*cos(phi)+1)

Now we have only two independent variables to deal with, phi and theta, rather than three: x, y, z.

 

Plot the domain using the objective function to color the surface.

plot3d(
   [r,theta,phi], theta= 0..Pi/2, phi= 0..Pi/2,
   coords= spherical,
   color= AS,
   orientation= [45,45,0], grid= [25,25], thickness= 0,
   axes= boxed
);

(This plot is much clearer in actual than it appears on MaplePrimes.) The values of the objective function, low to high, correspond to the color spectrum, red to violet. Knowing that, it is clear that there are three local minima---the two red spots and the one yellow spot---and that one of the red spots is the absolute minumum (red being less than yellow). Because of the x-y symmetry, the objective value at each red spot is the same. The situation for maxima is not so clear: They all lie on the border. But we aren't interested in maxima. By counting the grid lines, it is clear that one of the red spots occurs close to theta = 1/4*Pi/2, phi = 22/25*Pi/2.

 

Minimize the objective by solving for where its gradient is 0.

Sol:= fsolve({diff(AS,phi),diff(AS,theta)}, {theta=1/4*Pi/2, phi= 22/25*Pi/2});

{phi = 1.36079017176763, theta = .414107150255695}

And the minimal value is

evalf(eval(AS, Sol));

24.6692515212380

At (x, y, z) =

evalf(eval(Trans, Sol));   

[x = 2.00209163810771, y = .879965268827956, z = .466143967327395]

 

 



Download Min_on_sphere.mw

 

First 374 375 376 377 378 379 380 Last Page 376 of 395