开发者

Mathematica NDSolve: Is there a way to have variable coefficients?

开发者 https://www.devze.com 2023-02-20 11:02 出处:网络
Is there a way in mathematica to have variable coefficients for NDSolve? I need to vary the coefficient values and create multiple graphs, but I cannot figure out a way to do it short of reentering th

Is there a way in mathematica to have variable coefficients for NDSolve? I need to vary the coefficient values and create multiple graphs, but I cannot figure out a way to do it short of reentering the entire expression for every graph. Here is an example (non-functional) of what I would like to do; hopefully it is close to working:

X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] = 
  NDSolve[{\[Rho]eg'[t] == 
          (I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] - 
           I*.5*\[CapitalOmega]*\[Rho]ee[t] + 
           I*.5*\[CapitalOmega]*\[Rho]gg[t], 
    \[Rho]ge'[t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] + 
           I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] - 
           I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t],
    \[Rho]ee'[t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] + 
           I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[t],
    \[Rho]gg'[t] == I*.5*\[CapitalOmega]\[Conjugate]*\[R开发者_C百科ho]eg[t] - 
           I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[t],
    \[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] == 0, \[Rho]eg[0] == 0},
    {\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[\[Rho]ee[t] /. X[5, 2, 6]], {t, 0, 10},PlotRange -> {0, 1}]

In this way I would only have to re-call the plot command with inputs for the coefficients, rather than re-enter the entire sequence over and over. That would make things much cleaner.

PS: Apologies for the awful looking code. I never realized until now that mathematica didn't keep the character conversions.

EDIT a nicer formatted version:

Mathematica NDSolve: Is there a way to have variable coefficients?


You should just use SetDelayed (":=") instead of Set in the function definition:

X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] := 
  NDSolve[{\[Rho]eg'[
      t] == (I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] - 
      I*.5*\[CapitalOmega]*\[Rho]ee[t] + 
      I*.5*\[CapitalOmega]*\[Rho]gg[t], \[Rho]ge'[
      t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] + 
      I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] - 
      I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t], \[Rho]ee'[
      t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] + 
      I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[
        t], \[Rho]gg'[t] == 
     I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] - 
      I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[
        t], \[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] == 
     0, \[Rho]eg[0] == 
     0}, {\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[{\[Rho]ee[t] /. X[5, 2, 6], \[Rho]ee[t] /. 
    X[2, 6, 17]}], {t, 0, 10}, PlotRange -> {0, 1}]
0

精彩评论

暂无评论...
验证码 换一张
取 消