# Items tagged with procedureprocedure Tagged Items Feed

### The End of Parameters Marker, $... 6 hours ago 1 2 A large procedure I made caused me some problems. I finally found the source of the problem. There seems to be some interaction between the end of parameters marker,$, and the elementwise operation ~.

Consider these 3 versions of essentially the same procedure:

restart;
Q1:=proc(Var::list,$) local n; n:=nops(Var); print(Var,n); proc(var::list) subs(Var=~[seq(1..n)],var); #Elementwise end proc; end proc: Q1([x,y]); #Doesn't work : "invalid expression" Q2:=proc(Var::list,$) local n;
n:=nops(Var);
print(Var,n);
proc(var::list)
subs(zip(=,Var,[seq(1..n)]),var); # zip
end proc;
end proc:
Q2([x,y]); #No problem
Q3:=proc(Var::list) local n; #No end of parameters marker $n:=nops(Var); print(Var,n); proc(var::list) subs(Var=~[seq(1..n)],var); # Elementwise end proc; end proc: Q3([x,y]); # No problem lprint(Q3([x,y])); #Curious output involving $
#A fourth procedure in which the output is not a procedure works:
Q4:=proc(Var::list,\$) local n;
n:=nops(Var);
print(Var,n);
subs(Var=~[seq(1..n)],Var)
end proc:
Q4([x,y]);

Does anybody have any comments? Is it a bug?

### Write a function that takes an integer and a boole...

March 08 2014
0 1

so we have to Write a maple function with -> that takes an integer N and a boolean function

F: {(i,j) l 0<= i,j<= N} -> {true,false}

and returns a list containing all [i,j] such that F(i,j). A procedure that does this
would be

proc(N,F) local i, j, RV;
RV:=NULL;
for i from 1 to N do for j from 1 to N do
if F(i,j) then RV:=RV,[i,j] ; end if ;
end do ; end do ;
return RV ;
end proc ;

The problem is to do this inline, i.e. you have to write
(i,j)-> ...

February 26 2014
0 1

Dear all,

here, I propose two methods for Adams Moulton Methos, but which one can I used.

The n-step Adams Moulton method to solve y'(x)=F(x,y(x)) is defined by the stencil

y(x+h)=y(x)+h *sum_{j=-1}^{n-1} beta_j F( x-j*h, y(x-j*h) ) + O(h^{n+2})

I want a procedure with single argument ''n'' that calculates and return the ''beta_i'' coefficients

I get two Methods. Which one correspond to my question please, and I don't understand the procedure proposed.

For me; the first give the iterative schemae used, but don't return the vector coefficients ( beta_i) and this methode method an interpolation of the function.

The second method, there is a function f, how this function is maded, and the same for the matrux A and the vector b...

the First Method:

local P, t, f, y, n;

P := interp([t[n]+h, seq(t[n]-j*h, j = 0 .. k-2)], [f[n+1], seq(f[n-j], j = 0 .. k-2)], x);

y[n+1] = y[n]+simplify(int(P, x = t[n] .. t[n]+h))

end proc;

Second Method:

f:=proc(x,y) if x =0 and y=0 then 1 else x**y fi end;

n:=3; A:=matrix(n,n,(i,j)->f(1-j,1-i));

b:=vector(n, i->1/i);

linsolve(A,b);

### Binomial coefficients...

February 22 2014
1 4

I want to compute binomial coefficients mod 2 in Maple.  My numbers are getting big and the standard function binomial(n, k) is very slow. Is there a maple procedure or function which will be fast for computing this mod 2?  (I know how to do this theoretically, but not in maple.) Thank you for your help.

### Write a procedure whose input is a binary list an...

February 21 2014
1 5

m:=proc(n::list)
local N, S, i:
N:=nops(n);
S:={};
for i from 1 to N do
if n[i]=1 then do
S:=S union {i}; break; od; fi; od;
for i from 1 to N do
if n[i]=0 then do
S:=S union {}; break; od; fi; od;
end proc;

the procedure works if the last member of a list is 0, for example

m(1,0,1,0);

returns {1,3}

but if it ends in 1, nothing gets returned, example: m(1,0,1);

I can also get it to work the other way around, where it'll return the set if the list ends in 1 but not zero. I need it to work for both.

### Using numerical sol of ODE in another ODE...

February 19 2014
0 2

Say we solve numerically and ODE using Maple. Say ode1:= { diff(Q(x),x)= Q(x)/3x , Q(1)=1 }

The solution is a procedure so now suppose we have another ODE where the solution appears.Say  ode2:= { diff(f(x),x)= Q(x)*x , f(1)=4}.

To extract the solution of the first ODE I set sol1:=dsolve(ode1,numeric) and Q:=proc(x) local s: return rhs(sol(x)[2]): end proc:

But now I got an error message when I trying sol2:=dsolve(ode2,numeric).

Is it possible to use a procedure in the definition of the ODE one wants to solve?

### Ecountered : Error, unterminated procedure in Mapl...

February 18 2014
2 10

Hi Mapleprimes community,

I am a maple novice and was trying to write a procedure but cannot tell what my syntax error is.  I would greatly appreciate if anyone can point out what I did wrong or any improvement in the code that I can make.  Thanks

SLRrepeatedsample_pr.mw

Henry

### Composite Simpson's 3/8 Rule...

February 18 2014
1 3

I am trying to create a procedure that can solve integrals using the Composite Simpson's 3/8 rule. However when I test my procedure against maple's ApproximateInt I am getting the wrong results.

