Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Different Values, Constanst Results in Mathematica

  1. Aug 9, 2009 #1
    Hello,

    I have the following piece of code in Mathematica:

    Code (Text):
    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
     
  2. jcsd
  3. Aug 9, 2009 #2

    Dale

    Staff: Mentor

    That is correct. The formula you posted does in fact give the same value for different values of SNRdB.
     
  4. Aug 10, 2009 #3
    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
     
  5. Aug 10, 2009 #4

    Dale

    Staff: Mentor

    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.
     
  6. Aug 10, 2009 #5
    Execuse me, I didn't get you. Can you elaborate please.
     
  7. Aug 11, 2009 #6

    Dale

    Staff: Mentor

    For example:

    Code (Text):
    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.
     
  8. Aug 11, 2009 #7
    Yes, you are right. I will try do this. Thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Different Values, Constanst Results in Mathematica
Loading...