2

I want to solve a system of differential equations and each time I want to change two parameters. To prevent wrong result I want to clear every assignment after solving the equations in each iteration. I have a Do-loop with two iterator. The problem is that if I use Clear["Global`*"] then the outer iterator will not be accessible to the code inside the loop.

Here is an example:

Do[{Print[a]; Clear["Global`*"];}, {a, 1, 2}, {b, 1, 2}]

Result:

1

a

2

a

As you can see the iterator a is erased for the inner iterator b. How can I resolve this issue?

I expect the outer iterator to be printed each time. There are 4 iteration in total, so I expect to get:

1
1
2
2

I want both iterators, a and b, in spite of using Clear command to be accessible to the code inside loop. I want to clear everything after each iteration except iterators a and b.

MOON
  • 3,864
  • 23
  • 49

1 Answers1

2

How about this?

Do[{Print[a];
  Map[Clear, DeleteCases[Names["Global`*"], "a" | "b"]]},
 {a, 1, 2}, {b, 1, 2}]
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108