Items tagged with proc proc Tagged Items Feed

Hi all,
I am writing a procedure that I would like to have several outputs. The code is as follows:

Initialize := proc (p, theta, PiV, PiU, n, m)

J := CompJ(PiU, m);

for i to n do

if member(i, V) then

Ai[i] := CompAi(PiU, PiV, i, n, J)

end if

end do

end proc

The function calls other functions previously defined that are not important here. Basically I would like to return the set J and the list Ai, but I am not sure on how to do it. 

Once the procedure returns these elements, I would like to assign them to some variables. I am not sure on how this can be done when a function returns more than one thing.

Thanks in advance for the help
Bests
Manuele 

Sorry if this has been already posted.

 

When print() is invoked from a proc into a module, non-English characters are not properly displayed with Maple 18.

It works ok if it is invoked from within the workbook.

 

Example:
print("Están en perspectiva")

Put this sentence in a proc into a module and the character "á" wont be displayed

Output: "Est�n en perspectiva"

Any hint about how to treat this issue?

Thank you very much.

César Lozada 

 

 

Hi,

I want to display the matrix M, I used return M, in this procedure, but no matrix M displayed.

restart;
N:=2:
N:=2;
ff:=proc(N)
local M,i,p;
M:=array(1..2*N+1,1..2*N+1):
for i from 1 to 2*N+1 do
for  p from 1  to 2*N+1  do
   if p=1 then M[p,i]:=-2;
    elif  p=2*N+1  then M[p,i]:=-3;
else
M[p,i]:=0;
end if; end do; end do;
return M;
end proc;

 

Many thinks


firstly apologies in advance for stuff in this question such as "triangle symbol",  my computer is pretty old. 


ok so i was confused a bit here, what i'm trying to do is write a maple procedure that computes Af for a given f contained in V . except we only need to correct the bug in the script below. This script demonstrates such a procedure in the case that omega is a square. The domain is given here as the negative set of a function F contained in V .  I have left in notes where/what i think we need to do but i dunno how to...

N:=10 ; # Global Var
F:=(x,y)->sgn(abs(x-N/2)+abs(y-N/2)-N/4);
Average := proc(F, f0) local f, i, j;
f := f0; # !!!!!!!!!!!!!! something is bad here...
for i to N do for j to N do
if F(i, j) < 0 then
f[i, j] := (f0[i - 1, j] + f0[i + 1, j] + f0[i, j + 1] + f0[i, j - 1])/4 ;
end if;
end do;end do;
return f;
end proc;
f0:=Matrix(N,F); # just to have something to test the procedure
Average(F,f0); # does not return the expected average, modifies f0

 

the necessary information we were given to produce this so far was..

Let N be a positive integer and [N] = {i contained in N | 1<= i <=N }  Let "Omega" C {(i,j) contained in [N] x [N] | 2<=i,j<=N-1} be a subset. Let V = R^([N]x[N]) be the vector space of real valued functions [N]x[N] -> R
and A, "triangle symbol":V->V (average) and "triangle symbole" (Laplacian) be the linear maps such that
[Af](i; j) = f(i; j)      if (i; j) not contained in "Omega"   OR

                             [f(i, j + 1) + f(i, j - 1) + f(i + 1, j) + f(i - 1, j)]/4 if (i,j) is contained in "Omega"

["traingle symbol"f](i,j) =  0 if (i,j) isnt contained in "Omega"   OR

                            ( f(i,j) - [f(i, j + 1) + f(i, j - 1) + f(i + 1, j) + f(i - 1, j)]/4 )    if (i,j) is contained in "Omega"

 Please and thank you for any help in advance <3

                           

 


Dear the specialst in programmation, please I need to put a conditon if in this procedure with this form: Can you correct me this code please. Thank you.


aaa:=proc(x,y,f,N)
local bb,
for i= from 1 to N do

bb[i]:=f(x[i],y);

