I have a problem with mathematica

  • Context: Mathematica 
  • Thread starter Thread starter Reem Hashem
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Mathematica program that involves solving a system of differential equations using NDSolve. Participants share their experiences with errors encountered in the code, propose corrections, and discuss differences in output from various methods of implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original poster describes a complex Mathematica program with multiple nested loops and differential equations, mentioning errors they could not resolve.
  • One participant asks for clarification on the specific errors encountered by the original poster.
  • Another participant provides a corrected version of the code, emphasizing the importance of semicolons and curly braces in Mathematica, and notes that the d matrix was not as expected.
  • A later reply expresses gratitude for the help received and mentions a discrepancy in results when using a different method, asking for clarification on the reason for the difference.
  • One participant admits to misunderstanding the original poster's approach and notes that their method does not produce the same matrix as intended, suggesting a simpler way to construct the d matrix.
  • Another participant acknowledges their mistake regarding the d matrix and confirms that removing a duplicate line corrected the output.
  • One participant tests a modified version of the code and reports that while some results match, others appear to be reversed, prompting a call for careful testing of the code.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct errors in the code and the importance of understanding the structure of the d matrix. However, there are competing views regarding the best methods to achieve the desired results, and discrepancies in outputs remain unresolved.

Contextual Notes

Some participants highlight the complexity of the code and the potential for multiple errors, indicating that the discussion may not cover all assumptions or dependencies inherent in the original problem.

Reem Hashem
Messages
23
Reaction score
0
I wrote this programm in mathematica but I had some errors that I could not solved
I hope that anybody could help me
For [a = 0, a <= 30, a = a + 10,
For[z = 3.7, z <= 5, z = z + 0.2,
For[w = 1000, w <= 20000, w = w + 1000,
{m = NDSolve[{Derivative[1][y1][t] == y2[t],
Derivative[1][y2][t] == y3[t], Derivative[1][y3][t] == y4[t],
Derivative[1][y4][t] == -z^4 y1[t] + 2 z^2 y3[t] +
z^2 w (-t + t^2) y5[t] + a y3[t],
Derivative[1][y5][t] == y6[t],
Derivative[1][y6][t] == (-1 + 2 t) y1[t] + z^2 y5[t] -
a y8[t], Derivative[1][y8][t] == y7[t],
Derivative[1][y7][t] == -y6[t] + z^2 y8[t],
y1[0] == y2[0] == y5[0] == y7[0] == 0, y3[0] == 1,
y4[0] == y6[0] == y8[0] == 0}, {y1, y2, y3, y4, y5, y6, y7,
y8}, {t, 0, 1}]}
{n = NDSolve[{Derivative[1][y9][t] == y10[t],
Derivative[1][y10][t] == y11[t],
Derivative[1][y11][t] == y12[t],
Derivative[1][y12][t] == -z^4 y9[t] + 2 z^2 y11[t] +
z^2 w (-t + t^2) y13[t] + a y11[t],
Derivative[1][y13][t] == y14[t],
Derivative[1][y14][t] == (-1 + 2 t) y9[t] + z^2 y13[t] -
a y16[t], Derivative[1][y16][t] == y15[t],
Derivative[1][y15][t] == -y14[t] + z^2 y16[t],
y9[0] == y10[0] == y13[0] == y15[0] == 0, y12[0] == 1,
y11[0] == y14[0] == y16[0] == 0}, {y9, y10, y11, y12, y13,
y14, y15, y16}, {t, 0, 1}]}
{l = NDSolve[{Derivative[1][y17][t] == y18[t],
Derivative[1][y18][t] == y19[t],
Derivative[1][y19][t] == y20[t],
Derivative[1][y20][t] == -z^4 y17[t] + 2 z^2 y19[t] +
z^2 w (-t + t^2) y21[t] + a y19[t],
Derivative[1][y21][t] == y22[t],
Derivative[1][y22][t] == (-1 + 2 t) y17[t] + z^2 y20[t] -
a y24[t], Derivative[1][y24][t] == y23[t],
Derivative[1][y23][t] == -y22[t] + z^2 y24[t],
y17[0] == y18[0] == y21[0] == y23[0] == 0, y22[0] == 1,
y20[0] == y19[0] == y24[0] == 0}, {y17, y18, y19, y20, y21,
y22, y23, y24}, {t, 0, 1}]}
{c = NDSolve[{Derivative[1][y25][t] == y26[t],
Derivative[1][y26][t] == y27[t],
Derivative[1][y27][t] == y28[t],
Derivative[1][y28][t] == -z^4 y25[t] + 2 z^2 y27[t] +
z^2 w (-t + t^2) y29[t] + a y27[t],
Derivative[1][y29][t] == y30[t],
Derivative[1][y30][t] == (-1 + 2 t) y25[t] + z^2 y28[t] -
a y32[t], Derivative[1][y32][t] == y31[t],
Derivative[1][y31][t] == -y30[t] + z^2 y32[t],
y25[0] == y26[0] == y29[0] == y31[0] == 0, y30[0] == 1,
y28[0] == y27[0] == y32[0] == 0}, {y25, y26, y27, y28, y29,
y30, y31, y32}, {t, 0, 1}]}
{d = Array[f, {4, 4}]}
{{f[1, 1]} = y1[1] /. m}
{{f[1, 2]} = y9[1] /. n}
{{f[1, 3]} = y17[1] /. l}
{{f[1, 4]} = y25[1] /. c}
{{f[2, 1]} = y2[1] /. m}
{{f[2, 2]} = y10[1] /. n}
{{f[2, 3]} = y18[1] /. l}
{{f[2, 4]} = y26[1] /. c}
{{f[3, 1]} = y5[1] /. m}
{{f[3, 2]} = y13[1] /. n}
{{f[3, 3]} = y21[1] /. l}
{{f[3, 4]} = y29[1] /. c}
{{f[4, 1]} = y7[1] /. m}
{{f[4, 2]} = y15[1] /. n}
{{f[4, 3]} = y23[1] /. l}
{{f[4, 4]} = y31[1] /. c}
{d = Array[f, {4, 4}]}
{b = Det[d]}
{Print[ a, " " , z, " ", w, " ", b]}]]]
 
