Implementing Bisection Method in Matlab: Troubleshooting Error Message

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an error encountered while implementing the bisection method in Matlab. Participants explore issues related to function definitions and script organization, as well as clarifying output expectations from the algorithm.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant reports an error message indicating an undefined function 'f' for input arguments of type 'double', suggesting a problem with how the function is defined or accessed in the script.
  • Another participant explains that local functions cannot be defined in a Matlab script file and suggests storing the function in a separate file or converting the script into a function file.
  • It is proposed that an anonymous function could be used as an alternative to defining 'f' in a separate file, with a specific syntax provided for implementation.
  • A participant expresses gratitude for the solution and seeks clarification on whether to display 'c' or 'f(c)' to identify the root, indicating uncertainty about the output of the bisection method.
  • Another participant confirms that 'c' should be displayed and suggests modifications to the algorithm's parameters and function for further exploration of convergence.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper function definition in Matlab and the output expectations from the bisection method. However, there are multiple approaches suggested for defining the function, indicating some disagreement on the best method to implement.

Contextual Notes

Limitations include the assumption that participants are familiar with Matlab's function handling and the specific requirements for script versus function files. The discussion does not resolve the best practice for function definition in all contexts.

Who May Find This Useful

Individuals learning Matlab, particularly those interested in numerical methods like the bisection method, may find this discussion beneficial.

renolovexoxo
Messages
23
Reaction score
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
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.
 
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?
 
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K