Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I wrote some code in Mathematica to calculate and plot, in tw0 different ways, a function depending of two variables. But the plots are empty even if it takes a few minutes to evaluate the plot command. But the weirdest thing is that when I change an option of the plot when clicking on "plot style" for example, the plot appears briefly and then disappears again! Here is the code:

Vmax = Quantity[1, "Volt"]
L0 = Quantity[0.5 , "MicroHenry"]
C0 = Quantity[2, "PicoFarad"]
Zl = Quantity[100, "Ohm"]
tw = Quantity[0.5, "NanoSeconds"]
l = Quantity[1.2, "Meters"]
Z0 = UnitSimplify[Sqrt[L0/C0]]
Γ = (QuantityMagnitude[Zl] - QuantityMagnitude[Z0])/
      (QuantityMagnitude[Zl] + QuantityMagnitude[Z0])
γ = UnitSimplify[Sqrt[C0*L0]]
U[t_] := QuantityMagnitude[
   Vmax]*(UnitStep[t] - UnitStep[t - QuantityMagnitude[tw]*10^-9])
Us[s_] := FullSimplify[LaplaceTransform[U[t], t, s]]
Vs[z_, s_] := 
  FullSimplify[
    Us[s]*(Exp[-s*QuantityMagnitude[γ]*z] + 
     QuantityMagnitude[Γ]*
     Exp[s*QuantityMagnitude[γ]*(z - 2*QuantityMagnitude[l])])/
       (1 + QuantityMagnitude[Γ]*Exp[-2*s*QuantityMagnitude[γ]*QuantityMagnitude[l]])]
V2[z_, t_] := 
  U[t - QuantityMagnitude[γ]*z] + 
    Sum[(-1)^(n - 1)*(QuantityMagnitude[Γ]^n*
        U[t + QuantityMagnitude[γ]*
           (z - 2*n*QuantityMagnitude[l])] - 
       QuantityMagnitude[Γ]^n*
        U[t - QuantityMagnitude[γ]*
           (z + 2*n*QuantityMagnitude[l])]), 
     {n, 1, Infinity}]
Plot3D[{V2[z, t]}, {t, 0, 5*10^-9}, {z, 0, 1.2}]
V1[z_, t_] := InverseLaplaceTransform[Vs[z, s], s, t]
Plot3D[{V1[z, t]}, {t, 0, 5*10^-9}, {z, 0, 1.2}]
share|improve this question
    
No answer, but a few observations: 1) Why starting with all those Quantities when you're converting them back to unit-less numbers a few steps later? Makes your code hard to read and only takes time. 2)FullSimplify in a delayed assignment causes the costly simplification to be run for every call. 3) The variables in the InverseLaplaceTransform are the parameters of the function call, meaning they are not variables anymore but only values and cannot be used in a symbolic calculation. You should use dummy variables that are replaced with the function parameters afterwards ... –  Sjoerd C. de Vries Apr 25 at 20:10
    
... 4) Why the use of the curly brackets in Plot3D? 5) The infinite sum may be quite costly and is recalculated every time because of the :=. Perhaps an NSum works better here. –  Sjoerd C. de Vries Apr 25 at 20:12
    
I also assume QuantityMagnitude[tw]*10^-9 to be incorrect as tw is already in ns. –  Sjoerd C. de Vries Apr 25 at 20:24
    
This question appears to be off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. –  m_goldberg Apr 26 at 5:26
    
@m_goldberg I do not agree here. One reason the plot doesn't appear is a bug in mathematica's Plot. In my answer I show a workaround. –  Sjoerd C. de Vries Apr 26 at 6:24

1 Answer 1

up vote 2 down vote accepted

I can get you at least your first plot and explain why you don't get the second. First, let's get rid of all the units mess:

Vmax = 1;
L0 = 0.5 10^-6;
C0 = 2 10^-12;
Zl = 100;
tw = 0.5 10^-9;
l = 1.2;
Z0 = Sqrt[L0/C0];
Γ = (Zl - Z0)/(Zl + Z0);
γ = Sqrt[C0*L0];
U[t_] := Vmax*(UnitStep[t] - UnitStep[t - tw])

Note that I removed the 10^-9 term in the last line. I believe that's probably an error.

Now the first Laplace transform:

Us[s_] = LaplaceTransform[U[t], t, s1] /. s1 -> s

1/s - E^(-5.*10^-10 s)/s

You can see I introduced a dummy variable here, so that you can use a numerical value for s here. After the transform, the dummy variable is replaced by your numeric parameter value. I also used Set(=) here instead of SetDelayed assuming that you are not going to change the U function it depended on. This will save some time later on.

Vs[z_, s_] = Us[s]*(Exp[-s*γ*z] + Γ*Exp[s*γ*(z - 2*l)])/(1 + Γ*Exp[-2*s*γ*l]);

V2[z_, t_] = U[t - γ*z] + 
             Sum[(-1)^(n - 1)*(Γ^n*U[t + γ*(z - 2*n*l)] - Γ^n*U[t - γ*(z + 2*n*l)]), 
                 {n, 1, Infinity}];

And now, for the first plot

Plot3D[V2[z, t*10^-9], {t, 0, 5}, {z, 0, 1.2}, ExclusionsStyle -> LightRed]

Note that I changed the location of the 10^-9 term from the t range declaration to the input of t as parameter. This makes the range of the parameters t and z in the plot more comparable. Plot functions often don't like variable ranges whose values differ by orders of magnitude. The change actually enables Plot3D to do its job :

Mathematica graphics

Now, for the second plot we have a problem:

V1[z_, t_] = InverseLaplaceTransform[Vs[z1, s], s, t1] /. {z1 -> z, t1 -> t}

InverseLaplaceTransform[((-0.6666666667 E^( 1.*10^-9 s (-2.4 + z)) + E^(-1.*10^-9 s z)) (1/s - E^(-5.*10^-10 s)/s))/(1 - 0.6666666667 E^(-2.4*10^-9 s)), s, t]

Mathematica does not know how to do this transform and returns it unevaluated. Of course, you can't get numerical values from it. It follows that you can't plot it either.

share|improve this answer
    
Thanks for the help! The problem is solved now! the command ExclusionsStyle -> LightRed did the trick. Without it, the plot is empty and with it, it works just fine. –  Yann Rosema Apr 28 at 15:44
    
Oh and by the way, if mathematica can't get numerical values for V1 why doesn't it give an error? –  Yann Rosema Apr 28 at 15:57
    
@yann Generally, Mathematica keeps on applying rules to objects as long as it has applicable ones available and returns the end result when none are left. Returning the original input may sometimes be the consequence of this and is generally not seen as an error. In fact it is this what enables many of Mathematica's symbolic capabilities. For more info, see this –  Sjoerd C. de Vries Apr 28 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.