Error in Code: Investigating Possible Solutions

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

Discussion Overview

The discussion revolves around troubleshooting a code snippet involving the use of the FindRoot function in a programming context. Participants explore potential errors in the code, particularly regarding the handling of output from FindRoot and alternative methods for finding solutions to the equation presented. The scope includes technical explanations and problem-solving approaches.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant identifies that FindRoot returns a rule rather than a number, suggesting a modification to the code to extract the number correctly.
  • Another participant expresses uncertainty about the choice of the initial guess (0.5) for FindRoot, noting that it works but questioning its correctness.
  • A participant explains the use of the ReplaceAll operator (/.), clarifying how it can be used to substitute the rule returned by FindRoot into a variable.
  • Concerns are raised about the behavior of the function near x=0, which may complicate the use of FindRoot, and a suggestion is made to consider alternative methods like bisection.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the output from FindRoot and the need to modify the code accordingly. However, there is no consensus on the best approach to find a solution to the equation, as some express uncertainty about the initial guess and others suggest alternative methods.

Contextual Notes

The discussion highlights limitations regarding the choice of initial values for FindRoot and the behavior of the function near certain points, which may affect the reliability of the results.

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 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K