1
$\begingroup$

enter image description here enter image description here

I have done following, but I don't know how to put that function into this:

function [x,y] = secant(fun,x0,x1,tol,max)
x(1) = x0;
x(2) = x1;
y(1) = feval(fun,x(1));
y(2) = feval(fun,x(2));
    for i = 3 : max
        x(i) = x(i-1) - y(i-1)/((y(i-1)-y(i-2))/(x(i-1)-x(i-2)));
        y(i) = feval(fun,x(i));
        if abs(x(i) - x(i-1)) < tol
            disp('Secant method converged!');
            break;
        end
        if i== max
            disp('Zero not found!');
        end
    end
n = length(x);
k = 1:n;
out = [k' x' y']; 

disp(out);
$\endgroup$
2
  • $\begingroup$ what is basic question?does not it work? $\endgroup$ Mar 30, 2014 at 18:48
  • $\begingroup$ Put what function into it? It looks like you have it most of it done and just need to test and debug it. You can always check if it's working by using a function for which you know the zeros, e.g., an arbitrary quadratic. Just choose x0 and x1 to straddle one of the zeros of the quadratic and you should get that abs(fun(y(end))) < tol. Something you want to look at is whether your termination condition is correct. Should it be x(i) - x(i-1) < tol like you have or should it be abs(y(i)) < tol? $\endgroup$ Apr 1, 2014 at 15:21

0

You must log in to answer this question.

Browse other questions tagged .