Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
negative value in loop

Subject: negative value in loop

From: Azrul

Date: 9 Mar, 2011 00:30:22

Message: 1 of 4

hi all,

I have a coding that store positive and negative value in a loop. However, seems that only positive values are stored while there are Matlab shows 'NaN' for negative values. My code is :

format long
x_3=zeros(1000,1);

for n=1:1000;
    x_3(1)=4;
    x_3(n+1)=((x_3(n).^3)-5.*(x_3(n).^2)+25)./3;
    if abs(x_3(n+1)-x_3(n))<=(10^(-6));
        break
    end
    num2str(x_3,'%.6f')
end

My question is, how can i obtain that NaN value ( i think it store negative value ). Thank you in advance.

Subject: negative value in loop

From: dpb

Date: 9 Mar, 2011 01:53:03

Message: 2 of 4

On 3/8/2011 6:30 PM, Azrul wrote:
> hi all,
>
> I have a coding that store positive and negative value in a loop.
> However, seems that only positive values are stored while there are
> Matlab shows 'NaN' for negative values. My code is :
>
> format long
> x_3=zeros(1000,1);
>
> for n=1:1000;
> x_3(1)=4;
> x_3(n+1)=((x_3(n).^3)-5.*(x_3(n).^2)+25)./3;
> if abs(x_3(n+1)-x_3(n))<=(10^(-6));
> break
> end
> num2str(x_3,'%.6f')
> end
>
> My question is, how can i obtain that NaN value ( i think it store
> negative value ). Thank you in advance.

I get no NaN's here although your code is equivalent to

x(1)=4;
for n=2:1000
   x(n) = (x(n-1)^3 - 5*x(n-1)^2 +25)/3;
end

If I do this here I get

 >> x=zeros(1000,1); x(1)=4;
 >> for n=2:1000,x(n)=((x(n-1).^3)-5.*(x(n-1).^2)+25)./3;end
 >> x(1:5);
 >> x(1:5)'
ans =
     4.0000 3.0000 2.3333 3.4938 2.2048
 >> x(995:1000)'
ans =
     2.4235 3.2891 2.1637 3.9071 2.7722 2.6264
 >> max(x), min(x)
ans =
      4
ans =
     2.1605
 >> min(abs(diff(x)))
ans =
     0.0038
 >>

Not sure where you NaNs are coming from; your version gives identical
results numerically here.

--

Subject: negative value in loop

From: Roger Stafford

Date: 9 Mar, 2011 05:30:22

Message: 3 of 4

"Azrul " <azrul.afifi@gmail.com> wrote in message <il6hmu$5gd$1@fred.mathworks.com>...
> I have a coding that store positive and negative value in a loop. However, seems that only positive values are stored while there are Matlab shows 'NaN' for negative values. My code is :
> .......
- - - - - - - -
  It looks as though your iteration is an attempt to converge on one of the roots of x^3-5*x^2-3*x+25 = 0, namely the root x = 2.7165. The three roots of this cubic are necessarily fixed points in that iteration.

  Unfortunately in the near vicinity of the above root your iteration is highly unstable and diverges from, rather than converges to, the root. If an x value is a tiny epsilon above the root, the next x will be about 1.675*epsilon below the root and successive iterations will oscillate endlessly above and below the root, never remaining near the root for very long. In my trial run the largest x, excluding the initial 4 was 3.9152 and the smallest was 2.1605 (in agreement with dpb's findings,) and approaches to these two extremes were still being made near the end of the thousand iterations. If you make a plot of the successive x values, you will see what I mean.

Roger Stafford

Subject: negative value in loop

From: dpb

Date: 9 Mar, 2011 14:43:17

Message: 4 of 4

On 3/8/2011 11:30 PM, Roger Stafford wrote:
> "Azrul " <azrul.afifi@gmail.com> wrote in message
> <il6hmu$5gd$1@fred.mathworks.com>...
>> I have a coding that store positive and negative value in a loop.
>> However, seems that only positive values are stored while there are
>> Matlab shows 'NaN' for negative values. My code is :
>> .......
> - - - - - - - -
> It looks as though your iteration is an attempt to converge on one of
> the roots of x^3-5*x^2-3*x+25 = 0, namely the root x = 2.7165. The three
> roots of this cubic are necessarily fixed points in that iteration.
>
> Unfortunately in the near vicinity of the above root your iteration is
> highly unstable and diverges from, rather than converges to, the root.
> If an x value is a tiny epsilon above the root, the next x will be about
> 1.675*epsilon below the root and successive iterations will oscillate
> endlessly above and below the root, never remaining near the root for
> very long. In my trial run the largest x, excluding the initial 4 was
> 3.9152 and the smallest was 2.1605 (in agreement with dpb's findings,)
> and approaches to these two extremes were still being made near the end
> of the thousand iterations. If you make a plot of the successive x
> values, you will see what I mean.

I was rushed for time last night so to amplify...

If, indeed the goal is to find the roots, Matlab has much better
tools...if one is only interested in a real root, fzero is much more
robust than the above scheme...

 >> fzero(inline('x^3 - 5*x^2 + 25'),4)
ans =
    -1.9030
 >>

Succeeds even w/ a pretty sorry initial starting point...

If one desires all the roots, then roots() is the choice...

 >> roots([1,-5,0,25])
ans =
    3.4515 + 1.1063i
    3.4515 - 1.1063i
   -1.9030
 >>

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us