0
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ 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}]; $\endgroup$ – Nasser Jun 9 '16 at 9:02
  • $\begingroup$ Besides what Nasser already suggested, you should also have a look at this mathematica.stackexchange.com/q/2158/21606 and this mathematica.stackexchange.com/q/7924/21606. $\endgroup$ – Lukas Jun 9 '16 at 9:06
  • $\begingroup$ 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? $\endgroup$ – Guillermo Martínez Somonte Jun 9 '16 at 9:23
  • $\begingroup$ Nasser: what does a stand for? I have to suppose that f(a) is the solution function y(x)? $\endgroup$ – Guillermo Martínez Somonte Jun 23 '16 at 10:16
0
$\begingroup$

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~

$\endgroup$

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.