plotting data from other function

61 views Asked by At

I'm trying to do some kind of bisection method program that show how i get to the final answer by plotting everything. Do you know why in my file i can't plot data from function in another m file? There is that error:

mainhazia 27 end Cannot find an exact (case-sensitive) match for 'Roots'

The closest match is: roots in C:\Program Files\MATLAB\R2012b\toolbox\matlab\polyfun\roots.m

Error in mainhazia (line 23) plot(Roots,f(Roots),'.');

My code :

Main :

f=@(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
ezplot(f);
hold on ;
plot(XR,f(XR),'r*'); 
plot(xL,f(xL),'r*');
for df=xL:0.15:XR
 if f(xL)*f(df)<= 0
     xR=df; 
     BisectionM(f,xR,xL,eps);
     plot(Roots,f(Roots),'.'); 
     xL=df; 
     xR=XR;
 end
end

BisectionM :

function Roots = BisectionM(f,xR,xL,eps)     
while abs(xR - xL) > eps
             xM = (xR + xL) / 2;
             if (f(xL))*(f(xM)) > 0
                 xL = xM;
                 plot(xL,f(xL),'*');
             else
                 xR = xM;
                 plot(xR,f(xR),'*');
             end
             Roots = xM;

        end
    end

Sorry about my English , its not my native language .

1

There are 1 answers

1
rozsasarpi On

Check whether this is what you are looking for (the result of BisectionM is not assigned to any variable):

in the main file change:

BisectionM(f,xR,xL,eps);

to:

Roots = BisectionM(f,xR,xL,eps);

It produces the following plot: enter image description here

f=@(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
ezplot(f);
hold on ;
plot(XR,f(XR),'r*');
plot(xL,f(xL),'r*');
for df=xL:0.15:XR
    if f(xL)*f(df)<= 0
        xR=df;
        Roots = BisectionM(f,xR,xL,eps);
        plot(Roots,f(Roots),'.');
        xL=df;
        xR=XR;
    end
end