Items tagged with loop loop Tagged Items Feed

I need to write a procedure that does the following :

Write a procedure quadsum whose input is an integer n and whose output is a list of pairs of solutions [x,y] to the above formula.

Your procedure should implement the following algorithm.

1 Initialization
Set
"mylist = []."

Start at
x = 0
and
y = 0.

2 Phase A
Increment both
x
and
y
until
"x^2+y^2 >=n."

Phase B
Repeat the following until
x^2>n

If you are above the circle
x^2 + y^2 = n
then go down in unit steps until you are on or below the circle.

If you are on the circle, add the point to the list
"mylist. "

If you are on or below the circle
x^2 + y^2 = n
then go one step to the right. My procedure is as follows: but it runs into an infinite loop(most probably because of the while loop defined inside the while loop). What am I doing incorrectly?

I have atta

 

 

****** My question *****

for k from 0 to n do    # n is any integer.

func := f(x):             # func is any funciton of x.

D := diff(func, x$k);   # The maple don't allow to uses k but I want to diff k-th order in each k-loop.

end do;                    # How to diff func for k-th times in each k-loop.

Hi all,

I want to design a procedure which can generate multiple loop automatically. While I  assign a value to it, it will create a multiple loop with same number of layers.    

For example,

P(1):

for i from 1 to n do
...
end do:

 

p(2):

for i from 1 to n do

for j from 1 to n do
...

end do: end do:

 

p(3)...

p(4)...

...

 

 

 

I'd appreciate any help on this topic. Thank a lot.

 

Hi,

So I have a simple while loop and keep receiving this error and have no idea how to fix it. I don't get why it works for the first few, and then the error starts. Here is my code and output:

Input:

restart;
n:=0:
x:=Pi/3;
m:=0:
total:=0:
while abs(1/2-total) > 10^(-8) do
k:=((-1)^(n/(2))*x^(n))/(n!):
total:= total +k;
evalf(total, 11);
print(%);
n:=+2;
end do:

OUTPUT:
1
- Pi
3
1.
0.45168864437
Error, cannot determine if this expression is true or false: 0 < -50000001/100000000+(1/18)*Pi^2

 

Why does it think i'm asking if something is greater than 0? Any help??

I paste below a simple code illustrating what I want to create: two lists from a set with pairs. I wonder it is a very simple task but I my lists aren't create in the end. 

 

 

> restart;
>
> lista:=[[1,10],[2,20],[3,30]]:
> x:=[]:
> y:=[]:
>
> for i from 1 to nops(lista) do
>     for j from 1 to 2 do
>
>         if j = 1 then
>             x[i,j]:=op(1,op(i,lista));
>        fi;
>        if j = 2 then
>             y[i,j]:=op(2,op(i,lista));
>       fi;
>    od;
> od;
>
> x;
> y;

 

 

In resume then after the for loop is terminated I want to be left with two lists:

x:= [1,2,3] and y:=[10,20,30]. I'm sure it has a quick fix but I'm stuck and would appreciate any help/advice.  

Thanks in advance!

I've got the following lists :

list1:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,

1240, 1496, 1785, 2109, 2470, 2870]
list2:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,

1240, 1496, 1785, 2109, 2470, 2870]

each generated by a procedure I defined. I need to verify that they are equal, which is the case. However, when I tried to use the evalb function as well as a flag that I was updating during a loop, in both cases, I got 'false' as  the answer along with the error message: 

"error, final value in a for loop must be numeric or a character"

What I am doing wrong?

Hello friends,


I try to write a loop in Maple.


The equation is f (x) = x + 3


x is a variable and should be from 50 to 70 with dx = 5 can be used in the function, so that f (x) is to be determined.

How can I write such a loop?

I am new to Maplesoft environment. I was trying to create a nested loop as given below:

I came across this error:

                               j:=1
Error, increment of for loop must be numeric.

Could anyone kindly help?

 

Thanks

I am having issues when defining functions in a loop. First, I define the first two functions as follows (here, r(x) is a function already assigned).

 

f_0 := x -> r(x):

f_1 := x -> r(x)*f_0(r(x)):

 

Then, I define successive functions in a for loop as follows.

 

for i from 2 to 10 do

   f_i := x -> r(x)*f_[i - 1](r(x));

end do

 

The loop defines the function f_2 but compiles erroneously for f_3 which, and I do admit, relies on f_2. Does someone have an idea of how to fix this issue? Any help will be greatly appreciated. Thanks.

Hi all,

