Error in Code: Investigating Possible Solutions

  • Mathematica
  • Thread starter EngWiPy
  • Start date
  • Tags
    Code Error
In summary: The "x == 0" part is a rule in FindRoot, and it will return a number when used in an equation like y=x/. It will return a number between 0 and 1.
  • #1
EngWiPy
1,368
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
  • #2
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.
 
  • #3
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
 
  • #4
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
 
  • #5
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.
 

Related to Error in Code: Investigating Possible Solutions

1. What are the common causes of coding errors?

Some common causes of coding errors include typos, incorrect syntax, logic errors, and incorrect use of functions or variables. These can often be avoided by double-checking code and using debugging tools.

2. How can I troubleshoot coding errors?

To troubleshoot coding errors, start by reading any error messages and checking for typos or incorrect syntax. Use debugging tools to step through the code and identify any logic errors. It can also be helpful to consult with other developers or refer to documentation.

3. What is the process for fixing a coding error?

The process for fixing a coding error typically involves identifying the source of the error, making necessary changes to the code, and testing to ensure the error has been resolved. It may also involve consulting with other developers, using debugging tools, and making multiple attempts at fixing the error.

4. How can I prevent coding errors in the future?

To prevent coding errors, it is important to follow best practices and use proper coding conventions. This includes writing clean, organized code, commenting appropriately, and using debugging tools. It can also be helpful to regularly review and refactor code to catch any potential errors.

5. What should I do if I encounter a particularly difficult coding error?

If you encounter a difficult coding error, don't be afraid to reach out for help. Consult with other developers, refer to documentation, or consider taking a break and returning to the problem with a fresh perspective. Sometimes, stepping away and coming back to the problem can help you find a solution more easily.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
34
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
347
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
250
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
372
Back
Top