Physics news on Phys.org
What particular errors?
 
This fixes the first couple of layers of errors.
Semicolons and {} are very different in Mathematica from what are used in other languages.
And your d matrix was not what you expected it to be.
I expect there are more errors to be found and fixed.

For[a = 0, a <= 30, a = a + 10,
For[z = 3.7, z <= 5, z = z + 0.2,
For[w = 1000, w <= 20000, w = w + 1000,
m = NDSolve[{y1'[t] == y2[t], y2'[t] == y3[t], y3'[t] == y4[t],
y4'[t] == -z^4 y1[t] + 2 z^2 y3[t] + z^2 w (-t + t^2) y5[t] + a y3[t], y5'[t] == y6[t],
y6'[t] == (-1 + 2 t) y1[t] + z^2 y5[t] - a y8[t], y8'[t] == y7[t], y7'[t] == -y6[t] + z^2 y8[t],
y1[0] == y2[0] == y5[0] == y7[0] == 0, y3[0] == 1, y4[0] == y6[0] == y8[0] == 0},
{y1, y2, y3, y4, y5, y6, y7, y8}, {t, 0, 1}];
n = NDSolve[{y9'[t] == y10[t], y10'[t] == y11[t], y11'[t] == y12[t],
y12'[t] == -z^4 y9[t] + 2 z^2 y11[t] + z^2 w (-t + t^2) y13[t] + a y11[t], y13'[t] == y14[t],
y14'[t] == (-1 + 2 t) y9[t] + z^2 y13[t] - a y16[t], y16'[t] == y15[t], y15'[t] == -y14[t] + z^2 y16[t],
y9[0] == y10[0] == y13[0] == y15[0] == 0, y12[0] == 1, y11[0] == y14[0] == y16[0] == 0},
{y9, y10, y11, y12, y13, y14, y15, y16}, {t, 0, 1}];
l = NDSolve[{y17'[t] == y18[t], y18'[t] == y19[t], y19'[t] == y20[t],
y20'[t] == -z^4 y17[t] + 2 z^2 y19[t] + z^2 w (-t + t^2) y21[t] + a y19[t], y21'[t] == y22[t],
y22'[t] == (-1 + 2 t) y17[t] + z^2 y20[t] - a y24[t], y24'[t] == y23[t], y23'[t] == -y22[t] + z^2 y24[t],
y17[0] == y18[0] == y21[0] == y23[0] == 0, y22[0] == 1, y20[0] == y19[0] == y24[0] == 0},
{y17, y18, y19, y20, y21, y22,y23, y24}, {t, 0, 1}];
c = NDSolve[{y25'[t] == y26[t], y26'[t] == y27[t], y27'[t] == y28[t],
y28'[t] == -z^4 y25[t] + 2 z^2 y27[t] + z^2 w (-t + t^2) y29[t] + a y27[t], y29'[t] == y30[t],
y30'[t] == (-1 + 2 t) y25[t] + z^2 y28[t] - a y32[t], y32'[t] == y31[t], y31'[t] == -y30[t] + z^2 y32[t],
y25[0] == y26[0] == y29[0] == y31[0] == 0, y30[0] == 1, y28[0] == y27[0] == y32[0] == 0},
{y25, y26, y27, y28, y29, y30, y31, y32}, {t, 0, 1}];
d = Array[f, {4, 4}];
f[1, 1] = y1[1] /. m; f[1, 2] = y9[1] /. n; f[1, 3] = y17[1] /. l; f[1, 4] = y25[1] /. c;
f[2, 1] = y2[1] /. m; f[2, 2] = y10[1] /. n; f[2, 3] = y18[1] /. l; f[2, 4] = y26[1] /. c;
f[3, 1] = y5[1] /. m; f[3, 2] = y13[1] /. n; f[3, 3] = y21[1] /. l; f[3, 4] = y29[1] /. c;
f[4, 1] = y7[1] /. m; f[4, 2] = y15[1] /. n; f[4, 3] = y23[1] /. l; f[4, 4] = y31[1] /. c;
d = Partition[Flatten[d], 4];
b = Det[d]; Print[a, " ", z, " ", w, " ", b]
]
]
]
 
