Search code examples
matlabnonlinear-functions

Nonlinear root finding in MATLAB


function y = f(z)

b=10;


y=(cos(z))+(b*(sin(z)/z))-cos(d);

This is my function file.

fun = @f; % function

x0 = 1; % initial point  

z = fzero(fun,x0);

While running above code I am getting only a single value. But What I need is a set of values satisfying

(cos(z))+(b*(sin(z)/z))-cos(d)=0

Where: d=-5:1:5

Please kindly help


Solution

  • You can use different initial points. This process can be automated:

    solutions= zeros(1,100);
    for k= 1:numel(solutions)
        x0= (rand-0.5)*20; % generate the random initial point in (-10,10)
        z = fzero(@f,x0);
        solutions(k)= z;
    end
    disp(unique(solutions))
    

    The result can include identical solutions due to the solver accuracy.

    Another way to find the initial points is described here