if bb[i]<=2 then y:=3*x[i] else y::=x[i]^2end if ;

end do:

end proc

 

 

Dear all;

Please I need your help to find the error in my code.

I want to solve an ode, with condition on step size.

ode := diff(y(x), x) = 2*x+y(x);
f:=(x,y)->2*x-y;

analyticsol := rhs(dsolve({ode, y(0) = 1}));
RKadaptivestepsize := proc (f, a, b, epsilon, N)
local x, y, n, h,k,z,R,p;
p:=2;
h := evalf(b-a)/N; ## we begin with this setpsize
x[0] := a; y[0] := 1; ## Initialisation
for n from 0 to N-1 do  ##loop
x[n+1] := a+(n+1)*h;  ## noeuds
k[1] := f(x[n], y[n]);
k[2] := f(x[n]+h, y[n]+h*k[1]);
k[3] := f(x[n]+h/2, y[n]+h/4*(k[1]+k[2]));
z[n+1] := z[n]+(h/2)*(k[1]+k[2]);## 2-stage runge Kutta.
y[n+1] := y[n]+(h/6)*(k[1]+k[2]+4*k[3]);
R:=abs(y[n+1]-z[n+1]); ## local erreur
hstar:=sqrt(epsilon/R)
if R=<=epsilon    then
   x[n] := x[n+1]+h;
   y[n]:=y[n+1];
   n:=n+1;

else

h:=hstar;
end if
 end do;
[seq([x[n], y[n]], n = 0 .. N)];
[seq([x[n], z[n]], n = 0 .. N)];
end proc:

epsilon:=1e-8;
RKadaptivestepsize((x,y)->2*x-y,0,1, epsilon,20)

 

 

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)-> ...

 

please help...

I am trying some data analysis on an Excel worksheet using Maple. I have the following procedure but I don't know how to make it work for a specific subset of cells (this is a psychological studiy, so I want to have all my scores for each subject, located on the rows, and variable, located on a subset of columns). Example: I want to get an array of all the average scores in a rectangle of cells which goes horizontallyfrom A to AB, and vertically from row 1 to 15. Hope you can help me out! Cheers!

MediaRighe := proc (M) local i, j, r, c, S, N; r := LinearAlgebra:-RowDimension(M); c := LinearAlgebra:-ColumnDimension(M); S := Array(1 .. r); for i to r do S[i] := 0; N := 0; for j to c do if M(i, j) <> "-" then S[j] := S[j]+M(i, j); N := N+1 end if end do; S[i] := S[i]/N end do; return S end proc

I also have this for the columns (same thing, basically):

MediaColonne := proc (M) local i, j, r, c, S, N; r := LinearAlgebra:-RowDimension(M); c := LinearAlgebra:-ColumnDimension(M); S := Array(1 .. c); for j to c do S[j] := 0; N := 0; for i to r do if M(i, j) <> "-" then S[j] := S[j]+M(i, j); N := N+1 end if end do; S[j] := S[j]/N end do; return S end proc

Thanks everybody!

I am a new user of Maple. Could anyone help me to know how to call a Maple function/procedure from a Matlab program with a simple example? And conversely, how to call a Matlab function from Maple.

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";
   Units:-AddSystem(NewSystem, Units:-GetSystem(SI), u);
   return evalf(ScientificConstants:-Element(x, atomicweight, system = NewSystem, units))
   end proc

end module;

What should I do to save it correctly?

Thank in advance,
Mads


 

 

 

 

Please help me, here my problem:

 

Parsing a string works correctely like this:

ABC := 3443;

print("ABC = ", eval(parse(cat("A", "B", "C"))));

                

 

But using PROCEDURE it doesn't work:

myParse := proc ()

     local PEA;
   

      PEA := 4334;

      print("PEA = ", eval(parse(cat("P", "E", "A"))))

end proc;

myParse();

                        "PEA = ", PEA

 

I suppose to have "PEA = ", 4334

