2

I'm starting using Mathematica. I have two coupled differential equations and after using NDSolve the output is in the form of a Interpolating Function. I want to Plot the square modulus of one of the two solutions, but after several trials the plot is still empty. I want to know how to work with this function.

sol = NDSolve[{a1'[t] == -I*V12*Exp[-I*w21*t]*a2[t], 
 a2'[t] == -I*V21*Exp[I*w21*t]*a2[t], a1[0] == 1, a2[0] == 0},
 {a1, a2}, {t, 40}] 

(* {{a1->InterpolatingFunction[{{0.,40.}},"<>"],
  a2->InterpolatingFunction[{{0.,4‌​0.}},"<>"]}}    *)  

f1=a1->InterpolatingFunction[{{0.,40.}},"<>"]      

f2 = Conjugate[fun] // ComplexExpand 
f3 = fun*gun Plot[f3, {t, 0, 40}]

Where fun is f1 and gun is f2.

Verbeia
  • 34,233
  • 9
  • 109
  • 224
user13722
  • 61
  • 1
  • 2

1 Answers1

5

I can't make heads or tails of above, but perhaps this will get you started. Read the documentation - it's your best source of information.

First, let's get some solution from NDSolve :

sol =  NDSolve[{u''[t] + u[t] == 0, u[0] == 0, u'[0] == 1}, u, {t, 0, \[Pi]}]

(* {{u->InterpolatingFunction[{{0.,3.14159}},<>]}} *)

So, NDSolve has given us a set of Rules (in this case, just one) that maps the solution for u to an InterpolatingFunction. Read the documentation re: rules if you are new to the concept.

So, we'll take that rule and extract the function itself into a convenient name:

interpFN = u /. First@sol

(* InterpolatingFunction[{{0.,3.14159}},<>] *)

We can use this (pretty much) like any other function:

interpFN[2]
(* 0.909297 *)

And plot it in the same way:

Plot[interpFN[x], {x, 0, Pi}]

enter image description here

Hope that helped somewhat...

ciao
  • 25,774
  • 2
  • 58
  • 139
  • Thanks for answer!, but what about if I want to plot the square modulus of "u" assuming that is a complex function? I can conjugate individual points of the Interpolating function, but I fail conjugating the whole function – user13722 Apr 16 '14 at 16:24