0

How do I plot the following graph in Matlab code with same dot lines and continuous line as in the graph? What is the code?

enter image description here

The slant line is the $\eta=2 \xi+1$.

I am a beginner in matlab. Kindly help me with the above drawing

MAS
  • 10,638

2 Answers2

1

Here's a way to do it:

\begin{align} & \mathsf {xi=[-2:0.1:5];}\\ &\mathsf{eta1=2*xi+1;}\\ &\mathsf{cnd=(xi<=2); // \text{ Indicator function that's 1 iff xi <= 2, 0 otherwise}}\\ &\mathsf{eta2=cnd.*(2*xi+1) + (1-cnd).*5;}\\ &\mathsf{figure(1);clf;hold\, on;}\\ &\mathsf{plot(xi,eta1, '--');}\\ &\mathsf{plot(xi,eta2, '-');} \end{align}

enter image description here

Stefan Lafon
  • 12,256
  • 11
  • 29
1

One possible solution, that also shows my way of working with Matlab.

First, we define $\xi$ varying from $-1$ to $2$ and then compute $\eta_1(\xi)\equiv 2$ and $\eta_2(\xi) = 2\xi+1$.

xi=-1:0.1:2;
eta1 = 2*ones(size(xi));
eta2 = 2*xi + 1;

Now we have to find the intersection. We can do it based on the data, as the first point $\xi$ where $\eta_2(\xi)\ge \eta_1(\xi)$:

intersection_index = find(eta1 <= eta2, 1);
intersection_xi = xi(intersection_index);

Or, if we know that the intersection point is $\xi=0.5$, we can find its index as the index of the last point in the $\xi$-array such that $\xi \le 0.5$:

intersection_xi = 0.5;
intersection_index = find(xi<=intersection_xi, 1, 'last');

Now we can divide data into two parts and plot them:

f=figure;
ax=axes(f);
hold on;

pl_eta1_part1 = plot(ax, xi(1:intersection_index), eta1(1:intersection_index),'LineStyle','-');
pl_eta1_part2 = plot(ax, xi(intersection_index:end), eta1(intersection_index:end),'LineStyle','--');

set(pl_eta1_part2,'Color',get(pl_eta1_part1,'Color'));

pl_eta2_part1 = plot(ax, xi(1:intersection_index), eta2(1:intersection_index),'LineStyle','-');
pl_eta2_part2 = plot(ax, xi(intersection_index:end), eta2(intersection_index:end),'LineStyle','--');

set(pl_eta2_part2,'Color',get(pl_eta2_part1,'Color'));

Since Matlab normally uses a new color for a new plot, we manually set colors of second parts equal to the colors of first parts with

set(pl_eta1_part2,'Color',get(pl_eta1_part1,'Color'));

Another option is to provide the value of Color as an argument of the plot command, similar to the LineStyle option.

The resulting figure is

enter image description here

Arastas
  • 2,329
  • what is the figure in your program? Can you please show the graph – MAS Jul 12 '19 at 12:23
  • @M.A.SARKAR I have updated the answer. – Arastas Jul 12 '19 at 17:05
  • nice but on the blue line, the dot line part should be for $x<1/2$ while continuous line part for $x>1/2$. Your figure just opposite in that case. Any how I am a begginer and your code is more general. Just help me, how do I add another line $y=\frac{1}{2}x+2$ in the above figure additionally. Please help me – MAS Jul 13 '19 at 02:48
  • your prgram code "ax=figure(f)" says undefined – MAS Jul 13 '19 at 19:45
  • @M.A.SARKARThere is no ax=figure(f) in my code. – Arastas Jul 14 '19 at 13:32
  • @M.A.SARKAR Just add plot(xi,0.5*xi+2); – Arastas Jul 14 '19 at 13:33