Following previous question at

http://www.mapleprimes.com/questions/149581-Improve-Algorithm-Dsolve

and also

http://www.mapleprimes.com/questions/149243-BVP-With-Constraining-Integrals

I wrote the following code

***********************

restart:

gama1:=0:


phi0:=0.00789:


rhocu:=2/(1-zet^2)*int((1-eta)*rho(eta)*c(eta)*u(eta),eta=0..1-zet):

eq1:=diff(u(eta),eta,eta)+1/(mu(eta)/mu1[w])+((1/(eta-1)+1/mu(eta)*(mu_phi*diff(phi(eta),eta)))*diff(u(eta),eta)):
eq2:=diff(T(eta),eta,eta)+1/(k(eta)/k1[w])*(2/(1-zet^2)*rho(eta)*c(eta)*u(eta)/(p2*10000)+( (a[k1]+2*b[k1]*phi(eta))/(1+a[k1]*phi1[w]+b[k1]*phi1[w]^2)*diff(phi(eta),eta)-k(eta)/k1[w]/(1-eta)*diff(T(eta),eta) )):
eq3:=diff(phi(eta),eta)-phi(eta)/(N[bt]*(1-gama1*T(eta))^2)*diff(T(eta),eta):
mu:=unapply(mu1[bf]*(1+a[mu1]*phi(eta)+b[mu1]*phi(eta)^2),eta):
k:=unapply(k1[bf]*(1+a[k1]*phi(eta)+b[k1]*phi(eta)^2),eta):
rhop:=3880:
rhobf:=998.2:
cp:=773:
cbf:=4182:
rho:=unapply(  phi(eta)*rhop+(1-phi(eta))*rhobf ,eta):
c:=unapply(  (phi(eta)*rhop*cp+(1-phi(eta))*rhobf*cbf )/rho(eta) ,eta):
mu_phi:=mu1[bf]*(a[mu1]+2*b[mu1]*phi(eta)):

a[mu1]:=39.11:
b[mu1]:=533.9:
mu1[bf]:=9.93/10000:
a[k1]:=7.47:
b[k1]:=0:
k1[bf]:=0.597:
zet:=0.5:
#phi(0):=1:
#u(0):=0:
phi1[w]:=phi0:
N[bt]:=0.2:
mu1[w]:=mu(0):
k1[w]:=k(0):

eq1:=subs(phi(0)=phi0,u(0)=0,eq1):
eq2:=subs(phi(0)=phi0,u(0)=0,eq2):
eq3:=subs(phi(0)=phi0,u(0)=0,eq3):

p:=proc(pp2) global res,F0,F1,F2:
if not type([pp2],list(numeric)) then return 'procname(_passed)' end if:
res := dsolve({eq1=0,subs(p2=pp2,eq2)=0,eq3=0,u(0)=0,u(1-zet)=0,phi(0)=phi0,T(0)=0,D(T)(0)=1}, numeric,output=listprocedure):
F0,F1,F2:=op(subs(res,[u(eta),phi(eta),T(eta)])):
evalf(2/(1-zet^2)*Int((1-eta)*(F1(eta)*rhop+(1-F1(eta))*rhobf)*( F1(eta)*rhop*cp+(1-F1(eta))*rhobf*cbf )/(F1(eta)*rhop+(1-F1(eta))*rhobf)*F0(eta),eta=0..1-zet))-pp2*10000:
end proc:


s1:=Student:-NumericalAnalysis:-Secant(p(pp2),pp2=[6,7],tolerance=1e-6);

                   HFloat(6.600456858832996)

p2:=%:



