Re: Plot a function on Time Scales
- To: mathgroup at smc.vnet.net
- Subject: [mg63593] Re: Plot a function on Time Scales
- From: "Borut Levart" <BoLe79 at gmail.com>
- Date: Fri, 6 Jan 2006 05:24:41 -0500 (EST)
- References: <dpiupr$ntk$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Unal, There are more ways you can do that, it really depends on your preference for the output. To define a continuous function, consider the build-in Boole[] function and define your function like this: intervalList = {{-1, 1}, {12, 20}}; pointList = {2, 5, 8, 11}; f[x_]:=x^2 * Boole[Or @@ (IntervalMemberQ[Interval[#], x] & /@ intervalList) || MemberQ[pointList, x]] It's continuous: with the desired value inside intervals and at specified points and 0 elsewhere. But I don't like the Plot output with the vertical lines (however those can be erased) and there are apparently no points drawn either. So I would in your place draw the plot of the function (and not show it: $DisplayFunction = Identity), and then keep only the points that lie on the intervals (Cases). Then I would split the points according to a test that two neighboring points lie on the same interval (Split), and draw the result with Show[Graphics]. Hope that helps: ins[T_] := Cases[T, _?VectorQ]; pts[T_] := Cases[T, _?NumericQ]; g[f_, T_] := Block[{$DisplayFunction = Identity}, Plot[f[x], {x, Min[T], Max[T]}]]; bothIntervalMemberQ[in_?(Head[#] === Interval &), x1_, x2_] := And @@ (IntervalMemberQ[in, #] & /@ {x1, x2}) ptsFromIntervals[intervalList_] := Line[pts_] :> Cases[pts, _?(Or @@ Map[Function[{in}, IntervalMemberQ[Interval[in], #[[1]]]], intervalList] &)] splitIntervals[points_, intervalList_] := Split[points, (Or @@ Map[Function[{in}, bothIntervalMemberQ[Interval[in], #1[[1]], #2[[1]]]], intervalList]) &] Your example: f = #^2 &; T = {{-1, 1}, 2, 5, 8, 11, {12, 20}}; Show[ Graphics[{ Line /@ splitIntervals[g[f, T][[1, 1, 1]] /. ptsFromIntervals[ins[T]], ins[T]], PointSize[.02], Point /@ Transpose[{pts[T], f /@ pts[T]}] }], AspectRatio -> Automatic, Axes -> True] My example: f = Sin; T = {{-p, 1}, Sequence @@ Range[1.5, 6, .5], {6.5, 4 p}}; Show[ Graphics[{ Line /@ splitIntervals[g[f, T][[1, 1, 1]] /. ptsFromIntervals[ins[T]], ins[T]], PointSize[.01], Point /@ Transpose[{pts[T], f /@ pts[T]}] }], AspectRatio -> Automatic, Frame -> True] Greeting, Borut Levart, Slovenia