Group Abstract Group Abstract

Message Boards Message Boards

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

No output from Print inside For

Posted 2 years ago

enter image description here

Here is the code:

For[i = 1, i = 7, i++
    For[lambda = 0.01, lambda = 0.2, lambda = lambda + 0.001
        Print[
        Log10[R[10^-3, 10^-3, 10^(-i/10), 10^(-i/10), lambda, 0.5, 0]]]
    ]
  ];

Theses are othe fiunctions

ql[Y0A_, Y0B_, etaA_, etaB_, 
  lambda_] := (1 - ((1 - Y0A)/(1 + etaA*lambda)^2) - ((1 - 
       Y0B)/(1 + etaB*lambda)^2) + ((1 - 
       Y0A)*(1 - 
        Y0B)/(1 + etaA*lambda + etaB*lambda - etaA*etaB*lambda)^2))

el[Y0A_, Y0B_, etaA_, etaB_, lambda_, e0_, 
  ed_] := ((e0*
      ql[Y0A, Y0B, etaA, etaB, 
       lambda]) - ((2*(e0 - ed)*etaA*etaB*
        lambda (1 + lambda))/((1 + etaA*lambda)*(1 + etaB*lambda)*(1 +
           etaA*lambda + etaB*lambda - etaA*etaB*lambda))))*(1/
    ql[Y0A, Y0B, etaA, etaB, lambda])

H[x_] := -x*Log2[x] - (1 - x)*Log2[1 - x]
R[Y0A_, Y0B_, etaA_, etaB_, lambda_, e0_, ed_] := 
 0.5*ql[Y0A, Y0B, etaA, etaB, 
   lambda]*(1 - 2 H[el[Y0A, Y0B, etaA, etaB, lambda, e0, ed]])
POSTED BY: Indranil Maiti
2 Replies
Posted 2 years ago

There are syntax errors in the For loops, missing commas. The test needs to be a boolean expression, not a Set.

For[i = 1, i != 7, i++, 
  For[lambda = 0.01, lambda != 0.2, lambda = lambda + 0.001 , 
   Print[Log10[R[10^-3, 10^-3, 10^(-i/10), 10^(-i/10), lambda, 0.5, 0]]]]];

There are much better alternatives to For in the Wolfram Language.

Table[{i, lambda, Log10[R[10^-3, 10^-3, 10^(-i/10), 10^(-i/10), lambda, 0.5, 0]]},
  {i, 1, 7}, {lambda, 0.01, 0.2, 0.001}] // Flatten[#, 1] &

This evaluates to a list of {i, lambda, Log10[R]} for each value of i and lambda, which can be used for subsequent computation, export to a CSV file, ...

POSTED BY: Rohit Namjoshi
Posted 2 years ago

You have several problems. The first is basic syntax. You're missing a comma between i++ and For. New lines do not work as expression delimiters. Same problem between 0.001 and Print.

Another problem is understanding the difference between =, which is used for assignment, and ==, which is used for testing equality. So, in this snippet,

For[i = 1, i = 7, i++

you actually just reassigned the value of i to 7.

Yet another problem is understanding how For works. The body of the loop is evaluated each time the test evaluates to True. So, even if we changed the above snippet to this,

For[i = 1, i == 7, i++,

the body would still never execute, because checking i==7 immediately after setting i=1 will result in False. You probably want something like this:

For[i = 1, i <= 7, i++,

You have the same issues with the inner loop.

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