Items tagged with loop loop Tagged Items Feed

AOA... Pl correct it

How can I write a general procedure which will take an integer n and create n nested loops:


Thank you in advance.



I wish to be able to calculate the roots of the function f(p) by using the roots of the function h(p) and applying the bisection method due to the fact that the roots of h(p) bracket the roots of f(p) as can be seen in the graph below. I have done this before for another example when h:=J0(p); and hence i could use The commands BesselJZeros(0,n)/BesselJZzeros(0,n+1) to find the roots. So my problem arises with the finding the roots of h(p) and how to insert them into my bisection loop(underlined below).

Any advice would amazing. Many thanks


Digits := 30:
with (plots):

#Define given parameters

R:=1: #external radius of particles, cm

d:=10^(-3): #diffusivity cm^2 per second

alpha:= 1: #fractional void volume

c0:=10^(-6): #concentartion of soltion in void volume of solid initially, moles per liter

C0:=0: #concentration of main body of solution initially, moles per liter

k1:=0.5: #constant in adsorption isotherm (ka)

k2:=0.75: #constant in adsorption isotherm (kd)

k:=2.5: #equilbrium constant for adsorption kinetics

n0:=(k1/k2)*c0:#initial amount absrobed on solid, moles per liter

V:=0.1: #volume of external solution, liters

W:=0.1: #weight of absorbant, grams

rho:=2.0: #solid aparrant density, g/cc












f:=p->(BesselJ(0,R*sqrt(-delta*p))*k*p-(R*sqrt(-delta*p))*BesselJ(1,R*sqrt(-delta*p))*(d*p/R + 2*beta*k/(R^2)));

proc (p) options operator, arrow; BesselJ(0, R*sqrt(-delta*p))*k*p-R*sqrt(-delta*p)*BesselJ(1, R*sqrt(-delta*p))*(d*p/R+2*beta*k/R^2) end proc




proc (p) options operator, arrow; BesselJ(0, R*sqrt(-delta*p)) end proc





