Carl Love

Carl Love

28035 Reputation

25 Badges

12 years, 318 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

I don't think that assuming works with fsolve, but evalc will convert the equations into something that fsolve will handle. In particular, it replaces abs with an equivalent sqrt expression and replaces argument with an equivalent arctan expression.

fsolve(evalc~({eq1, eq2}), {a, z});

     {a = 0.561266211604431e-1, z = .313474043774231}

If x is a decimal number, then the number of places after the decimal point is -op(2,x). This is not necessarily the same as the number of digits of precision of x.

You shouldn't use the same symbol, x, to represent both the unknown in your expression and the initial value of that unknown. I prefer to use x0 to represent the initial value. Here's some corrected code:

simple_iterations:= proc(
   fun::algebraic, x0::complexcons, 
   {epsilon::positive:= 1e-5}, {itermax::posint:= 20000}
)
local N:= indets(fun, And(name, Not(constant))), f, iter, rez, x;
   if nops(N)>1 then error "Too many variables." end if;
   f:= evalf@unapply(fun, [N[]]);
   rez:= x0;
   x:= f(x0);
   for iter to itermax while abs(rez-x) > epsilon do
      rez:= x;
      x:= f(x)
   end do;
   if iter = itermax+1 then error "Did not converge" end if;
   x
end proc:

simple_iterations(1/sin(Pi*x/180), 7);

 

I only have time for a quick Answer right now. The answer to your second question is a definite "no"; it's typical to just use a "large" value to approximate infinity. But the value that you've used is much too large. The values used are typically 5 or 10.

I'll look at your problem in more detail later. One thing that stands out is that none of your IBCs fix the value of x.

Maple's numerical PDE solver is very limited. There must one spatial (space-based) and one temporal (time-based) independent variable, at least implicitly. Which do you consider the spatial, and which do you consider the temporal?

Here is a highly generalizable process for finding the area enclosed by curves. But you must still look at the plot to decide which boundary is which.
 

Problem: Find the area of the region enclosed by y=sqrt(x), x=4 and the x-axis.

 

Solution:

The x-axis is y=0.

Curves:= [y=sqrt(x), x=4, y=0]:  V:= [x,y]:

Use command solve to find intersection points for each pair of curves. The case of one of the curves being a vertical line doesn't need to be treated as a special case.

Pts:= map(solve, combinat:-choose(Curves,2), {V[]});

[{x = 4, y = 0}, {x = 4, y = 2}, {x = 0, y = 0}]

(1)

(minx,maxx,miny,maxy):= (min,max)~((v-> eval~(v, Pts))~(V))[];

0, 4, 0, 2

(2)

xr:= maxx-minx:  yr:= maxy-miny:

plots:-implicitplot(
   Curves, x= minx-xr/3..maxx+xr/3, y= miny-yr/3..maxy+yr/3,
   thickness= 3, gridrefine= 3
);

 

Integrate with respect to x:

(Upper, Lower):= (sqrt(x), 0):

Int(Upper-Lower, x= minx..maxx);

Int(x^(1/2), x = 0 .. 4)

(3)

value(%);

16/3

(4)

There's no need for it here, but just for the sake of generality, I integrate with respect to y also:

solve~(Curves, x);

[y^2, 4]

(5)

(Left,Right):= (y^2, 4):

Int(Right-Left, y= miny..maxy);

Int(-y^2+4, y = 0 .. 2)

(6)

value(%);

16/3

(7)

 


 

Download Area_between.mw

 

A boundary that is a vertical line becomes one of the limits of integration.

If there is any hidden code associated with worksheet, then it can be accessed from the menu Edit -> Startup Code. Many (maybe most) worksheets have their code in plain view right in the worksheet.

Here is how to classify binary matrices by their rank over GF(2).
 

restart:

n:= 4:

