I have created a list as shown in picture.enter image description here

the digits inside f are values of a and q respectively. I want to solve the Mathieu equation..x''[t]+(a-2*q*Cos[2*t])*x[t]==0,x[0]==1,x[0]==1 now i want to use each element of the list to solve this equation. I could solve this equation simply by giving value of a and q as shown in the picture by putting value of a and q, but then I will have to do it a very large times. So I want to do the same by using each element of the list. I think this can be done by looping but I don't know how. Please help.

  • 1
    $\begingroup$ Kamal, this is the third question you post, with minimal changes. Here are a few things you will want to do. 1) DO NOT SHOW IMAGES OF CODE. Instead, paste code in plain text, formatted appropriately, so we can copy / paste it into our MMA. 2) Please explain clearly and completely what you are trying to achieve. Explain the problem in words, then your approach, then show the code. $\endgroup$ – MarcoB Mar 4 '19 at 17:28
  • $\begingroup$ ok, actually I want to solve the Mathieu equation and plot the a and q parameter, but at the first I posted simpler problem to get the logic to solve it. $\endgroup$ – Kamal Kumar Mar 5 '19 at 5:53

Here is a modified version of your function:

f[{a_, q_}] := (sol = NDSolve[{x''[t] + (a - 2*q*Cos[2*t])*x[t] == 0, 
                    x[0] == 1, x'[0] == 1}, x, {t, 0, 5}];
               (x /. First[sol]) /@ Range[0, 5, 0.2])

so that f[{1,1}] gives the solution for a=1, q=1. To do it for all the a's and q's, try something like this:

aqs = Flatten[Outer[List, Range[0, 1, 0.1], Range[0, 1, 0.1]], 1]
f[#] & /@ aqs

The Outer function gives all the Lists that will define the {a,q} pairs, which is named aqs. These are mapped in the second line to values for f to act on.

| improve this answer | |
  • $\begingroup$ Thanks, this way i got a matrix of different values of x, now I want to select x which lie between -1 and 1, I selected by doing Select[list,-1<#<1&], now how can I know that which element of the list corresponds to which {a,q}. $\endgroup$ – Kamal Kumar Mar 5 '19 at 8:02
  • $\begingroup$ You can see the correspondence between a, q and the output by looking at the matrix aqs. f is applied to the elements of aqs in order. $\endgroup$ – bill s Mar 5 '19 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.