不好意思,
请问我想要写一个适用于各种范围的周期函数 跟画图
因为是新手,刚刚开始摸索,一直写不出来
能帮我看一下下面两种写法Bug在哪里吗?
还是有其他更好的写法吗?
g 为周期函数 g[x], x 为变量, {a, b} 为周期, {c, d} 为画图的范围
ㄧ、
PeriodicExtension[g_, x_, {a_, b_}] :=If[a < x < b, g[x],If[x < a, PeriodicExt
ension[g, x + (b - a), {a, b}],
PeriodicExtension[g, x - (b - a), {a, b}]]]
f[x_] = Piecewise[{{x, 1 < x < 2}, {-x, 2 < x < 4}}]
Plot[PeriodicExtension[f, x, {1, 4}], {x, -5, 10} ]
f2[x_] := x^2 /; -1 < x < 1
Plot[f2[x], {x, -1, 1}]
Plot[PeriodicExtension[f2, x, {-1, 1}], {x, -5, 5} ]
Clear[periodicPlot];
periodicPlot[g_, x_, {a_, b_}, {c_, d_}] :=
Module[{e = b - a, gi, pf, z},
pf[gi_, z_] :=
If[a < z < b, gi[z], If[z < a, pf[gi, z + e], pf[gi, z - e]]];
Plot[pf[g, x], {x, c, d}]]
periodicPlot[f2, x, {-1, 1}, {-5, 5}]
二、
PeriodicExtension[g_, x_, {a_, b_}, xv_] :=If[a < x < b, g /. x -> xv,
If[x < a, PeriodicExtension[g, x + (b - a), {a, b}],
PeriodicExtension[g, x - (b - a), {a, b}]]]
Clear[PeriodicExtension];
PeriodicExtension[g_, x_, {a_, b_}, xv_] :=
If[a < xv < b, g /. x -> xv,
If[xv < a, PeriodicExtension[g, x, {a, b}, xv + (b - a)],
PeriodicExtension[g, x, {a, b}, xv - (b - a)]]];
?PeriodicExtension
f[x_] = Piecewise[{{x, 1 < x < 2}, {-x, 2 < x < 4}}]
Plot[PeriodicExtension[f[x], x, {1, 4}, y], {y, -5, 10} ]
Plot[PeriodicExtension[Sqrt[x], x, {1, 4}, y], {y, -5, 10} ]
f2[x_] := x^2 /; -1 < x < 1
Plot[f2[x], {x, -1, 1}]
Plot[PeriodicExtension[f2[x], x, {-1, 1}, y], {y, -5, 5} ]
Plot[PeriodicExtension[ x^2, x, {-1, 1}, y], {y, -5, 5} ]
Clear[periodicPlot];
periodicPlot[g_, x_, {a_, b_}, {c_, d_}] :=
Module[{e = b - a, gi, pf, z, xv},
pf[gi_, z_] :=
If[a < z < b, gi /. x -> z ,
If[z < a, pf[gi, z + e], pf[gi, z - e]]];
Plot[pf[g, xv], {xv, c, d}]]
periodicPlot[f2[x], x, {-1, 1}, {-5, 5}]
periodicPlot[x^2, x, {-1, 1}, {-5, 5}]
感谢~