ruu:=evalf(2/(1-zet^2)*(Int((1-eta)*F0(eta),eta=0..1-zet))):
phb:=evalf(2/(1-zet^2)*(Int((1-eta)*F0(eta)*F1(eta),eta=0..1-zet))) / evalf(2/(1-zet^2)*(Int((1-eta)*F0(eta),eta=0..1-zet))) :
TTb:=evalf(2/(1-zet^2)*(Int((1-eta)*F2(eta),eta=0..1-zet))):
rhouu:=evalf(2/(1-zet^2)*(Int((1-eta)*(F1(eta)*rhop+(1-F1(eta))*rhobf)*F0(eta),eta=0..1-zet))):
with(plots):
res(parameters=[R0,R1]):
odeplot(res,[[eta,u(eta)/ruu],[eta,phi(eta)/phb],[eta,T(eta)/TTb]],0..zet);

 

*************************************

as you can see at the second line of the code, the value of phi0:=0.00789. however, I want to modify the code in a way that phi0 is calculated with the following addition constraint

evalf(2/(1-zet^2)*(Int((1-eta)*F0(eta)*F1(eta),eta=0..1-zet))) / evalf(2/(1-zet^2)*(Int((1-eta)*F0(eta),eta=0..1-zet)))-0.02=0

I would be most grateful if you could help me in this problem.

Thanks for your attention in advance

Amir

Hi, I am currently creating a procedure so that i can input two matrices and a vector and multiply them together in different patterns, I want the result to be either one of the vectors {4,0,0,0} or {0,4,0,0} or {0,0,4,0} or {0,0,0,4}, I am trying to create an if loop that says is one of the S's is equal to one of these print ("Synchronising"), I have decalred these vectors inside the procedure and all the S's but my if statement is still not working. I think S needs to be decalred more clearly inside but i'm unsure what to do, any help would be much appreciaited! Here is my procedure:

 

proc_cerny1:=proc(A::Matrix,B::Matrix,C::Vector)
local s, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, e, f, g, h;
s:=C.A;
s1:=C.B;
s2:=C.A.B;
s3:=C.B.A;
s4:=C.A.A;
s5:=C.B.B;
s6:=C.A.A.A;
s7:=C.B.B.B;
s8:=C.A.B.B;
s9:=C.A.B.A;
s10:=C.B.B.A;
s11:=C.B.A.A;
s12:=C.A.A.B;
s12:=C.B.A.B;
s13:=C.B.A.A;
s14:=C.B.A.A.A;
e:=Vector([4,0,0,0],orientation=row);
f:=Vector([0,4,0,0],orientation=row);
g:=Vector([0,0,4,0],orientation=row);
h:=Vector([0,0,0,4],orientation=row);
g:=Vector([0,0,4,0],orientation=row);
if (s7 = e)then
print("Synchronising")
elif (s7 = g) then
print ("Synchronising")
elif (s7 = h) then
print ("Synchronising")
elif (s7 = g) then
print("Synchronising")
else
print("Not Synchronising");
end if;

return s, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14;
end proc:

 

Thanks 

Hi there!

I want to improve the way I code up my procedures. For instance I want to code them safe in the sense that they only accept the arguments when they have the correct type.

I know that in principle this is done with the double-colon operator in the following way:

proc(argument::argumenttype)::valuetype;

 

(Q1)Now, given that my argument is of a type such as set, list, Array, etc.:

(a) How would I specify the allowed types of the elements of that list (for all elements, and for specific ones)? And if the element of the list is a list again, how would I specify the type of that's elements? and so forth..

(b) How would I specify the allowed size of the listArray, etc. ?

 

(Q2) How do I specify the types of more than one output, if my proc returns more than one value?

 

(Q3) I know that there are special integer types, such as nonnegint, posint, negint. Is there also a way to specify that the ineger (or float) argument has to be greater than some given value, e.g. >2, or that it has to lie between two values, or out of a given set?

 

Thats all for now. Thanks for your help guys! : )

Gernot

I would like to take the sin of a procedure, but it keeps giving me an error. Is it possible at all, or should I rewrite it into a matrix?

like this: sin(proc(x)...end proc)

1 2 3 4 5 6 Page 1 of 6