Here is my attempt:

restart;

f:= x -> exp(x)*sin(4*x); # function I am using

simp := proc(a, b, n)
local h, sum, i, single:
h := (b-a)/n:
sum := 0:
single := (3*h/8) * (f(a) + f(b)): # this is the end points
for i from a+h by h to b-h do
sum := sum + (3*h/8) * (3*f(i)):
end do:
print(evalf(sum + single));
end proc:

simp(0,1,12);
0.6224486445
evalf(Student:-Calculus1:-ApproximateInt(f(x), 0..1, method = simpson[3/8], partition=12));

0.5323516717

As you can see my answer is not very close to the answer given by Maple. I am not sure why my procedure simp is wrong.

### Procedure for Array using recursion....

February 17 2014
0 6

I'm trying to create and populate the Array A with a sequence of lower triangular matrices based on the following relation:

A[k+1][i,j] = a (1+k) A[k][i,j] + b ((1+j) A[k][i-1,j] - j A[k][i-1,j-1])

The rows and columns of each matrix are given by i and j; a and b are scalars. The structure of A is not important provided a matrix can be accessed for a given k. The highest k value will not be more than about 100.

The starting matrix for k=1 is

[-1  0]
[ 1 -1]

Any guidance on where to start with setting up the appropriate procedure would be greatly appreciated.

Thanks.

### Orbit of 2x2 matrix through a given list of points...

February 16 2014
1 7

okay so im quite confused, im trying to write a procedure that prints the orbit of a 2x2 matrix through a given list of points.  our arguments should be a a (2x2) matrix, a list L of points represented by 2 vectors, ie (2x1) matrices and a natural number N. Returnvalue : NULL

a side effect should be to

A^i.L[j] for i = 0 ... N and j = 1 ... nops(L)

thus, an example, if the proc is called P, then

P(Matrix([[0,-1],[1,0]]),[Matrix([[1],[1]]),Matrix([[1],[0]])],2)

should plot the points

(1,1) ,(-1,1) , (-1,-1)  for the 1st point  and

(1,0)  ,(0,1) , (-1,0) for the second

thanks for any help in advance

### how to solve matrix equation problem in maple?...

February 14 2014
0 2

solve matrix equation Ax+xB=C ? where X_(n\times n), and  A,B,C \ in M_n(R) we are given. write maple procedure and check by a 3* 3 case.

Hint( USE MATRIX  CRAMER RULE), OR GAUSS-ELIMINATION.

### interpolation in a procedure...

February 10 2014
2 3

Hi I have been trying to get the inperpolation of some values within a procedure without much success. Maybe someone in mapleprimes might know this already.

Basically what I am trying to do is to generate a matrix of values. The values of the columns 1 to 3 are just some values, but then I want column 4 to be the result of the interpolation of some values.

I got different errors such as:

Error, (in CurveFitting:-ArrayInterpolation) data must consist of real, floating point values

Anyway the procedure is shown below:

InterProc := proc ()

local i; global A, global variable1, global variable2;

A := Matrix(1 .. 4);

variable1 := [0, 100];

variable2 := [12, 20];

with(CurveFitting);

for i from 1 to 10 do

A(i, 1) := 3+i;

A(i, 2) := 2*i+A(i, 1);

A(i, 3) := A(i, 2)-1;

A(i, 4) := ArrayInterpolation(variable1, variable2, A(i, 1));

end do;

end proc;

InterProc();

### Procedure to produce array of parameters and numer...

February 07 2014
1 3

I have defined the following procedure, S(x,a,b,s), in Maple with the goal of creating an exportable two column, multi-row array, containing the least positive real root of a high order polynomial f(x,y)=0 in the 2nd column, and a parameter y in the first column.

The procedure takes four numerical arguments (x,a,b,s) and varies parameter y from the initial non-negative value of a, by stepsize s, until the value min(b,1) is reached.

Unfortunately, the output 4x2 array only has the last calculated [y,solution] entries in the first row. Successive rows are filled with zeros.

Is there anyone kind enough to point out the error in the way I have defined this procedure? Many thanks in advance. Procedure is:

S := proc (x, a, b, s
global Ry;
for y from a by s to b while y < 1
do R := Array(1 .. ceil((min(b, 1)-a)/s), 1 .. 2, [[y, FindMinimalElement(select(type, [fsolve(f(x) = 0)], positive))]])
end do;
end proc;

### how to Contrast the behavior of functions and expr...

January 31 2014
0 3

Contrast the behavior of functions and expressions by performing the following commands.
a)Define a function f equal to x^3 and Define an expression g equal to x^3
b) Evaluate f and g at 2.
c) Evaluate f and g at y.
d )Assign the value 2 to x. Evaluate f and g.

### How do I save a package?...

January 23 2014
2 5

Hi everyone

I am currently trying to make my own simple package including a few procedures. So far I have been able to write some "code" that actually works when I open the document and hit "enter". I would, however, like to save the package so it can be accessed during any Maple session using the command "with". I have unsuccesfully tried to comprehend the Maple help pages regarding this question but I definitely don't want to mess things up.

This is what I have written:

mat := module ()
description "useful procedures for mathematics, physics and chemistry";
export AtomicWeight;
option package;

AtomicWeight := proc (x) description "returns the average atomic mass of the naturally ocurring element";
return evalf(ScientificConstants:-Element(x, atomicweight, system = NewSystem, units))
end proc

end module;

What should I do to save it correctly?