MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: NDSolve[] with nested If[] and Piecewise[] usage:

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91427] Re: [mg91403] Re: NDSolve[] with nested If[] and Piecewise[] usage:
  • From: DrMajorBob <drmajorbob at att.net>
  • Date: Thu, 21 Aug 2008 04:16:04 -0400 (EDT)
  • References: <25682308.1219270716763.JavaMail.root@m08>
  • Reply-to: drmajorbob at longhorns.com

I'd use one of these:

sep = 1;
len = 3;
wdef1[t_] := y[t];
wdef2[t_] := y[t];
val1 = 50;
val2 = 20;
xv = 1/2;
yv = 1/5;
Clear[t]
funifcase2b[t_] =
  If[0 <= t < sep/len, val1 + val1 xv yv^2 (wdef1[t]) Sin[t],
      If[sep/len <= t <= (2 sep)/len,
       val1 + val2 + val1 xv yv^2 (wdef1[t]) Sin[t] +
        val2 yv xv^2 (wdef2[t]) Sin[t - sep/len],
       If[(2 sep)/len < t <= 1,
        val2 + val2 yv xv^2 (wdef2[t]) Sin[t - sep/len]]]] /.
     If -> if /. if -> If // PiecewiseExpand

\[Piecewise] {
   {20 - Sin[1/3 - t] y[t], 2/3 < t <= 1},
   {50 + Sin[t] y[t], 0 <= t < 1/3},
   {70 - Sin[1/3 - t] y[t] + Sin[t] y[t], 1/3 <= t <= 2/3}
  }

or

funifcase3b[t_] =
  Piecewise[{{val1 + val1 xv yv^2 (wdef1[t]) Sin[t],
     0 <= t < sep/len}, {val1 + val2 + val1 xv yv^2 (wdef1[t]) Sin[t] +
       val2 yv xv^2 (wdef2[t]) Sin[t - sep/len],
     sep/len <= t <= (2 sep)/len}, {val2 +
      val2 yv xv^2 (wdef2[t]) Sin[t - sep/len], (2 sep)/len < t <=
      1}}]

\[Piecewise] {
   {50 + Sin[t] y[t], 0 <= t < 1/3},
   {70 - Sin[1/3 - t] y[t] + Sin[t] y[t], 1/3 <= t <= 2/3},
   {20 - Sin[1/3 - t] y[t], 2/3 < t <= 1}
  }

Evaluate doesn't evaluate everything, because If has the attribute  
HoldRest; I defeated that by replacing If with if, then replacing if with  
If. Then I created a Piecewise equivalent via PiecewiseExpand.

Piecewise didn't work as you expected (I think) because you used  
SetDelayed (:=) where you needed Set (=).

The two definitions above are equivalent functions, but one could be  
faster than the other, depending on what intervals x is more likely to  
inhabit. You want the most likely interval first, least likely last, etc.  
That's probably a very slight improvement at best, however.

Bobby

On Wed, 20 Aug 2008 03:05:01 -0500, Gopinath Venkatesan <gopinathv at ou.edu>  
wrote:

