修改Mathematica代码实现指定区间的函数曲线分段绘制
修改后的Mathematica代码
eqn[d_Integer?NonNegative, r_, B_, A_, nmax_Integer?Positive] := Sum[Exp[-n*\[Lambda]]*(1 - r)^n Exp[n*k^2 - k^2*A (HarmonicNumber[n + d] - HarmonicNumber[d]) + B*k*HarmonicNumber[n]], {n, 1, nmax}] == -1 + Exp[\[Lambda]]/r; AValues = {2}; BValues = {1}; Show[ Block[{$MaxExtraPrecision = 500}, ContourPlot[ Evaluate@Table[eqn[0, 0.25, B, A, 100], {A, AValues}, {B, BValues}], {k, -2, 2}, {\[Lambda], -0.5, 3.5}, FrameLabel -> (Style[#, 14] & /@ {k, "\!\(\*SubscriptBox[\(\[Lambda]\), \(r\)]\)(k)"}), RotateLabel -> False, PlotPoints -> 100, MaxRecursion -> 4, AspectRatio -> 1/GoldenRatio, PlotLegends -> Automatic, GridLines -> {{-0.5, 1.0}, {0}}, GridLinesStyle -> Directive[Gray, AbsoluteThickness[1], Dashed], (* 限制蓝色曲线仅在k=-0.5到1.0之间显示 *) RegionFunction -> Function[{k, λ}, -0.5 <= k <= 1.0] ] ], (* 黑色曲线拆分为两段,仅显示虚线外区域 *) Plot[k^2 + Log[0.75], {k, -2, -0.5}, PlotStyle -> {Black}], Plot[k^2 + Log[0.75], {k, 1.0, 2}, PlotStyle -> {Black}], PlotLegends -> Placed[ {"\!\(\*SubscriptBox[\(\[Lambda]\), \(0\)]\)(k)+ln(1-r)"}, {.5, .7} ] ]
修改说明
- 给
ContourPlot添加RegionFunction参数,明确指定仅在-0.5 <= k <= 1.0范围内绘制蓝色曲线; - 将原本单段的黑色曲线
Plot拆分为两个区间:k∈[-2,-0.5]和k∈[1.0,2],确保只在两条虚线外侧显示; - 保留原代码的图例、网格线、格式等其他设置,保证整体视觉风格一致。
内容的提问来源于stack exchange,提问作者caren
相关产品推荐
相关产品推荐