Last edited:
Thank you very much, that was really helpful.

I am facing a little problem when I run my code in another method, it outputs a slightly different result than what your code does. Can you please manifest the reason for that?

See the attachments.
 

Attachments

I thought I had figured out what you were doing. It appears that I was wrong. If you print out the contents of the d matrix on each iteration for each method you can see that my method is not creating the same matrix. Sorry. I should have made certain the output matched your original method, but it isn't clear how I would have done that.

I still don't understand why you are doing this: d = Array[f, {4, 4}] twice.

It seems like there should be a simpler way of constructing that d matrix, even simpler than what I showed, but apparently I haven't figured out how you are using your subscripts.

At least you were able to get the flood of errors corrected and the semicolon versus {} working. Maybe that is enough for you.
 
THANK you Mr Bill for your help
I knew my mistake it was writing d matrix twice so when I removed one of them the output became the same of yours
Thank you very much
 
Now to test and see if either of the results are correct.

I tried to modify your code like this:

{m} = NDSolve...
{n} = NDSolve...
{l} = NDSolve...
{c} = NDSolve...
d = {{y1[1], y9[1], y17[1], y25[1]},
{y2[1], y10[1], y18[1], y26[1]},
{y5[1], y13[1], y21[1], y29[1]},
{y7[1], y15[1], y23[1], y31[1]}} /. Join[m, n, l, c];

The first 7 results are the same with my method and this modified method.
The 8th and 9th results appear to be reversed.
The 10th up to 20th results are the same.
The 21th and 22th results appear to be reversed.
etc.

Please test your code carefully to make certain the results are correct.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K