不好意思,
請問我想要寫一個適用於各種範圍的週期函數 跟畫圖
因為是新手,剛剛開始摸索,一直寫不出來
能幫我看一下下面兩種寫法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}]
感謝~