Message Boards Message Boards

0
|
1875 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Loop function question

Posted 11 years ago
Hi All,

I'm new to Mathematica. Below is the code I have trouble with.
Do[Print[Subscript[C, k][x_, y_] =
   Subscript[q, 2 k - 1] +
    x*Subscript[q,
     2 k] - (Subscript[q, l] + y*Subscript[q, l + 1])], {k, 3, 5,
  1}, {l, 3}]

In this loop, I want k=3 map to l=1, k=4 map to l=2, and k=5 map to l=3. However in this loop function, it loops through l=1,2,3 when k=3 and so on. So is there an automatic way or code to stop the certain looping?

Thanks in advance and sorry for my English.
POSTED BY: Kimi
2 Replies
First run Quit to make sure that the Kernel is fresh and then run the following:
Do[
   With[{l = k - 2},
    Print[Subscript[C, k][x_, y_] == Subscript[q, 2 k - 1] +  x*Subscript[q, 2 k] - (Subscript[q, l] + y*Subscript[q, l + 1])]
   ],
{k, 3, 5, 1}]

First, I've used == instead of = in the code above. I'm not sure if you really wanted to use "=" or "==" in your code.

Secondly, I've used With to say that l should be 2 less than k in the formula. Is this what you meant?
POSTED BY: Sean Clarke
Posted 11 years ago
Thank you Sean. That was a typo. It should be := instead of ==.

I will have a try "With" now. 

Thanks again for your help.
POSTED BY: Kimi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract