I'm trying to find the zeros of f(z)=c(z)-2500=0 using the secant method. I get the correct values (8.9, -2.6, 7.7, 12.3) but only if I put the starting values close. For instance this version of the program gives the answer 12.3:
w1 = 11; % x0
w2 = 12; % x1
[X,Z] = ode45(@(t,Z) soundsys(t,Z,w1),x,[2000 tand(w1)]);
f1 = Z(end,[1,end])-2500; % f_1 = f(x_1)
dw = 0.1;
iter = 0;
while abs(dw) > 1e-8 && iter < 10
[X,Z] = ode45(@(t,Z) soundsys(t,Z,w2),x,[2000 tand(w2)]);
f2 = Z(end,[1,end])-2500 % f_2 = f(x_2)
dw = - f2*(w2 - w1)/(f2 - f1);
w1 = w2;
f1 = f2;
w2 = w2 + dw;
iter = iter + 1;
end
disp(w1);
I must modify the starting values to be close to one of the solutions for it to work. Is there a more efficient method to use the secant method when the solution can have multiple roots?
Because the secant works by using the "derivative" of the function, so it will only seek toward the nearest solution and once it reaches this solution it cannot move any further.