#fixed receptacle for Matrix in Modular package form
M:= Matrix((n,n), datatype= float[8], order= C_order):
Alias:= (i,j)-> op(procname)[(i-1)*n+j]: #reindexer from Gray code to square matrix
rank:= 'rank':  #dummy procedure which will be used for its remember table
for p in Iterator:-BinaryGrayCode(n^2) do
   A:= Matrix((n,n), Alias[p]);
   LinearAlgebra:-Modular:-Mod(2, A, M);
   rank(A):= LinearAlgebra:-Modular:-Rank(2,M)        
end do:

#This is a separate procedure only because I want to hide the magic number 4,
#which extracts the remember table of a procedure:
Table:= (P::evaln(procedure))-> op(4, eval(P)):

#Divide the matrices into sets based on rank.
ByRank:= ListTools:-Classify(rank, [indices(Table(rank), nolist)]):

 

The above code runs many times faster than the corresponding code over the complex numbers.

 

Now the set corresponding to each rank is stored in ByRank.

ByRank[0];

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

nops(ByRank[4]);

20160

This count corresponds with the well-known formula for the size of GL(n, q):

nGL:= (n,q)-> mul(q^n-q^k, k= 0..n-1):

nGL(n,2);

20160

nops(ByRank[3]);

37800

``

Download rank_mod_2.mw

If someone knows how to make this more efficient by using ArrayTools:-Alias or by other inplace operations, I would appreciate seeing it.

How about

(T-> (p-> [T[..-2][], p])~(combinat:-permute(T[-1])[..max(2,nops(T[-1])!)/2])[])~(Tours);

But, there must be a better way to your overall goal. If you describe that goal better, maybe I can figure out a better way.

min({L[ ]} minus {0});

I think that you're having a problem that the other respondents aren't because you're saving the module to a library and they aren't. Global variables aren't part of the module and thus won't be saved unless they are explicitly mentioned. There is no need for global variables. Try this:

MonPackageFonctions:= module()
option package;
local GeometricData, r, l, ModuleLoad:= ()-> SetGeometricData(0.2, 1);
export 
   GammaNum:= xx->
      evalf(eval(Pi-arccos((-xx^2+l^2-r^2)/xx/r/2), GeometricData)),
   psiNum:= xx->
      evalf(
         eval(
            -arcsin(sqrt(-(xx^4-2*xx^2*l^2-2*xx^2*r^2+l^4-2*l^2*r^2+r^4)/xx^2/r^2)/2/xx/l),
            GeometricData
         )
      ),
   SetGeometricData:= proc(r::algebraic, l::algebraic)
      GeometricData:= [thismodule:-r= r, thismodule:-l= l];
      NULL
   end proc
;
   ModuleLoad()
end module:

This exports a procedure that lets you set the GeometricData externally but also uses r= 0.2 and l=1 as defaults.

If the file is named myfile.bat then do

system("myfile.bat");

There are many possibilities. The criteria are mostly aesthetic. How about this?
 

restart:

N:= 9:

Dist:= Matrix(
   N$2, shape= symmetric, scan= triangular,
   [0, op]~([[26,15,20,7 ,25,16,24,29],
                [15,23,26,33,40,38,54],
                   [24,13,20,27,35,43],
                      [26,42,34,15,39],
                         [18,14,31,32],
                            [25,49,45],
                               [32,20],
                                  [30]]
)):

d:=<0, 18, 26, 11, 30, 21, 16, 29, 37>:

Procedure "tour_length" calculates the length of a given tour.

tour_length:= proc(tour::list(posint))
   option remember;  
   add(Dist[tour[i],tour[i+1]], i= 1..nops(tour)-1) + Dist[tour[-1],tour[1]]
end proc:

P:= [$2..5]:

Tour2:= ([1,op]~)~(combinat:-setpartition(P));

