![如何在 MatLab、Mathematica 和 Desmos 中为图中的不同分支获取不同的颜色](https://linux22.com/image/1611504/%E5%A6%82%E4%BD%95%E5%9C%A8%20MatLab%E3%80%81Mathematica%20%E5%92%8C%20Desmos%20%E4%B8%AD%E4%B8%BA%E5%9B%BE%E4%B8%AD%E7%9A%84%E4%B8%8D%E5%90%8C%E5%88%86%E6%94%AF%E8%8E%B7%E5%8F%96%E4%B8%8D%E5%90%8C%E7%9A%84%E9%A2%9C%E8%89%B2.png)
我正在尝试获得像这样的平板波导色散图(虚线):
我在 Matlab 中尝试了以下代码:
function main
fimplicit (@(x,y)f(x,y),[0 10])
end
function fun = f(x,y)
nc=1.45; %cladding
nf=1.5;
ns=1.4; %substrate
h=5; %width of waveguide
beta=sqrt(x^2*nf^2-y.^2);
gammas=sqrt(beta.^2-x^2*ns^2);
gammac=sqrt(beta.^2-x^2*nc^2);
z=sin(h*y);
%TE mode
fun=z-cos(h*y)*(gammac+gammas)./(y-gammas.*gammac./y);
end
使用 Desmos:
使用 Mathematica:
nc = 1.45;
nf = 1.5;
ns = 1.4;
h = 5;
ContourPlot[
Sin[h y]*(y^2 - (Sqrt[x^2*(nf^2 - nc^2) - y^2]*
Sqrt[x^2*(nf^2 - ns^2) - y^2])) ==
Cos[h y]*(Sqrt[x^2*(nf^2 - nc^2) - y^2] +
Sqrt[x^2*(nf^2 - ns^2) - y^2])*y, {x, 0, 10}, {y, 0.1, 10}]
所有图表都与预期形式高度一致。 但是原始图中的每个分支都有不同的颜色,我如何在 MatLab、Desmos 或 Mathematica 中实现这一点?
答案1
数学
更新
添加图例,使用深黄色。
colors = {Blue, Green, Red, Cyan, Magenta, RGBColor["#cdcd41"]};
labels = MapThread[
ToString[Subscript[Style["TE", Bold, 16, #2],
Style[ToString@#1, Bold, 12, #2]], StandardForm] &, {Range[0, 5], colors}];
legend = LineLegend[colors, labels, LegendLayout -> "ReversedColumn", LegendMarkerSize -> 20];
plot = ContourPlot[
Sin[h y]*(y^2 - (Sqrt[x^2*(nf^2 - nc^2) - y^2]*
Sqrt[x^2*(nf^2 - ns^2) - y^2])) ==
Cos[h y]*(Sqrt[x^2*(nf^2 - nc^2) - y^2] +
Sqrt[x^2*(nf^2 - ns^2) - y^2])*y, {x, 0, 10}, {y, 0, 4}, PlotLegends -> legend];
coloredLines = Riffle[colors, Cases[plot, _Line, Infinity]];
plot /. {a___, Repeated[_Line, {6}], c___} :> {a, Sequence @@ coloredLines, c}
原始答案
我找不到使用ContourPlot
选项来为隐函数图的线条着色的方法。这里有一种方法(黑客)通过后处理绘图表达式来实现。
plot = ContourPlot[
Sin[h y]*(y^2 - (Sqrt[x^2*(nf^2 - nc^2) - y^2]*
Sqrt[x^2*(nf^2 - ns^2) - y^2])) ==
Cos[h y]*(Sqrt[x^2*(nf^2 - nc^2) - y^2] +
Sqrt[x^2*(nf^2 - ns^2) - y^2])*y, {x, 0, 10}, {y, 0, 4}];
coloredLines = Riffle[{Blue, Green, Red, Cyan, Magenta, Yellow}, Cases[plot, _Line, Infinity]];
plot /. {a___, Repeated[_Line, {6}], c___} :> {a, Sequence @@ coloredLines, c}