COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Help: array iterations outcoming is NaN; WHY?

• Follow

```Im using the next function to generate vectors according to the equation:
x(i+1)=x(i) + 3*x(i)^2 - 2*x(i)^3 + 0.5*x(i-1)

function [M] = generate(n)
x1=rand(30,1);
x2=rand(30,1);
M(:,1)=x1;
M(:,2)=x2;
for i=2:n-1
M(:,i+1)=M(:,i)+3*(M(:,i)).^2-2*(M(:,i)).^3+0.5*M(:,i-1);
end

but when I try n=100 , the last 94 columns of M result in NaN. Is this normal when the entries of the column are of order 1e-100000... ? That is, too very small; or too big of the order 1e100000... ? or am I commiting some error?
```
 0

```"landon donovan" <lutobu@gmail.com> wrote in message <hsdb7j\$mj9\$1@fred.mathworks.com>...
> Im using the next function to generate vectors according to the equation:
> x(i+1)=x(i) + 3*x(i)^2 - 2*x(i)^3 + 0.5*x(i-1)
>
> function [M] = generate(n)
> x1=rand(30,1);
> x2=rand(30,1);
> M(:,1)=x1;
> M(:,2)=x2;
> for i=2:n-1
>     M(:,i+1)=M(:,i)+3*(M(:,i)).^2-2*(M(:,i)).^3+0.5*M(:,i-1);
> end
>
> but when I try n=100 , the last 94 columns of M result in NaN. Is this normal when the entries of the column are of order 1e-100000... ? That is, too very small; or too big of the order 1e100000... ? or am I commiting some error?

Haven't tried your code, but it looks like the recursive scheme is unstable. That's why people spending their time studying stability, convergence, ...

Bruno
```
 0

```"landon donovan" <lutobu@gmail.com> wrote in message <hsdb7j\$mj9\$1@fred.mathworks.com>...
> Im using the next function to generate vectors according to the equation:
> x(i+1)=x(i) + 3*x(i)^2 - 2*x(i)^3 + 0.5*x(i-1)
>
> function [M] = generate(n)
> x1=rand(30,1);
> x2=rand(30,1);
> M(:,1)=x1;
> M(:,2)=x2;
> for i=2:n-1
>     M(:,i+1)=M(:,i)+3*(M(:,i)).^2-2*(M(:,i)).^3+0.5*M(:,i-1);
> end
>
> but when I try n=100 , the last 94 columns of M result in NaN. Is this normal when the entries of the column are of order 1e-100000... ? That is, too very small; or too big of the order 1e100000... ? or am I commiting some error?

The individual pieces of your polynomial are becoming very large. Eventually they become too large for a finite value and become Infinity. And then at some point you do an Infinity - Infinity calculation and the result is NaN. This is not a stable way to generate whatever you are trying to generate.

James Tursa
```
 0

2 Replies
225 Views

6/20/2013 11:45:33 AM