| New Reply |
Bisection method in Matlab |
Share Thread | Thread Tools |
| Oct30-12, 03:13 PM | #1 |
|
|
Bisection method in Matlab
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'. |
| Oct30-12, 03:42 PM | #2 |
|
Mentor
|
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. |
| Oct30-12, 04:11 PM | #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? |
| Oct30-12, 04:24 PM | #4 |
|
Mentor
|
Bisection method in Matlab
c, of course. f(c) will be close to zero -- in this case. Try this, however:
|
| New Reply |
| Thread Tools | |
Similar Threads for: Bisection method in Matlab
|
||||
| Thread | Forum | Replies | ||
| Bisection method in c++ | Engineering, Comp Sci, & Technology Homework | 2 | ||
| Bisection Method in C | Engineering, Comp Sci, & Technology Homework | 7 | ||
| Differential equations, euler's method and bisection method | Calculus & Beyond Homework | 3 | ||
| Bisection Method | Programming & Comp Sci | 2 | ||
| Bisection method | General Math | 1 | ||