Implementing Bisection Method in Matlab: Troubleshooting Error Message

In summary, the conversation discusses an error message that occurs when attempting to use a function in a Matlab script file. The solution is to either store the function in a separate Matlab file or use an anonymous function. The final question involves finding the root of a function using the bisection method in Matlab. It is recommended to print the value of c or NumIt to ensure the algorithm has converged on a zero.
  • #1
renolovexoxo
25
0
Here is the code I have, but I keep getting the error message: Undefined function 'f' for input arguments of type 'double'.

I don't know what I have that is causing this. Does anybody see what's wrong with my code?

MaxIt = 1000;
epsilon = 10^-5;
a=1;
b=2;
c = (b+a)/2;
NumIt = 0;
while NumIt< MaxIt && abs(f(c))>epsilon
if f(a)*f(c) < 0
b = c;
else
a = c;
end
NumIt = NumIt + 1;
c = (b+a)/2;
end
end
function y = f(x)
y = exp(x)-2^-x+2*cos(x)-6;
end
Undefined function 'f' for input arguments of type 'double'.
 
Physics news on Phys.org
  • #2
As written, you would need to store your function f(x) in a separate Matlab file named f.m. You can't have local functions in a Matlab script file. You can have local functions in a Matlab function file, so one way to avoid having a separate file for that tiny function is to turn your script into a Matlab function.

This is such a short and simple function that may not even want to do that. Another option is to use an anonymous function instead. Define f(x) via f = @(x) exp(x)-2^-x+2*cos(x)-6; You'll need to put this near the top of your script rather than at the bottom.
 
  • #3
Okay, that worked great! Thank you.

Last question, just because I have a hard time in Matlab.
When I'm looking at the solution, do I want to display c or f(c) to get the root? It would be c, correct?
 
  • #4
c, of course. f(c) will be close to zero -- in this case. Try this, however:
  • Change MaxIt to 1024 or so (adjust downward if you get divide by zero).
  • Change the initial value of a from +1 to -1.
  • Change your function to f = @(x) 1/x;
It's worthwhile to print either f(c) or NumIt (or both) to see if the algorithm truly did converge on a zero.
 
Last edited:
  • #5


It seems like the issue is that the function 'f' is not defined properly. In order for the code to run, the function 'f' needs to be defined with an input argument of type 'double'. In this case, the input argument should be 'x'. Make sure to define the function properly before running the code again. Additionally, check for any typos or syntax errors in the code. If the issue persists, try debugging by printing out the values of 'a', 'b', and 'c' to see if they are being updated correctly in the while loop.
 

1. How do I know if I am implementing the bisection method correctly in Matlab?

The best way to ensure that you are implementing the bisection method correctly in Matlab is to check your code against a reliable source or tutorial. You can also test your code with simple examples to see if you are getting the expected results.

2. Why am I getting an error message when implementing the bisection method in Matlab?

There can be several reasons for getting an error message when implementing the bisection method in Matlab. Some common reasons include syntax errors, incorrect variable names, or dividing by zero. Double-check your code and make sure it follows the correct syntax and logic.

3. How can I troubleshoot my code when I encounter an error message?

The first step to troubleshooting your code when you encounter an error message is to carefully read and understand the error message. This will give you a clue about what might be causing the error. Next, you can use Matlab's debugging tools, such as breakpoints and the step function, to track the execution of your code and identify the source of the error.

4. What is the role of tolerance in implementing the bisection method in Matlab?

Tolerance is a measure of how close the solution obtained by the bisection method is to the actual root of the function. It is used to control the precision of the solution and can be adjusted depending on the desired accuracy. A smaller tolerance value will result in a more accurate solution, but it may also increase the number of iterations required to reach the solution.

5. How can I improve the performance of my bisection method implementation in Matlab?

There are a few ways to improve the performance of your bisection method implementation in Matlab. One way is to use vectorized operations instead of loops, as they are generally faster. You can also try to optimize your code by minimizing the number of function evaluations or using more efficient algorithms, such as the secant method or the false position method. Additionally, using preallocation and avoiding unnecessary function calls can also improve the performance of your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
567
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Back
Top