Items tagged with loop loop Tagged Items Feed

AOA... Pl correct it

Help.mw

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

add(...(add(f(x_1,...,x_n),i_1=0..k_1),...),i_n=0..k_n);

Thank you in advance.

Hi, 

 

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


restart;

Digits := 30:
with (plots):
with(RootFinding):

#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

delta:=(1/d)*((p+alpha*k2+k1)/(p+alpha*k2));

beta:=W*alpha*d/(rho*V);

   

1000*(p+1.25)/(p+.75)

 

0.500000000000000000000000000000e-3

 

 

 

 

 


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

(2)

h:=p->(BesselJ(0,R*sqrt(-delta*p)));
plot([f(p),h(p)],p=-0.3..0,axis=-5..5,legend=["f(p)","h(p)"]);


 




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

 

 

 

(3)



points:=5:
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
pe:=(pu+pl)/2;
if f(pu)*f(pe) <0 then
pl:=pe;
elif f(pl)*f(pe)<0 then
pu:=pe;
end if;
od;
rts[n]:=pe;

od;
rts[n]:=p[n];

 

 

Download spherical_continue.mw


 

Download spherical_continue.mw

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)):
od:
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,

restart:with(plots):

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;

alpha:=2*10^(-12):NB:=0.075:N0B:=0.2:t:=360000:

Cheers.

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

a.xls

 

Hello,

restart:

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

N0:=0.2:L:=0.25:Kc:=2*10^(-12):t:=360000:d:=2.010619298*10^(-10):

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

plot(N,x=0..0.25,axes=box);

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

N[x]:=evalf(N0-sqrt(Pi*Kc/(2*d))*sum(((((erfc((n*L+x)/(2*sqrt(d*t))))+erfc(((n+1)*L-x)/(2*sqrt(d*t))))),n=0..infinity)));
end do;

Thanks

  

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,

eq1:=((diff(f(eta),eta$3)))-a*diff(f(eta),eta$1)^2+b*f(eta)*diff(f(eta),eta$2)=0;

bc:=f(0)=0,D(f)(0)=1+c*(D@@2)(f)(0),D(f)(8)=d:

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]);

p:=proc(a1,b1,c1,d1)
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]

r:=0:

for i from 1 to Ia do
q:=sol(i*ha);
for j from 1 to Ib do
v:=q:-value(b=j*hb);

for l from 1 to Ic do

w:=v:-value(c=j*hc);
for k from 1 to Id do
r:=r+1;
A[r,..]:=subs(v(hd*k),subs(w(hc*l),Vector[row]([i*ha,b,c,d,rhs(sol[8])])))
end do

end do

end do
end do;
time()-t0;

interface(rtablesize=infinity);

 

A;

 

Thanks

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!

 

proc_cerny1:=proc(A::Matrix,B::Matrix,C::Vector,N)
local x, S, i, j, T, R, y;
x:=(N-1)^2;
S:=Array([]);
S[0]:=C;
i:=0;
j:=0;
while (i<(2^N)) do
T=S[i].A;
S[j]=T;
j=j+1;
R=S[i].B;
S[j]=R;
i=i+1;
for y from 0 to j do
if (S[y]=T) then
S[i]=S[i]-T;
fi:
od:
print(S);
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.

thanks.

 

 

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?

 

Thanks

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

pp:=0.0:sumn:=0:sumnm1:=0:sumnp1:=0:

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;

sumnp1:=pp;

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

shanks;

od;

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;

 

table([1=(3,3),2=(3,3),3=(3,3),4=(3,3),5=(3,3),6=(3,3),7=(3,3),9=(3,3),9=(3,3)

 

Thanks in advance

how the function if work?...

November 01 2013 miname 15

Hi

 I have a problem why the result does not appear..my 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.

 Thanks

 

1 2 3 4 5 6 7 Page 1 of 8