I'm writing a simple function to find the roots of a user defined equation. The function is as follows:
function [root] = NR(func, dfunc, x_0)
x_r = x_0;
while func(x_r) > 10^-6
x_r = x_0 - func(x_0)/dfunc(x_0);
x_0 = x_r;
end
root = x_r;
fprintf('The root in the given interval is %.4f\n', root)
I defined my function 'func' and its derivative 'dfunc' as follows
func=@(x) 2*x^2-3; dfunc=@(x) 4*x;
When attempting the use the function with the following input, it returns the following error messaage
NR(func,dfunc,-1) Undefined function 'NR' for input arguments of type 'function_handle'.
What am I doing wrong? Thanks in advance for any help.
make sure you matlab's working directory/path has the NR function
if that's not desired, you can do
addpath('path_where_NR_is');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.