Mathematica Error in Code: Investigating Possible Solutions

  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Code Error
AI Thread Summary
The discussion focuses on resolving an error in a code snippet that uses the FindRoot function. The main issue identified is that FindRoot returns a rule rather than a numerical value, which can be corrected by using the replacement operator "/." to extract the number from the rule. Participants also discuss the importance of selecting an appropriate starting point for FindRoot, noting that the choice of 0.5 was effective in this case, despite uncertainties about its correctness. Additionally, there are suggestions to explore alternative methods for finding roots, such as implementing a bisection method if the function behaves well. Understanding the output of FindRoot and how to manipulate it is emphasized as crucial for future coding tasks.
EngWiPy
Messages
1,361
Reaction score
61
Hi,

I have the following code:
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
 
Physics news on Phys.org
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.
 
Bill Simpson said:
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
 
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
 
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.
 

Similar threads

Replies
6
Views
2K
Replies
4
Views
3K
Replies
10
Views
3K
Replies
34
Views
4K
Replies
1
Views
2K
Replies
13
Views
2K
Replies
1
Views
2K
Replies
11
Views
5K
Replies
4
Views
1K
Replies
1
Views
2K
Back
Top