View Full Version : Mathematica Error
S_David
Feb17-11, 09:55 PM
Hi,
I have the following code:
For[SNRdB = 0, SNRdB <= 30, SNRdB = SNRdB + 1,
SNR = 10^(SNRdB/10);
y = FindRoot[
ExpIntegralE[0, x/SNR] - ExpIntegralE[1, x/SNR] == SNR, {x, 0.5}];
R = N[1/SNR*NIntegrate[Log[2, q/y]*Exp[-q/SNR], {q, y, Infinity}]]]
what is the error in this code?
Thanks in advance
Bill Simpson
Feb17-11, 10:43 PM
FindRoot does not return a number, it returns a rule, like x->0.393774.
If you change that to
y = x /. FindRoot[ExpIntegralE[0, x/SNR] - ExpIntegralE[1, x/SNR] == SNR, {x, 0.5}];
I believe you will end up with the number in y that you intend.
S_David
Feb18-11, 10:30 AM
FindRoot does not return a number, it returns a rule, like x->0.393774.
If you change that to
y = x /. FindRoot[ExpIntegralE[0, x/SNR] - ExpIntegralE[1, x/SNR] == SNR, {x, 0.5}];
I believe you will end up with the number in y that you intend.
I discovered my error after I posted this post, and tried to come up with a new approach, and I couldn't tfind an effective one. Your approach is working perfectly, and it is effective as well. But can you explain to me what does this x/. mean?
Thanks
S_David
Feb18-11, 12:47 PM
Another thing, is there any other way to find a solution for my equation? I mean FindRoot[] requires you to indicate the root around something, and I don't know where the root is. I mean it is somewhere between 0 and 1, but when I put some values I get errors, until I put 0.5, and then it is working. I don't know if my choice of 0.5 is correct.
Thanks
Bill Simpson
Feb18-11, 03:01 PM
If you look at the documentation for FindRoot you see it returns "rules", not numbers. These rules can be used in other places.
Example
In[19]:=FindRoot[3x+4==0,{x,0}]
Out[19]={x -> -1.33333}
That output is a rule, actually a list of a single rule.
/. which is also called ReplaceAll will search through an expression and replace things based on a rule it is given. So
In[20]:=x/.{x -> -1.33333}
Out[20]= -1.33333
gives you a "bare number" that you can use in some calculation.
So what you originally had in your very first question was y=FindRoot and then you used y thinking it was a number. When you replaced that with y= x/.FindRoot the Find returned the rule, the /. substituted it into x and the = assigned that to y.
Is all that clear? Try really simple little examples and compare these with the help until you are sure you understand this. That will be essential in the future.
Next, if I Plot your ExpI[]-Expi[]-SNR I see it goes off to infinity near x==0. I'm guessing that is part of the reason you are having difficulties with FindRoot. I'm not sure what advice to give you on this. Perhaps someone else will look at what you have and suggest something. I've written my own bisection method in the past, but that would only help if one end doesn't blow up and it doesn't blow up anywhere in the middle.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.