help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fsolve shows repeated results


From: Steven Davis
Subject: Re: fsolve shows repeated results
Date: Fri, 16 Oct 2015 15:47:58 -0500

Fausto,

I'm just picking up Octave after having used Matlab years ago, so I'm not really an expert, you'll want to check this for yourself.

The lines:

xi = [0.1:0.5:5.0];
    
    for xguess = xi

Tell Octave to make an array xi that is [0.1,0.6,1.1,1.6,2.1,2.6,3.1,3.6,4.1,4.6] and then run a for-loop for each of those values.  Since you are giving it 10 different starting points, it is naturally going to return different values for a periodic function like x*tan(x).

Steven 

On Fri, Oct 16, 2015 at 3:16 PM, Doug Stewart <address@hidden> wrote:


On Fri, Oct 16, 2015 at 3:54 PM, Fausto Arinos de A. Barbuto <address@hidden> wrote:

Hello,

When run on Octave 4.0.0 on a W7 machine the following piece of
code:

%--------------------------------------------------------
function raizes3

    format long;
    options =  optimset('TolX',1.0e-15,'TolFun',1.0e-15);
    xi = [0.1:0.5:5.0];
   
    for xguess = xi
        [x,fval] = fsolve(@f,xguess,options)
        fprintf('\n');
    end
end

function y = f(x)
    y = x*tan(x) - 42.0;
end
%--------------------------------------------------------

produces the results shown below:

x =  1.53428203880513
fval =   2.04636307898909e-012

x =  1.53428203880513
fval =   2.04636307898909e-012

x =  1.53428203880513
fval =  -4.97379915032070e-014

x =  4.60322412114047
fval =  -7.38964445190504e-013

x =  4.60322412114047
fval =  -7.38964445190504e-013

x =  4.60322412114047
fval =  -3.55271367880050e-014

x =  4.60322412114047
fval =  -3.55271367880050e-014

x =  7.67327748954486
fval =   2.27373675443232e-013

x =  4.60322412114047
fval =  -3.55271367880050e-014

x =  4.60322412114047
fval =  -3.55271367880050e-014

As one can see the two existing roots of y = x*tan(x) - 42 in the interval
0.1 <= x <= 5.0 were found more than once: 1.534... was found thrice while
4.603... was found _six_ times -- as if fsolve were iterating back & forth
around a particular root so that it was found several times.

But the most curious part of the whole process comes right after fsolve()
finds the root x = 7.673... (at this point root x = 4.603... had already
been found four times): fsolve() identifies x = 4.603 as a root another two
extra times when I for one would have expected the root-finding process to
have stopped at x = 7.673...

I wonder why this happens. y = x*tan(x) - C certainly isn't the most well-
behaved of the functions as it shows sharp, up-and-down spikes too often.
But the strange behaviour described in the paragraph right above has no
clear explanation (to me at least).

What's your take on this?

Regards,

Fausto



_______________________________________________
Help-octave mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-octave



You ask it to find a root 10 different times and then it did that and gave you 10 answers.
Why are you surprised?

Doug
--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-octave



reply via email to

[Prev in Thread] Current Thread [Next in Thread]