Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.1K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why do I get a strange error in a For loop?

Posted 11 years ago

Can some one please explain why the first version of this program gets the error: Set::write: Tag Times in mst Null is Protected. >> Twice, but the second version works fine?

f[x_] = x^2;
s = x /. Solve[x^2 == x, x]
For[i = 1, i <= Length[s], i++,
 lst = NestList[f, s[[i]] - 0.05, 10];
 Print[lst] 
 mst = NestList[f, s[[i]] + 0.05, 10];
 Print[mst]
 ]

f[x_] = x^2;
s = x /. Solve[x^2 == x, x]
For[i = 1, i <= Length[s], i++,
 lst = NestList[f, s[[i]] - 0.05, 10];
 mst = NestList[f, s[[i]] + 0.05, 10];
 Print[lst]
  Print[mst]
 ]
POSTED BY: Emmett Hayes
6 Replies

Thank you

POSTED BY: Emmett Hayes

Easy! put a semicolon after each complete statement. So instead of

Print[lst] mst = NestList[f, s[[i]] + 0.05, 10]; 

you will have

Print[lst]; mst = NestList[f, s[[i]] + 0.05, 10]; 
POSTED BY: David Reiss
Posted 10 years ago
POSTED BY: Amir Arshia

I would have to see your code in a notebook to be sure, but try putting a; after your For statement and see if that fixes things.

POSTED BY: David Reiss

Thank you; that explains a number of strange errors I've gotten. Now my question is how do I tell the processor that they are two statements?

POSTED BY: Emmett Hayes

Because you are trying to evaluate the expression

Print[lst] mst = NestList[f, s[[i]] + 0.05, 10]; 

The left hand side is not a parameter, but rather it is the product of Print[lst] and mst.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard