secant method help matlab

2 views (last 30 days)
dulanga
dulanga on 21 Sep 2014
Commented: Alberto on 22 Sep 2014
hi i have been asked to make a function file for secant menthod
fxi=f(xi);
fxi_1=f(xi_1);
% Initialize iteration count iter = 1;
%iteration for the secant starts while abs(fxi)>precsion
xi=xi-(fxi/fxi_1);
xi_1=(xi-f(xi).(xi_1-xi))/(f(xi_1)-f(xi))
% Increment the iteration count by 1
iter = iter + 1;
end %the final xr value is the root root=xi; % The total iteration number iter = iter - 1; this what i have done
when i run the values its gives this error(Attempted to access f(2); index out of bounds because numel(f)=1.) can anyone help ?
  1 Comment
Alberto
Alberto on 22 Sep 2014
I think your code interpret f, not as a function, but as an array with values. So, when you are trying to evaluate f(x), where x=2, your code looks for the second values in an array f.
Advice: 1) save your bound points in variables a and b (for example). Also create a proper function f (inline function or m-file function), so you can evaluate f in a and b. You will need a while or for+if to make your iterations.

Sign in to comment.

Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!