How to get different color for different branches in a plot in MatLab, Mathematica and Desmos

2

I am trying to obtain slab-waveguide dispersion plot like this (dashed line): Dispersion I tried following code in 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

What I got: plot

Using Desmos:

enter image description here

Using 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}]

enter image description here

All the plots are in excellent agreement with form expected. However the original plot has a different color for each branch, how can I implement this in MatLab, Desmos or Mathematica ?

Kutsit

Posted 2019-12-10T09:57:31.563

Reputation: 123

Perhaps it would be better if you could also provide the actual equations you are plotting. I believe each curve is related to a characteristic value of the implicit equation (like f(x,y)=c), is it so? With the equations, it is easier to modify the code to plot each curve with a different color. – Thales – 2019-12-10T10:34:40.830

Answers

1

Mathematica

Update

Add legend, use darker yellow color.

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}

enter image description here

Original answer

I could not find a way to use ContourPlot options to color the lines of an implicit function plot. Here is a way (hack) to do it by post processing the plot expression.

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}

enter image description here

Rohit Namjoshi

Posted 2019-12-10T09:57:31.563

Reputation: 126

Can you also please tell how to add TE_num to each line as is done in original figure – Kutsit – 2019-12-11T16:55:09.273

1@Kutsit I updated the answer to add a legend. Not exactly the same as the original, but the easiest way to get something similar. – Rohit Namjoshi – 2019-12-11T22:33:23.850

Thanks a lot !!! – Kutsit – 2019-12-12T18:36:18.720