[[[1, 5], [1, 2, 3, 4]], [[1, 2], [1, 5], [1, 3, 4]], [[1, 3], [1, 5], [1, 2, 4]], [[1, 4], [1, 5], [1, 2, 3]], [[1, 2], [1, 3], [1, 4], [1, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 5], [1, 3, 4]], [[1, 2], [1, 3, 4, 5]], [[1, 2, 4], [1, 3, 5]], [[1, 3], [1, 2, 4, 5]], [[1, 2, 3], [1, 4, 5]], [[1, 4], [1, 2, 3, 5]], [[1, 3], [1, 4], [1, 2, 5]], [[1, 2], [1, 4], [1, 3, 5]], [[1, 2], [1, 3], [1, 4, 5]]]

Tours_Distances:= (tour_length~)~(Tour2);

[[14, 85], [52, 14, 59], [30, 14, 69], [40, 14, 56], [52, 30, 40, 14], [98], [59, 59], [52, 72], [69, 35], [30, 82], [56, 53], [40, 61], [30, 40, 59], [52, 40, 35], [52, 30, 53]]

Vector(Tour2 =~ Tours_Distances);

Vector[column]([[[[1, 5], [1, 2, 3, 4]] = [14, 85]], [[[1, 2], [1, 5], [1, 3, 4]] = [52, 14, 59]], [[[1, 3], [1, 5], [1, 2, 4]] = [30, 14, 69]], [[[1, 4], [1, 5], [1, 2, 3]] = [40, 14, 56]], [[[1, 2], [1, 3], [1, 4], [1, 5]] = [52, 30, 40, 14]], [[[1, 2, 3, 4, 5]] = [98]], [[[1, 2, 5], [1, 3, 4]] = [59, 59]], [[[1, 2], [1, 3, 4, 5]] = [52, 72]], [[[1, 2, 4], [1, 3, 5]] = [69, 35]], [[[1, 3], [1, 2, 4, 5]] = [30, 82]], [[[1, 2, 3], [1, 4, 5]] = [56, 53]], [[[1, 4], [1, 2, 3, 5]] = [40, 61]], [[[1, 3], [1, 4], [1, 2, 5]] = [30, 40, 59]], [[[1, 2], [1, 4], [1, 3, 5]] = [52, 40, 35]], [[[1, 2], [1, 3], [1, 4, 5]] = [52, 30, 53]]])

LimitDist:= (L::positive)-> select(T-> max(tour_length~(T)) <= L, Tour2):

<LimitDist(56)[]>;

Matrix([[[[1, 4], [1, 5], [1, 2, 3]]], [[[1, 2], [1, 3], [1, 4], [1, 5]]], [[[1, 2, 3], [1, 4, 5]]], [[[1, 2], [1, 4], [1, 3, 5]]], [[[1, 2], [1, 3], [1, 4, 5]]]])

``


 

Download Tours.mw

In differential equations in Maple, the dependent variables must always be used in functional form, i.e., x(t) and y(t) rather than x and y. Therefore, your code should be changed to

with(inttrans);
eq5 := diff(x(t), t)+diff(y(t), t$2) = 5*exp(2*t);
eq5s := laplace(%, t, s);
eq6 := diff(x(t), t)-x(t)-(diff(y(t), t))+y(t) = 8*exp(2*t);
eq6s := laplace(%, t, s);
solve({eq5s, eq6s}, {laplace(x(t), t, s), laplace(y(t), t, s)});
subs({x(0) = 2, y(0) = 1, (D(y))(0) = 1}, %);
eq3 := invlaplace(%, s, t);

If you make this simple change, then the last line will give you the answer that you're looking for.

A more-sophisticated approach---which might be useful if you want to re-use this operation a lot---is to use the RandomTools package:

RandomTools:-AddFlavor(
   A = module()
   local r:= rand(-90..90);
   export Main:= ()-> (a-> [a, convert(a*degrees, radians)])(r());
   end module
):
randomize():
n:= 4:
RandomTools:-Generate(list(A, n));

 

First 195 196 197 198 199 200 201 Last Page 197 of 395