help-octave
[Top][All Lists]
Advanced

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

RE: fsolve shows repeated results


From: John Guin
Subject: RE: fsolve shows repeated results
Date: Fri, 16 Oct 2015 14:07:24 -0700

You could write your loop like this:

function raizes3

    format long;
    options =  optimset('TolX',1.0e-15,'TolFun',1.0e-15);

          [x,fval] = fsolve(@f, 0.10000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 0.60000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 1.10000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 1.60000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 2.10000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 2.60000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 3.10000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 3.60000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 4.10000,options)

fprintf('\n');

[x,fval] = fsolve(@f, 4.60000,options)
       
end

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

 

 

So your code computes the roots for 0.1 and prints the results, then 0.6, then 1.1, etc… until all 10 calls are made.  It’s just odd looking in that the roots have the same values for different inputs.  Each time the y function is called, it has no “memory” of the previous call – it computes the roots using the new value passed to it each time.

 

These 2 lines in your original code start a loop:

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

 

And then the code just computes the new root for each new loop variable.

 

Does this make a little more sense now?

 

John

 

 

From: address@hidden [mailto:address@hidden On Behalf Of Fausto Arinos de A. Barbuto
Sent: Friday, October 16, 2015 1:57 PM
To: Doug Stewart <address@hidden>
Cc: address@hidden
Subject: Re: fsolve shows repeated results

 

 

Doug,

 

Right, but I'm still lost as for how fsolve() goes back to root x = 4.603...

after root x = 7.673... had been found. Notice that the last element in list/

vector xi = [0.1:0.5:5.0] is 4.6. Then it is reasonable that root 4.603... be

found for the 6th and last time right before the for-loop (and the execution

as well) ends. But why 7.673... is found and then 4.603... once again?

 

Fausto

 

 

 

 

On Friday, October 16, 2015 5: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

 

 


reply via email to

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