You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mathematica 10.2分段函数(宝塔函数)绘图效果差的解决咨询

Fixing Smooth Plotting for Custom Piecewise "Pagoda" Function in Mathematica 10.2

Hey Felix, I’ve dealt with exactly this kind of jagged Plot3D output for piecewise functions before—let’s break down why your initial approach isn’t working and fix it with targeted adjustments.

Why Your Initial Code Has Issues

The root problem is that Mathematica’s default adaptive sampling doesn’t automatically prioritize the critical boundary points of your piecewise function (x=±1, y=±1). When you tried low MaxRecursion values like 2 or 3, you were actually limiting how much Mathematica could refine the sampling at those sharp edges, making the jaggedness worse instead of better.

Solution 1: Explicitly Define Boundaries + Optimize Sampling

Tell Mathematica exactly where the function changes behavior with the Exclusions parameter, then pair it with enough initial sampling points and reasonable recursion to smooth out edges:

f[x_] := Piecewise[{{0, x <= -1}, {-Abs[x] + 1, -1 < x < 1}, {0, x >= 1}}]
Plot3D[5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5},
 Exclusions -> {x == -1, x == 1, y == -1, y == 1}, 
 PlotPoints -> 50,                                  
 MaxRecursion -> 4,                                 
 Mesh -> None,                                      
 PlotStyle -> Directive[Opacity[0.8], LightBlue]    
]

Solution 2: Rewrite the Function with UnitStep

Replacing Piecewise with UnitStep can help Mathematica’s sampling algorithm recognize boundaries more naturally, since UnitStep is a built-in function with well-defined behavior:

f[x_] := (-Abs[x] + 1)*UnitStep[x + 1]*UnitStep[1 - x]
Plot3D[5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5},
 PlotPoints -> 50,
 MaxRecursion -> 4,
 Mesh -> None
]

Solution 3: Smooth the Sharp Edges (Optional)

If you’d prefer slightly rounded edges instead of sharp corners (which can eliminate jaggedness entirely), replace Abs[x] with a smooth approximation like Sqrt[x^2 + ε] (use a small ε value like 0.01):

ε = 0.01;
f[x_] := Piecewise[{{0, x <= -1}, {-Sqrt[x^2 + ε] + 1, -1 < x < 1}, {0, x >= 1}}]
Plot3D[5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5},
 PlotPoints -> 40,
 MaxRecursion -> 3,
 Mesh -> None
]

All these approaches should give you a clean, smooth plot of your pagoda function without the jagged artifacts you saw earlier.

内容的提问来源于stack exchange,提问作者roflmaostc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:51