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

*To*: mathgroup at smc.vnet.net*Subject*: [mg91403] Re: NDSolve[] with nested If[] and Piecewise[] usage:*From*: Gopinath Venkatesan <gopinathv at ou.edu>*Date*: Wed, 20 Aug 2008 04:05:01 -0400 (EDT)

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 *)