Different Values, Constanst Results in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a Mathematica code snippet that produces the same output despite varying the "SNR" variable in a loop. Participants explore the reasons behind this behavior, focusing on logical structuring of parameters and function definitions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the formula yields the same result for different "SNRdB" values, suggesting a logical issue in how parameters are handled.
  • Another participant proposes that some parameters should remain fixed while others change, indicating a need for clearer parameter management.
  • A suggestion is made to define a function that encapsulates all parameters, which could simplify the evaluation process and improve debugging.
  • Further elaboration on the function definition is provided, demonstrating how to structure the code for better clarity and functionality.
  • Participants express agreement on the need for a cleaner approach to the for loop by utilizing a function to handle parameters more effectively.

Areas of Agreement / Disagreement

Participants generally agree on the need for improved logical structuring of parameters and the utility of defining a function. However, there is no consensus on the specific implementation details or the initial cause of the issue.

Contextual Notes

The discussion highlights potential limitations in the original code's logical structure and the handling of variable dependencies, but does not resolve these issues definitively.

EngWiPy
Messages
1,361
Reaction score
61
Hello,

I have the following piece of code in Mathematica:

Code:
Na =.;
Q =.;
A = 23;
Q = 15;
Na = 21;
MGF[s_, gC_] := 1/(1 - gC*s);
a[n_] := If[n == 0, 2, 1];
For[SNRdB = 0, SNRdB <= 40, SNRdB = SNRdB + 2, SNR = 10^(SNRdB/10);
 gC = 0.5*SNR;
 
 Print[SetPrecision[Pout = ((2^-Q*E^(A/2))/SNR*\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]Binomial[Q, q]*\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 0\), \(Na + q\)]
FractionBox[
SuperscriptBox[\((\(-1\))\), \(n\)], \(a[n]\)] Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]\), gC]\), 
FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]]]\)\)) + (E^-A/(
       1 - E^-A) + (E^(A/2)*2^-Q)/SNR \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]
\*SuperscriptBox[\((\(-1\))\), \(Na + q + 1\)]*Binomial[Q, q]*Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I \((Na + q + 1)\))\)\), \(2*SNR\)]\), 
             gC]\), 
FractionBox[\(A + 2*Pi*I \((Na + q + 1)\)\), \(2*SNR\)]]]\)), 10]]]

Despite the variable "SNR" changed in each iteration of the for loop, the printed result is the same in all iterations. Why is this?

Thanks in advance
 
Physics news on Phys.org
That is correct. The formula you posted does in fact give the same value for different values of SNRdB.
 
DaleSpam said:
That is correct. The formula you posted does in fact give the same value for different values of SNRdB.

Thank you DaleSpam, your answer made me double check and think about the problem, and the problem was a logical one. Some parameters must be fixed and others must be changed, while what I did was that I changed all variables altogether.

Best regards
 
Ahh, ok. One thing you might think of doing is setting up a function that takes all of the parameters. That makes evaluating it at different values much easier.
 
DaleSpam said:
Ahh, ok. One thing you might think of doing is setting up a function that takes all of the parameters. That makes evaluating it at different values much easier.

Execuse me, I didn't get you. Can you elaborate please.
 
For example:

Code:
f[n_, Q_, q_, A_, gC_, Na_, SNR_] := 
(-1)^n/a[n] ((2^-Q*E^(A/2))/SNR*\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]\(Binomial[Q, q]*\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 0\), \(Na + q\)]
FractionBox[
SuperscriptBox[\((\(-1\))\), \(n\)], \(a[n]\)] Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]\), gC]\), 
FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]]]\)\)\)) + (E^-A/( 
      1 - E^-A) + (E^(A/2)*2^-Q)/SNR \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]\(
\*SuperscriptBox[\((\(-1\))\), \(Na + q + 1\)]*Binomial[Q, q]*Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I \((Na + q + 1)\))\)\), \(2*SNR\)]\), 
          gC]\), 
FractionBox[\(A + 2*Pi*I \((Na + q + 1)\)\), \(2*SNR\)]]]\)\))

Then your for loop is cleaner and easier to debug since you just call f in each iteration with different values for the arguments.
 
DaleSpam said:
For example:

Code:
f[n_, Q_, q_, A_, gC_, Na_, SNR_] := 
(-1)^n/a[n] ((2^-Q*E^(A/2))/SNR*\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]\(Binomial[Q, q]*\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 0\), \(Na + q\)]
FractionBox[
SuperscriptBox[\((\(-1\))\), \(n\)], \(a[n]\)] Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]\), gC]\), 
FractionBox[\(A + \((2*Pi*I*n)\)\), \(2*SNR\)]]]\)\)\)) + (E^-A/( 
      1 - E^-A) + (E^(A/2)*2^-Q)/SNR \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(q = 0\), \(Q\)]\(
\*SuperscriptBox[\((\(-1\))\), \(Na + q + 1\)]*Binomial[Q, q]*Re[
\*FractionBox[\(MGF[\(-
\*FractionBox[\(A + \((2*Pi*I \((Na + q + 1)\))\)\), \(2*SNR\)]\), 
          gC]\), 
FractionBox[\(A + 2*Pi*I \((Na + q + 1)\)\), \(2*SNR\)]]]\)\))

Then your for loop is cleaner and easier to debug since you just call f in each iteration with different values for the arguments.

Yes, you are right. I will try do this. Thanks
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K