I'm currently writing a code to solve the equations for a gravitational collapse of a star numerically. I'm using two dimensional Arrays for most of the physical quantities in my finite difference equations, with one index for the spatial dimension (I'm only considering spherically symmetric collapses) and one time index. I wrote two nested loops, where the inner one solves the equations from the centre of the star to the surface (with the exception of one quantitiy, where I have to integrate the other way around) and the outer loop advances the Arrays in time. My problem is, that Maple doesn't seems to evaluate the two dimensional Arrays. Instead for inserting the appropriate value for e.g. A[3,2], Maple just keeps on using A[3,2] which messes up some formulas, since I have to take roots of the numbers. At least that is what seems to be going on when I'm trying to debug the relevant part of the code and I check for the values, with whicih Maple calculates. I'm also using a one dimensional Array to store the timesteps, I'm using in the code and Maple doesn't seem to have problems evaluating them. Is anybody familiar with this problem? Does Maple have trouble with multidimensional Arrays in loops or have I just messed up my code? I have uploaded my code (I hope it worked). I apologize for my code being messy, but it's the first time I'm working with Maple and using numerical methods. The part which troubles me is labeled "# actual code".

Thank you all.

Oppenheimer-Snyder_Maple_3.mw

Hi there,

I would like to have the Jacobian matrix of an ODE system evaluated, and their eigenvalues computed, at the steady states of the system.

I know how to get the Jacobian matrix evaluated and the eigenvalues computed on an individual basis, setting manually each steady state as the argument of the matrix.

However, I would like to have it in a loop, so that the loop manages all steady states, that is:

steadyStates:= solve(mySystem); # would yield a set of pairs/lists

for each steadyState

m:=Jacobian(steadyStateN); # evaluate the Jacobian matrix

ev:= eigenvals(m); # compute the eigenvalues and save them to another variable/array and print them

end for:

First, I am not to find a way to loop over my steadyStates.

Attached is an example where the Jacobian matrix and eigenvalues are computed individually, where the steady states have been hard-coded once they have been computed: MaplePrimes_Predator_prey_model_Jacobian.mw

 

Any ideas on how to do this?

Thanks,

jon

 

 

Dear all;

I need your help to display the value of phi[jj] in the following loop. 

phi[1]:=(x,y)->exp(x-y);

for jj from 1 by 1 to 2 do
phi[jj+1]:=(x,y)->int(phi[1](x,s)*phi[jj](s,y),s=y..x);
end do;

 

but the index jj in the integral does not change. I have any idea what is the problem. 

Thank you very much for your help. 

 

 

Hi 

I'm having a problem with the statements inside a for-loop somehow being read in a different way than outside the loop. 

I've defined some functions earlier, and then I need to perform an integration using these functions, while I change one variable a little for each loop of the for-loop. 

The problem is that IN the for-loop, I get the same value from my integration for all loops. But when I execute the exsact same code OUTSIDE of the loop, I get the correct values, which are changing whenever i change the one variable. 

Here is the loop:

for i from 0 to 42 do

rotorshift := evalf[6](2*((1/180)* *Pi*((1/2)*Ø[gap]))/N[m]-1/2*(tau[p]-tau[s]));

PMmmf_func := x-> proc (x) if type(x-rotorshift, nonnegative) then if type(trunc((x-rotorshift)/tau[p]), odd) = true then -H[c]*l[m] else H[c]*l[m] end if else if type(trunc((x-rotorshift)/tau[p]), odd) = true then H[c]*l[m] else -H[c]*l[m] end if end if end proc

B[g] := proc (x) -> 1000*mu[0]*(PMmmf_func(x)+MMF_func(x))/d[eff, stator](x) end proc;

Flux(i+1) := (int(B[g](x), 0 .. tau[s])+int(B[g](x), 3*tau[s] .. 4*tau[s])+int(B[g](x), 6*tau[s] .. 7*tau[s])+int(B[g](x), 9*tau[s] .. 10*tau[s]))*10^(-3)*L[ro];

end do;

And for all the values in the "Flux" vector, I get the same value. But when I remove the loop, and change the value of manually, I get the correct (changing) values of flux!! 

Any ideas why this may be? This is really dricing me nuts. I've spent the beter part of the day on this, and I just can't seem to find a workaround, much less a reason for this behaviour.

many thanks!

I have about a year of experience in Maple, but as we know, we often come accross very simple problems that are difficult to find solutions to. Part of the problem is that I have not used conditional tests within for loops before.

I have a very large matrix, but until I solidify the algorithm, I'm practicing on a small matrix.

I created a 3 by 3 fibonacci matrix A:=<21,5,1|13,3,1|6,2,0>

I am trying to change the 1s to 0s without specific index reference. Here is my psuedocode that I hope to work on after some tips:

  • for i from 1 to 9
    if A[k] = 1 then
    A[k]=0
    end if
    end do

 

I get an error message saying "unterminated for loop," which is confusing me.

Hallo. I'd like to use two commands in loop like:

for i from 1 by 1 to k  do r:=Search(polyQ[i],[Q]) and T[r]:=T[r]-polyeq[i] end do

But Maple: "Error, unterminated loop". Hmm...

1 2 3 4 5 6 7 Last Page 1 of 10