0
|
5311 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

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

Posted 10 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] ] 
6 Replies
Sort By:
Posted 10 years ago
 Thank you
Posted 10 years ago
 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 9 years ago
 Dear David do you know what the problem is when i run the code below : For[k = 1, k < 11, k++, If[fitnessfnew[[i, k]] > fitnessfnew[[i - 1, k]], pbest1[[k]] = xx1[[k]]; pbest2[[k]] = xx2[[k]];pbest3[[k]] = xx3[[k]]; pbest4[[k]] = xx4[[k]];]vv1[[k]] = vv1[[k]] + c1 RandomReal[{0, 1}] (pbest1[[k]] - xx1[[k]]) + c2 RandomReal[{0, 1}] (gbest1 - xx1[[k]]);vv2[[k]] = vv2[[k]] + c1 RandomReal[{0, 1}] (pbest2[[k]] - xx2[[k]]) + c2 RandomReal[{0, 1}] (gbest2 - xx2[[k]]);vv3[[k]] = vv3[[k]] + c1 RandomReal[{0, 1}] (pbest3[[k]] - xx3[[k]]) + c2 RandomReal[{0, 1}] (gbest3 - xx3[[k]]);vv4[[k]] = vv4[[k]] + c1 RandomReal[{0, 1}] (pbest4[[k]] - xx4[[k]]) + c2 RandomReal[{0, 1}] (gbest4 - xx4[[k]]);xx1[[k]] = xx1[[k]] + vv1[[k]];xx2[[k]] = xx2[[k]] + vv2[[k]];xx3[[k]] = xx3[[k]] + vv3[[k]];xx4[[k]] = xx4[[k]] + vv4[[k]];]; it says : Set::write: Tag Times in -2.79834 Null is Protected. >> when i run it part by part with k=1,...,10 it works correctly !!! i really get confused...
Posted 9 years ago
 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 10 years ago
 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 10 years ago
 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.