Group Abstract Group Abstract

Message Boards Message Boards

Using function in table and plot its solution?

Posted 10 years ago

Hello,

I have solved a differential equation using NDSolve, and then put the solution inside a Table. Now I want to call the solution from the table and plot it, or check the solution's value for specific values of the variable t. How do I do it? I can't manage to call the value of the solution from the Table. I did the following:

Refine[s = NDSolve[{r\[Rho]ee'[t] == 2 \[CapitalOmega] im\[Rho]eg[t],im\[Rho]ee'[t] == 0, r\[Rho]eg'[t] == 2 \[CapitalOmega] im\[Rho]ee[t],
     im\[Rho]eg'[t] == \[CapitalOmega] (1 - 2 r\[Rho]ee[t]), r\[Rho]ee[0] == 0, im\[Rho]ee[0] == 0, r\[Rho]eg[0] == 0, im\[Rho]eg[0] == 0}, {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t],
      r\[Rho]eg[t]}, {t, 0, 1}], (*here there is a list of conditions  for the functions*)
  r\[Rho]ee \[Element] Reals && im\[Rho]ee \[Element] Reals && r\[Rho]eg \[Element] Reals && im\[Rho]eg \[Element] Reals && \[CapitalOmega] [Element] Reals && \[Gamma]eg \[Element] Reals && 0 <= r\[Rho]ee[t]^2 + im\[Rho]ee[t]^2 <= 1];

(*Then I defined a Table and put the solution in the Table*)
\[Rho]List = Table[1, {i, 5}];
\[Rho]List[[1]] = s;

Now I want to call the first function from the solution, and plot it, and I try the following:

\[Rho]ee = \[Rho]List[[1]][[1]][[1]]
Plot[\[Rho]ee[t], {t, 0, 1}]

And I get nothing in the plot. Any help?

Thanks!

POSTED BY: shaked rozen
3 Replies
Posted 10 years ago

Is this close enough?

\[CapitalOmega] = 1;
s = Refine[
    NDSolve[{
      r\[Rho]ee'[t] == 2 \[CapitalOmega] im\[Rho]eg[t], 
      im\[Rho]ee'[t] == 0, 
     r\[Rho]eg'[t] == 2 \[CapitalOmega] im\[Rho]ee[t], 
     im\[Rho]eg'[t] == \[CapitalOmega] (1 - 2 r\[Rho]ee[t]), 
     r\[Rho]ee[0] == 0, im\[Rho]ee[0] == 0, r\[Rho]eg[0] == 0, im\[Rho]eg[0] == 0},
     {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t],r\[Rho]eg[t]}, {t, 0, 1}], 
   r\[Rho]ee \[Element] Reals && im\[Rho]ee \[Element] Reals && r\[Rho]eg \[Element] Reals && 
   im\[Rho]eg \[Element] Reals && \[CapitalOmega] \[Element] Reals && \[Gamma]eg \[Element] Reals && 
    0 <= r\[Rho] ee[t]^2 + im\[Rho] ee[t]^2 <= 1];
sols = {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t], r\[Rho]eg[t]} = {r\[Rho]ee[t], im\[Rho]ee[t],
    im\[Rho]eg[t], r\[Rho]eg[t]} /. s[[1]];
tab = Table[1, {i, 5}];
tab[[1]] = sols;
Plot[tab[[1]][[1]], {t, 0, 1}]

enter image description here

If you look at

tab[[1]][[1]]

you can see that it already has the [t] at the end of it, so adding another [t] inside your Plot is just confusing it.

POSTED BY: Bill Simpson
Posted 10 years ago

Thanks for the answer!

But still, my problem is the following: I want to put 'sols' into a table in the following way:

tab = Table[1, {i, 5}]]

tab[[1]] = sols;

And then do something like

Plot[tab[[1]][[1]][t],{t,0,1}]

And it doesn't work - do you know how to do it?

Thanks!

POSTED BY: shaked rozen
Posted 10 years ago

NDSolve, and most of the other "N" functions, demand that all variables except those that you are solving for have been previously assigned constant numeric values. And I made a few other changes.

\[CapitalOmega] = 1;
s = Refine[
        NDSolve[{
          r\[Rho]ee'[t] == 2 \[CapitalOmega] im\[Rho]eg[t],
          im\[Rho]ee'[t] == 0,
          r\[Rho]eg'[t] == 2 \[CapitalOmega] im\[Rho]ee[t],
          im\[Rho]eg'[t] == \[CapitalOmega] (1 - 2 r\[Rho]ee[t]),
          r\[Rho]ee[0] == 0, im\[Rho]ee[0] == 0, r\[Rho]eg[0] == 0, im\[Rho]eg[0] == 0},
          {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t], r\[Rho]eg[t]}, {t, 0, 1}], 
     r\[Rho]ee \[Element] Reals && im\[Rho]ee \[Element] Reals && r\[Rho]eg \[Element] Reals && 
     im\[Rho]eg \[Element] Reals && \[CapitalOmega] \[Element] Reals && \[Gamma]eg \[Element] Reals && 
    0 <= r\[Rho] ee[t]^2 + im\[Rho] ee[t]^2 <= 1];
sols = {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t], 
    r\[Rho]eg[t]} = {r\[Rho]ee[t], im\[Rho]ee[t], im\[Rho]eg[t], 
     r\[Rho]eg[t]} /. s[[1]];
Plot[sols, {t, 0, 1}]

enter image description here

r\[Rho]ee[t_] = sols[[1]];
Plot[r\[Rho]ee[t], {t, 0, 1}]

enter image description here

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard