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?
The slant line is the $\eta=2 \xi+1$.
I am a beginner in matlab. Kindly help me with the above drawing
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}
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