Message Boards Message Boards

0
|
3756 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to incorporate numerical analysis into Graphics?

Posted 8 years ago

Does anyone know how to incorporate numerical analysis into Graphics? I set up a numerical solution to a differential equation using NDSolve. The solution executes perfectly fine. However, when you call the solution in Graphics, an error message pops up saying that it doesn't recognize the number of the solution (i.e. the coordinates). Is this a feature of Graphics? Plot can recognize the solution and use it just fine.

Here is example script (simple harmonic oscillator for demonstrative purposes):

Eqn1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t_] = 
  D[f1[t], {t, 2}] + \[Omega]01^2*f1[t] == A1*Cos[\[Omega]1*t];

IC11[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t_] = 
  f1[0] == \[Alpha]1;

IC12[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, 
   t_] = (D[f1[t], {t, 1}] == \[Gamma]1) /. {  t -> 0  };

Sol1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t0_, 
  time_] := NDSolve[
  {
   Eqn1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t],
   IC11[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t],
   IC12[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t]
   },
  f1[t],
  {t, t0, time}
  ]

x1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t0_, time_] :=
  Replace[
  f1[t],
  Sol1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t0, time]
  ]

Evaluate[x1[1, 0, 1, 0, 0, 0, 10]]

Graphics[{

  Rectangle[  {  Evaluate[x1[1, 0, 1, 0, 0, 0, 10]] + 1/3 , 0  }, {  
    Evaluate[x1[1, 0, 1, 0, 0, 0, 10]] - 1/3, 2/3  } ]

  (* Graphics *)}, ImageSize -> Medium]
POSTED BY: Tim Kirkpatrick
3 Replies

You can save yourself ReplaceAll and [[1]] using the new NDSolveValue (introduced in version 9). You seem to overuse Evaluate, which has a very specialized purpose. If you are using x1 with the same parameters and many different values of t it may be better to store the NDSolveValue in memory as a pure function (without explicit t). Here is a revised coding:

x1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t0_, time_] :=
   x1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t0, time] = 
   NDSolveValue[{Eqn1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1,
       t], IC11[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t], 
     IC12[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t]}, 
    f1, {t, t0, time}];

With[{x = x1[1, 0, 1, 0, 0, 0, 10]}, 
 Graphics[{Rectangle[{x[1.0001] + 1/3, 0}, {x[1.001] - 1/3, 2/3}]
   (*Graphics*)}, ImageSize -> Medium]]
POSTED BY: Gianluca Gorni

Hi, thank you for the tips! It turns out that x1 was still a list. There should be a [[1]] at the end of the expression for x1; this removes the list. In addition, there should be a t_ variable defined with the expressions for Sol1 and x1; this creates the interpolating function for x1 as a function of t. Lastly, the interpolating function can be called in graphics using ( Evaluate[ x1[t] /. t-> value ), where every t-value in the interpolating expression is replaced with a numerical value after the interpolating function is evaluated.

POSTED BY: Tim Kirkpatrick

There is a variable t in Sol1 and x1 that does not get a numeric value inside the Graphics. Moreover, the way you define them, Sol1 and x1 are lists. You may want to extract the (only) element of the list to work with. You should use ReplaceAll instead of Replace:

Eqn1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t_] = 
  D[f1[t], {t, 2}] + \[Omega]01^2*f1[t] == A1*Cos[\[Omega]1*t];

IC11[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t_] = 
  f1[0] == \[Alpha]1;

IC12[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, 
   t_] = (D[f1[t], {t, 1}] == \[Gamma]1) /. {t -> 0};

Sol1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t0_, 
  time_] := 
 First@NDSolve[{Eqn1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, 
     t], IC11[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t], 
    IC12[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t]}, 
   f1, {t, t0, time}]

x1[\[Alpha]1_, \[Gamma]1_, \[Omega]01_, \[Omega]1_, A1_, t0_, time_, 
  t_] := ReplaceAll[f1[t], 
  Sol1[\[Alpha]1, \[Gamma]1, \[Omega]01, \[Omega]1, A1, t0, time]]

Graphics[{Rectangle[{x1[1, 0, 1, 0, 0, 0, 10, 1.0001] + 1/3, 
    0}, {x1[1, 0, 1, 0, 0, 0, 10, 1.001] - 1/3, 2/3}]
  (*Graphics*)}, ImageSize -> Medium]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract