help me 'Matlab' for bisection method.

887 Views Asked by At

Pb1) use a matlab.

Graph the function $$f(x) = 32x^6 −48x^4 +18x^2 −1$$ on the interval $[−1, 1]$ and find all roots of $f (x) = 0$ accurate to within $\varepsilon = 1e−10$ by using the bisection method.

The true solutions are $\cos[(2n−1){\over}]$ for $n=1,2,...,6$.

Report the number of iterations and errors for each roots in tables.

My script is

f=@(x) 32*x.^6-48*x.^4+18*x.^2-1;

a=-1; b=-0.8; c=(a+b)/2; eps=1e-10; it=0;

g=@(n) cos((2*n-1)*pi./12);

n=1;

    while abs(a-c)>eps   

        if f(a)*f(c)>0

            a=c;

        else

            b=c;

        end

   c=(a+b)/2; it=it+1;     

    end

   fprintf('Iteration is %.d. And solution is %.10f \n',it,c); 


while (abs(c-g(n))>eps)

    n=n+1;

end

    fprintf('And exist such that True solution is %.10f\n',g(n));

and result

Iteration is 30. And solution is -0.9659258262

And exist such that True solution is -0.9659258263

since the graph of $f(x)$ is enter image description here

so the script divide the interval of $[-1,1]$ for using bisection method.

but for solving a Pb1, this script is rewritten for each sub-interval.

can you help me?

I hope to NO rewritten for each sub-interval.

1

There are 1 best solutions below

0
On BEST ANSWER

One option is to put your bisection code in a for loop that loops over the subintervals.

point = [-1;-0.9;-0.5;0;0.5;0.9;1];

for interval = 1:6
    start = point(interval);
    finish = point(interval+1);

    % regular bisection method
    ....
end