rts:= Array(1..points):
for n from 1 by +1 to points do
pl:=evalf(#**first root of h**);
pu:=evalf(#**second root of h, i.e n+1 root**);
pe:= (pl+pu)/2;
while abs(f(pe))>10^(-6) do
if f(pu)*f(pe) <0 then
elif f(pl)*f(pe)<0 then
end if;







Hi Maple Prime-ers!

I have a question about efficiency.  I have a set of algebraic equations with some polynomials, that I would like to solve at different points.  I've tried using a for-loop and a map-loop.  Here is a example:


n:=10000;  #Number of solving points
eq1:={b = ''a^2'', c = b^3/2, d = c^(1/2)*4 + b^2}; #Equation to solve

a := convert([seq(i,i=1..n)],Vector);  #timesteps

ans := Vector[column](n)

## Try solving in a for-next loop
t1 := time():
for q from 1 to n do
ans(q):=solve(subs({'a' = a(q)},eq1)):
t2 := time() - t1;

## try solving in a map loop
t1s := time():
ans_s := map(q->solve(subs({'a' = a(q)},eq1)),a);
t2s := time() - t1s;

On my computer (2.2Ghz, 2 cores), these both take 115s to solve.  Using Map over For-Next did not speed up computational speed.  

The problem I wish to tackle has 12 equations, invovles 5th order polynomials, and n ~= 300000.  Solving this set of equations takes 2-3 hours.

Anyone know a more efficient method?  Thanks for reading :D


I have an Array that has both floats and pi and e expressions mixed in.  Would like to scan the Array and print the non-floats to the screen.

My if then statement works until I put it into a loop then I get no output.  Puzzled...

> hits := identify(d);
> for i from 1 to 501 do if type(Hits[i], 'float') = false then Hits[i] end if end do;

Hello everyone,


xNData := ExcelTools:-Import("D:/a.xls"):

L := convert(xNData, listlist);

L := [[1.0, 0.75e-1], [2.0, .1], [3.0, .12], [4.0, .14], [5.0, .16], [6.0, .18], [7.0, .2], [8.0, .22], [9.0, .24], [10.0, .26]]

In the above L for each x we have N. I want to sub a single set of value [x,N] into THE equation NN to get a value for d and repeat the same process for each set of [x,N] to get d.  

NN := -N+(N0B-NB)*(erf((1/2)*x/(sqrt(t)*sqrt(d)))-sqrt(erf((1/2)*alpha/d)))/sqrt(erfc((1/2)*alpha/d))+NB;



@Care Love, sorry for the trouble. I have edited the question.





N := N0-(1/2)*sqrt(2)*sqrt(Pi*Kc/d)*(sum(erfc((1/2)*(L*n+x)/sqrt(d*t))+erfc((1/2)*((n+1)*L-x)/sqrt(d*t)),

n = 0 .. infinity));


When I plot N vs x = 0..0.25 then there is no issue


but when try to use a loop to get the data, Maple cannot evaluate

for x from 0 to 0.25 by 0.01 do

end do;



Or I didn't get something right? This is with Maple 16.02 on a Mac:

maple worksheet snapshot


> for i from 1 to 10 while i<=5 do print(i) end do; #works all right
> for i from 1 to 10 while i>=5 do print(i) end do; #nothing shows up?

Loop help required...

December 17 2013 J4James 175

Helloo everyone,



Now the question is how we can get data in the following form 

a      b     c      d         dif(f(eta),eta$2) at eta=0

1      1     1      1                  0.1

1     2      1      1                   0.2

1     2      2       1                   0.3

1     2     2      2                      0.4

2      1     1      1                  0.5

2     2      1      1                   0.6

2     2      2       1                   0.7

2     2     2      2                      0.8

In the above table, I want to vary a, b, c, d and to find out the values from the ode for dif(f(eta),eta$2) at eta=0

Here is my try but no luck


sol:= (a1,b1,c1,d1)->dsolve({bc,eq1}), numeric,output = array([0]);

subs(sol(a1,b1,c1,d1):-value()(a1,b1,c1,d1),Vector[row]([a1,b,c,d,rhs(sol[3])])) #dif(f(eta),eta$2) at eta=0 is called as rhs(sol[3])
end proc;

ha:=.01: hb:=.1: hc:=0.1: hd:=0.1: #Increments in a, b, c and d, respectively
Ia:=2: Ib:=2: Ic:=2: Id:=2: #Number of increments
A:=Matrix(ha*hb*hc*hd,5); #Rows: [a,b,c,d,dif(f(eta),eta$2) at eta=0]


for i from 1 to Ia do
for j from 1 to Ib do

for l from 1 to Ic do

for k from 1 to Id do
end do

end do

end do
end do;






Hi, I am trying to write a procedure that inputs two matrices (A,B) and vector (C), it then multiplies C by A, and thenC by B and adds these to the array, it then multiplies each other them by A and B and adds them to the array etc up to (N-1)^2 (N will be given), this is what I hve so far, I am trying to write an if loop inside to say if it's already in the array then do not add it again but i'm unsure. Any help would be appreciated thanks!


local x, S, i, j, T, R, y;
while (i<(2^N)) do
for y from 0 to j do
if (S[y]=T) then
end proc:

how to do loop in loop...

December 03 2013 miname 15

Hello every one,

I am facing problem with for loop. Please have a look.


let say I have T[0]:= 99 ; X[0]:=0; f(x):=108 + 2 x

  for i from 1 to 2 do f(x[i-1] ):

                              y[i]:=x[i-1] +1 ;

if f(y[i])<f(x[i-1]) then x[i]:=y[i] elif 0.98< e0.99/T[0]  then x[i]:=y[i] else x[i]:=x [i-1]

end if;

end do;

after I got the answer from this loop, now I want to continue the loop for T[1]:= 0.1 T[0], 

 how  to do another loop by using the same  information but in different value of T? in other words, the only change in the looping is the value T so that the next loop I have the set of answer.




Help with loops...

November 20 2013 jem4303 5

I am trying to work with a for do loop with a while component. I have the error but I am not sure what the desired error should be. I know it has to be small but how small?



Im trying to plot the different values of shanks as points in this ] loop.


for i from 1 to 60 do

if(n>2) then sumnm1:=sumn fi;

if (n>1) then sumn:=sumnp1 fi; p:=1/evalf(2*beta*BesselJ(2,zeros[i])+epsilon*zeros[i]*BesselJ(0,zeros[i])+zeros[i]*BesselJ(1,zeros[i])); pp:=pp+p;


if (n>2) then shanks:=(sumnp1*sumnm1-sumn*sumn)/(sumnp1-2*sumn+sumnm1) fi;



sumnp1: -2*sumn+sumnm1:

I have tried plotting it but it only plots the last value. 

Many thanks James

Problem with Nested loop...

November 13 2013 karthik88 5

I am using maple 17 and I was trying to run a nested loop as shown below. I expected to get all combinations of i and j (9 components). But i and j always remain 3. What is the problem here? Can anyone suggest alternate solutions? I dont understand in which order maple increments i,j or k in each loop.


for k to 9 do

      for i to 3 do

          for j to 3 do

          a[k] := i, j

          end do

      end do

end do;




Thanks in advance

how the function if work?...

November 01 2013 miname 15


 I have a problem why the result does not code are:

> P := array([[8, 4], [8, 3], [8, 2], [7, 1], [6, 0], [5, 0], [4, 0], [2, 1], [1, 1], [1, 4]]);

> for j from 2 to 5 do k[j] := j+1;

x[j] := add(P[j, 1], j = j-1 .. j+2);

X[j] := add(P[j, 1]^2, j = j-1 .. j+2);

y[j] := add(P[j, 2], j = j-1 .. j+2);

Y[j] := add(P[j, 2]^2, j = j-1 .. j+2);

xy[j] := add(P[j, 1]*P[j, 2], j = j-1 .. j+2);

cx[j] := evalf(x[j]/k[j]);

cy[j] := evalf(y[j]/k[j]);

c11[j] := evalf(X[j]/k[j]-cx[j]^2);

c22[j] := evalf(Y[j]/k[j]-cy[j]^2);

c12[j] := evalf(xy[j]/k[j]-cx[j]*cy[j]);

C[j] := evalf(Matrix(2, 2, [[c11[j], c12[j]], [c12[j], c22[j]]]));

E[j] := Eigenvalues(C[j]);

if E[j][1] > E[j][2] then lambda[j] := E[j][2]/(E[j][1]+E[j][2]) else lambda[j] := E[j][1]/(E[j][1]+E[j][2])

end if;

end do;

the If function does not work and the result does not appear. Is there any problem in my code.



1 2 3 4 5 6 7 Page 1 of 8