Hello, everyone.(adsbygoogle = window.adsbygoogle || []).push({});

I have a rather bizzarre problem. I wrote a code that takes a matrix U[1] and calculates a matrix U[2] from it using simple math. I wrote three For loops, one to go across the U[n], the other two to read the matrix U[n] across all the lines and columns. I do it twice because it's a predictor/corrector kind of computaion. Pretty straightforward. Here's the code:

For[n = 1, n <= 10, n++, UU2 = ConstantArray[0, {dim, dim}];

UU3 = ConstantArray[0, {dim, dim}];

UU = U[n];

UU2[[1, All]] = UU[[1, All]]; UU2[[10, All]] = UU[[10, All]];

UU2[[All, 1]] = UU[[All, 1]]; UU2[[All, 10]] = UU[[All, 10]];

UU3 = UU2;

For[i = 2, i <= dim - 1, i++,

For[j = 2, j <= dim - 1, j++,

UU2[[i, j]] =

UU[[i, j]] - dt/dx*(G[UU[[i, j]]] - G[UU[[i - 1, j]]]) -

dt/dx*(H[UU[[i, j]]] - H[UU[[i, j - 1]]]) + dt*S[UU[[i, j]]];

]

]

For[i = 2, i <= dim - 1, i++,

For[j = 2, j <= dim - 1, j++,

UU3[[i, j]] =

0.5*(UU[[i, j]] + UU2[[i, j]] -

dt/dx*(G[UU2[[i + 1, j]]] - G[UU2[[i, j]]]) -

dt/dx*(H[UU2[[i, j + 1]]] - H[UU2[[i, j]]]) +

dt*S[UU2[[i, j]]]);

]

]

U[n + 1] = UU3;

]

The code above works. It gives me exactly what I need but it's suppose to be a bit unstable so I'm trying to write a different code now. The following code is nigh identical but for some reason Mathematica gives me all kinds of errors and fails to compute. Here's the faulty code:

For[n = 1, n <= 3, n++,

UU2 = ConstantArray[0, {dim, dim}];

UU3 = ConstantArray[0, {dim, dim}];

UU = U[n];

UU2[[1, All]] = UU[[1, All]]; UU2[[dim, All]] = UU[[dim, All]];

UU2[[All, 1]] = UU[[All, 1]]; UU2[[All, dim]] = UU[[All, dim]];

UU3 = UU2;

For[i = 2, i <= dim - 1, i++,

For[j = 2, j <= dim - 1, j++,

UU2[[i, j]] = UU[[i, j]] - dt/dx/2*(G[UU[[i, j]]] - G[UU[[i - 1, j]]]) + dt/2*S[UU[[i, j]]];

]

]

For[i = 2, i <= dim - 1, i++,

For[j = 2, j <= dim - 1, j++,

UU3[[i, j]] = 0.5*(UU[[i, j]] - UU2[[i, j]] - dt/2/dx*(G[UU2[[i + 1, j]]] - G[UU2[[i, j]]]) +

dt/2*S[UU2[[i, j]]]);

]

]

U[n + 1] = UU3;

]

The only difference I can see is that the calculation done in each step of the For loop is a bit shorter since it doesn't employ the H[UU2[[i,j]]] parts. I don't see how this could affect the overall scheme.

These are the errors Mathematica reports when I execute the second piece of code:

Set::write: Tag Times in Null Null U[2] is Protected. >>

Part::partw: Part All of U[2] does not exist. >>

Part::partd: Part specification U[2][[All,1]] is longer than depth of object. >>

Part::partd: Part specification U[2][[All,10]] is longer than depth of object. >>

Part::partw: Part 2 of U[2] does not exist. >>

Part::partw: Part 2 of U[2] does not exist. >>

General::stop: Further output of Part::partw will be suppressed during this calculation. >>

Part::partd: Part specification U[2][[1,2]] is longer than depth of object. >>

General::stop: Further output of Part::partd will be suppressed during this calculation. >>

Set::write: Tag Times in Null Null U[3] is Protected. >>

Set::write: Tag Times in Null Null U[4] is Protected. >>

General::stop: Further output of Set::write will be suppressed during this calculation. >>

Of course I clear all variables before running the second code and reload the definitions for G, H and S which are being used in the computation.

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Mathematica problem: two nigh-identical codes

Loading...

Similar Threads - Mathematica problem nigh | Date |
---|---|

Mathematica Solve Perturbation problem with mathematica | Dec 7, 2017 |

Mathematica For looping problem not getting answer | May 24, 2017 |

Mathematica Graph Plotting Problem in Mathematica | Nov 22, 2016 |

Mathematica - Countour plot problem | Jun 10, 2015 |

Mathematica problem with plotting | Apr 5, 2015 |

**Physics Forums - The Fusion of Science and Community**