0

From solving n times a numerical (complex) equation, like:

n = 10;
eq = Range[n];


For[a = 1, a < (n + 1), a++, 
 eq[[a]] = 
  NDSolve[{y''[x] + a*y[x] == 0, y[0] == 1 + I, y'[0] == 1 + 0 I}, 
  y, {x, 0, 30}]]

I get the (complex) n solutions in the variable value:

value = Range[n];

For[a = 1, a < (n + 1), a++, 
 value[[a]] = 
   With[{exp = Through[({y} /. First[eq[[a]]])[#]]}, exp &]];

Which is a list of 10 complex interpolating functions. I can plot the real and imaginary parts of each element of value with no problem.

Now, I'm interested in working with the squared modulus of value, i.e. its squared real part plus its squared imaginary part. I'm trying to define two new variables as the real and imaginary parts of value:

valuereal = Range[n];
valueim = Range[n];

For[a = 1, a < (n + 1), a++, 
 valuereal[[a]] = 
   Re[value[[a]]]];

For[a = 1, a < (n + 1), a++, 
 valueim[[a]] = 
   Im[value[[a]]]];

But it seems that I can't plot these new variables (maybe I can't define the real/imaginary parts in this way?). How can I work correctly with the real/imaginary part of value (an complex interpolating function)?

Question: How can work with the real/imaginary parts of an complex Interpolating function?

Note: I tried to work with:

Abs[value[[a]]]

But I had the same problem.

I'd appreciate all kind of answers a lot.

  • 2
    there is no need to use For. Use Table instead. Something like f[a_] := NDSolve[{y''[x] + a*y[x] == 0, y[0] == 1 + I, y'[0] == 1 + 0 I}, y, {x, 0, 30}]; eq = Table[f[a], {a, 1, n}]; – Nasser Jun 09 '16 at 09:02
  • Besides what Nasser already suggested, you should also have a look at this http://mathematica.stackexchange.com/q/2158/21606 and this http://mathematica.stackexchange.com/q/7924/21606. – Lukas Jun 09 '16 at 09:06
  • Nasser: That was somewhat I already had, but In a (much) simpler (and nicer) form. But my problem still there: how can I name a new variable as the real part of an element of eq? – Guillermo Martínez Somonte Jun 09 '16 at 09:23
  • Nasser: what does a stand for? I have to suppose that f(a) is the solution function y(x)? – Guillermo Martínez Somonte Jun 23 '16 at 10:16

1 Answers1

0

Your problem is that you cannot distinguish f[g][x] and f[g[x]]

All your code are in a form like defining com=Im[sol], but when plotting, you plot like com[x] which, after inserting com's definition, becomes Im[sol][x] instead of Im[sol[x]] which will generate the correct result.

So, try use LeftComposition instead of your method.

com=Im@*sol
Plot[com[x],{x,0,1}]

This code will yield desired result.

Hope this post can help you~

Wjx
  • 9,558
  • 1
  • 34
  • 70