Message Boards Message Boards

0
|
6067 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Avoid "Error General::partw" while using Part on the following code?

Posted 6 years ago

This code has an error but I do not know how to fix it

Clear[a, b, n, h, ETA, f, H, G, F1, F2, F3, k1, k2, k3, k4, l1, l2, \
l3, l4, m1, m2, m3, m4, Error]

a = 0;
b = 10;

n = 20;

h = (b - a)/n;

ETA = f = G = H = Error = Table[0, {n + 10}];

ETA[[1]] = 0;

f[[1]] = 0;
G[[1]] = 0;




F1[G_] := G;
F2[H_] := H;
F3[f_, H_] := -(1/2)*f*H;
Do[
 H[[1]] = 0.01*j;
 Print[Error[[j]] = 1 - G[[20]], "   ", 0.01*j, "   ", j];
 Do[

  k1 = h*F1[G[[i]]];
  l1 = h*F2[H[[i]]];
  m1 = h*F3[f[[i]], H[[i]]];

  k2 = h*F1[G[[i]] + l1/2];
  l2 = h*F2[H[[i]] + m1/2];
  m2 = h*F3[f[[i]] + k1/2, H[[i]] + m1/2 ];

  k3 = h*F1[G[[i]] + l2/2];
  l3 = h*F2[H[[i]] + m2/2];
  m3 = h*F3[f[[i]] + k2/2, H[[i]] + m2/2 ];

  k4 = h*F1[G[[i]] + l3];
  l4 = h*F2[H[[i]] + m3];
  m4 = h*F3[f[[i]] + k3, H[[i]] + m3];


  f[[i + 1]] = f[[i]] + (k1 + 2*k2 + 2*k3 + k4)/6;
  G[[i + 1]] = G[[i]] + (l1 + 2*l2 + 2*l3 + l4)/6;
  H[[i + 1]] = H[[i]] + (m1 + 2*m2 + 2*m3 + m4)/6;
  ETA[[i + 1]] = a + i*h

  , {i, n + 1}]


 , {j, 1, 99}]

These are the mistakes

Set::partw: Part 31 of {1,0.91881,0.856369,0.804695,0.759696,0.719129,0.681693,0.64659,0.613306,0.581493,0.550904,0.52136,0.492723,0.464887,0.437766,0.411289,0.385399,0.360045,0.335185,0.310783,0.286806,0.263226,0.240017,0.217158,0.194628,0.172409,0.150485,0.12884,0.10746,0.0863339} does not exist.

Set::partw: Part 32 of {1,0.91881,0.856369,0.804695,0.759696,0.719129,0.681693,0.64659,0.613306,0.581493,0.550904,0.52136,0.492723,0.464887,0.437766,0.411289,0.385399,0.360045,0.335185,0.310783,0.286806,0.263226,0.240017,0.217158,0.194628,0.172409,0.150485,0.12884,0.10746,0.0863339} does not exist.

Set::partw: Part 33 of {1,0.91881,0.856369,0.804695,0.759696,0.719129,0.681693,0.64659,0.613306,0.581493,0.550904,0.52136,0.492723,0.464887,0.437766,0.411289,0.385399,0.360045,0.335185,0.310783,0.286806,0.263226,0.240017,0.217158,0.194628,0.172409,0.150485,0.12884,0.10746,0.0863339} does not exist.
POSTED BY: diegosachica
3 Replies
Posted 6 years ago

I can't see what you're trying to do: your intent is lost in the complexity. So consider a simpler thing. We have lists a, b, and c. For each element of a, we want to add it to the corresponding element of b, and multiply the sum by the sine of the corresponding element of c. We can do it the complicated way:

sum = 0;
Do[
    sum += (a[[i]]+b[[i]]) Sin[c[[i]]],
    {i, 1, 10}];
sum

Or, we can express the idea much more simply and clearly:

(a+b).Sin[c]

We take advantage of the fact that Plus and Sin automatically thread over lists. Sum of products is an extremely common pattern, which Dot implements. Note that in the complicated version we have unnecessary symbols i and sum, and it only works for lists of length 10. The simple way works for lists of any length: the only have to be the same length.

Mathematica has functions that compactly express common operations on arrays: Inner, Outer, Map, Thread, ListConvolve, etc. By using them you make your code clearer, shorter, and easier to debug.

POSTED BY: Updating Name

Well, one problem is:

n = 20;
ETA = f = G = H = Error = Table[0, {n + 10}];
Print[Error[[j]] = 1 - G[[20]], "   ", 0.01*j, "   ", j];

j runs from 1 to 99, but Error only has 30 elements.

This code is almost incomprehensible, doing element by element operations in a language much more suited to vectors.

POSTED BY: John Doty
Posted 6 years ago

Hello John Doty, thanks for your help, the error was solved, but what are the advantages using vectors and how would you do it?

POSTED BY: diegosachica
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract