I have a problem with mathematica

  • Context: Mathematica 
  • Thread starter Thread starter Reem Hashem
  • Start date Start date
  • Tags Tags
    Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 3K views
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]}]]]
 
on Phys.org
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.