> Hello Jean,
>
> Thank you for replying me.
>
> I use v1[t], v2[t], v3[t], phi[t](symbol phi) and vi[i][j] as unknown  
> functions (you can say variables, thats why I introduced 5 equations and  
> 10 initial conditions).
>
> I sincerely believe there should not be any problem as such in doing so.  
> The last time I posted, Oliver and DrMajorBob, both pointed out that the  
> parts of the expressions inside the If[] remain unevaluated. For that I  
> used Evaluate[] for each of the parts/expressions inside If[] and it  
> worked - produced same result as the Piecewise[] did.
>
> Now for this case (which is very similar), the Piecewise[] itself did  
> not work. Thats why I am clueless.
>
> Just for your reference, please see the code at the bottom: After  
> putting Evaluate[] inside for one of the problem, I was able to solve  
> it. But this is not the problem I am looking to solve - it is already  
> solved from suggestions given by Oliver and DrMajorBob. Please see my  
> previous post that starts with "Sometime back I posted this question on  
> the ..." posted on August 19. You can browse the bottom for the code  
> that I am looking for your help and suggestions to solve. Thank you.
>
> (* Working sample code starts here *)
>
> sep = 1;
> len = 3;
> wdef1[t_] := y[t];
> wdef2[t_] := y[t];
> val1 = 50;
> val2 = 20;
> xv = 1/2;
> yv = 1/5;
> funifcase2[t_] :=
>   Evaluate[If[Evaluate[0 <= t < sep/len],
>     Evaluate[val1 + val1 xv yv^2 (wdef1[t]) Sin[t]],
>     Evaluate[
>      If[Evaluate[sep/len <= t <= (2 sep)/len],
>       Evaluate[
>        val1 + val2 + val1 xv yv^2 (wdef1[t]) Sin[t] +
>         val2 yv xv^2 (wdef2[t]) Sin[t - sep/len]],
>       Evaluate[
>        If[Evaluate[(2 sep)/len < t <= 1],
>         Evaluate[
>          val2 + val2 yv xv^2 (wdef2[t]) Sin[t - sep/len]]]]]]]];
>
> (* previous definition of funifcase2[t] -- not working *)
>
> (* funifcase2[t_] :=
>   If[0 <= t < sep/len, val1 + val1 xv yv^2 (wdef1[t]) Sin[t],
>    If[sep/len <= t <= (2 sep)/len,
>     val1 + val2 + val1 xv yv^2 (wdef1[t]) Sin[t] +
>      val2 yv xv^2 (wdef2[t]) Sin[t - sep/len],
>     If[(2 sep)/len < t <= 1,
>      val2 + val2 yv xv^2 (wdef2[t]) Sin[t - sep/len]]]]; *)
>
> funifcase3[t_] :=
>   Piecewise[{{val1 + val1 xv yv^2 (wdef1[t]) Sin[t],
>      0 <= t < sep/len}, {val1 + val2 +
>       val1 xv yv^2 (wdef1[t]) Sin[t] +
>       val2 yv xv^2 (wdef2[t]) Sin[t - sep/len],
>      sep/len <= t <= (2 sep)/len}, {val2 +
>       val2 yv xv^2 (wdef2[t]) Sin[t - sep/len], (2 sep)/len < t <=
>       1}}];
> Chop[Table[funifcase2[t], {t, 0, 1, 0.1}]]
> Print["compare values, just to check the correctness of equation \
> above"];
> Chop[Table[funifcase3[t], {t, 0, 1, 0.1}]]
> Print["The definition funifcase2[t] is ", funifcase2[t]];
> Print["The definition funifcase3[t] is ", funifcase3[t]];
> ?funifcase2
> ?funifcase3
>
> solifcase2 =
>   NDSolve[{y''[t] + y'[t] + y[t] - funifcase2[t] == 0, y[0] == 0,
>     y'[0] == 1}, y, {t, 0, 1}];
> Plot[Evaluate[{y[t], y'[t]} /. solifcase2], {t, 0, 1},
>  PlotStyle -> Automatic]
> Print["Proceeding to solve the above equation with Piecewise \
> definition"];
> solifcase3 =
>   NDSolve[{y''[t] + y'[t] + y[t] - funifcase3[t] == 0, y[0] == 0,
>     y'[0] == 1}, y, {t, 0, 1}];
> Plot[Evaluate[{y[t], y'[t]} /. solifcase3], {t, 0, 1},
>  PlotStyle -> {Black, {Red, Dashed}}]
>
> (* Code ends here *)
>
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: Re: DiffMaps package
  • Next by Date: Hypergeometric1F1 polynomial
  • Previous by thread: Re: NDSolve[] with nested If[] and Piecewise[] usage:
  • Next by thread: Graphing Difference Equation