secant method help matlab

2 次查看(过去 30 天)
dulanga
dulanga 2014-9-21
评论: Alberto 2014-9-22
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 个评论
Alberto
Alberto 2014-9-22
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by