Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.8K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Can we use decimal values as array elements?

Posted 4 years ago

Dear All,

Usually we define the array elements in the form x[0],x[1],x[2], etc

But instead can we define array elements in the form x[0.1],x[0.2],etc

In my code I use a For loop, within this loop I need to use array elements with decimal entries.

We all are aware of using integer elements in arrays like

For[j=1,j<6,j++,x[j]=2*x[j-1]];

my situation is i need to find x[0.1],x[0.2], etc. so i tried a code like the one below

For[j=1,j<6,j++,g=j/10; x[g]=2*x[g-0.1]];

Table[{j, x[j]}, {j, 0, 5, 0.1}]

and my output was

{{0., x[0.]}, {0.1, x[0.1]}, {0.2, x[0.2]}, {0.3, x[0.3]}, {0.4, 
  x[0.4]}, {0.5, x[0.5]}, {0.6, x[0.6]}, {0.7, x[0.7]}, {0.8, 
  x[0.8]}, {0.9, x[0.9]}, {1., x[1.]}, {1.1, x[1.1]}, {1.2, 
  x[1.2]}, {1.3, x[1.3]}, {1.4, x[1.4]}, {1.5, x[1.5]}, {1.6, 
  x[1.6]}, {1.7, x[1.7]}, {1.8, x[1.8]}, {1.9, x[1.9]}, {2., 
  x[2.]}, {2.1, x[2.1]}, {2.2, x[2.2]}, {2.3, x[2.3]}, {2.4, 
  x[2.4]}, {2.5, x[2.5]}, {2.6, x[2.6]}, {2.7, x[2.7]}, {2.8, 
  x[2.8]}, {2.9, x[2.9]}, {3., x[3.]}, {3.1, x[3.1]}, {3.2, 
  x[3.2]}, {3.3, x[3.3]}, {3.4, x[3.4]}, {3.5, x[3.5]}, {3.6, 
  x[3.6]}, {3.7, x[3.7]}, {3.8, x[3.8]}, {3.9, x[3.9]}, {4., 
  x[4.]}, {4.1, x[4.1]}, {4.2, x[4.2]}, {4.3, x[4.3]}, {4.4, 
  x[4.4]}, {4.5, x[4.5]}, {4.6, x[4.6]}, {4.7, x[4.7]}, {4.8, 
  x[4.8]}, {4.9, x[4.9]}, {5., x[5.]}}

Could anyone please help me to solve this issue?

4 Replies

You expect machine arithmetic to behave as if it were exact. Try this:

f[t_, x_] := 2 x;
x[0] = 1;
For[j = 1, j < 21, j++, g = j/10;
  t[g] = j*1/100;
  x[g] = 2 + f[t[g], x[g - 1/10]]];
x[gg_Real] := x[Rationalize[gg]];
Table[{j, x[j]}, {j, 0, 1, .1}]
POSTED BY: Gianluca Gorni

Yes x is a function of time

For example,

 f[t_, x_] := 2 x;

x[0.] = 1;

For[j = 1, j < 21, j++, g = N[j/10]; 

t[g] = j*0.01; 

 x[g] = 2 + f[t[g], x[g - 0.1]]];

Table[{j, x[j]}, {j, 0, 1, 0.1}]

Gives the output,
 {{0., 1}, {0.1, 4}, {0.2, 10}, {0.3, 22}, {0.4, 46}, {0.5, 
  94}, {0.6, 190}, {0.7, x[0.7]}, {0.8, 2 + 2 x[0.7]}, {0.9, 
  2 + 2 (2 + 2 x[0.7])}, {1., 2 + 2 (2 + 2 (2 + 2 x[0.7]))}}

Only for few values are manipulated.

Posted 4 years ago

Hi Vijayalakshmi,

In the WL, List elements are referenced using Part.

l = Range[5];
l[[3]]
(* 3 *)

Part[l, 3]
(* 3 *)

A fractional index does not make sense. What exactly is x? It cannot be a list. Is it a function?

POSTED BY: Rohit Namjoshi

There is a mix of exact and inexact numbers that do not match. Try this way:

For[j = 1, j < 6, j++, g = N[j/10]; x[g] = 2*x[g - 1/10]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard