What is the mathlab code for this figure?
57 views (last 30 days)
Show older comments
1 Comment
Divyajyoti Nayak
on 22 Dec 2024 at 19:19
Hi @Souhad, are you asking for code on how to make figures that look like the image or the logic for how to plot the curve shown in the figure? Either ways, it would be better if you could show what you have tried already.
Answers (2)
John D'Errico
on 22 Dec 2024 at 19:24
Moved: John D'Errico
on 22 Dec 2024 at 19:24
Why not try it? You might learn something.
Break the problem down into smaller problems, each of which you can solve yourself.
Start with plot. plot can plot a line or a curve, drawn thrugh a set of points. Or you can use fplot, if the curve is defined by some given function.
Next, those horizontal and vertical lines can be drawn, again by plot. But if you want to see lines terminated by arrowheads, the function quiver can do that.
You can add specific text annotation to a figure using the function text. And text can handle subscripts too.
And any of these plotting tools will allow you to control the color of a line easily.
Your figure has labels on the axes. In MATLAB, these will be controlled by the use of graphics handles.
So why not make an effort?
Start by learning to plot a simple curve.
help fplot
Next, try adding one horizontal or vertical line.
help quiver
Eat a programming elephant one byte at a time. (Well, this is not really that large of a problem, but at first, it will seem that way.) The only way to learn is by making an effort though.
0 Comments
Star Strider
on 22 Dec 2024 at 20:34
It would help to have the function for .
x = linspace(0, 9.5).';
y = x;
g = @(x) 10*(1 - exp(-0.3*x)); % Arbitrary ‘g(x)’
p = [2.5; 5.6; 6.35];
Lvg = (x >= 2) & (x <= 8); % Defines ‘g(x)’ Plot Range
yx = @(xv) interp1(x, y, xv);
figure
plot(x(Lvg), g(x(Lvg))) % Plot Curve Segment
hold on
plot(x, y, '-k')
plot(p(3), g(p(3)), '.k', MarkerSize=12)
plot(p(3), yx(p(3)), '.k', MarkerSize=12)
plot(p(2), g(p(2)), '.k', MarkerSize=12)
hold off
Ax = gca;
Ax.XTickLabel = []; % Turn Off Tick Labels
Ax.YTickLabel = []; % Turn Off Tick Labels
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim; % Required
yl = ylim; % Required
pos = gca().Position; % Required
annotation('textarrow', xapf([0 yx(g(p(3)))],pos,xl), yapf([g(p(3)) g(p(3))],pos,yl), String='$p_3=g(p_2)\ $', Interpreter='LaTeX')
annotation('textarrow', xapf([0 0]+p(3),pos,xl), yapf([0 g(p(3))],pos,yl), String='$p_2$', Interpreter='LaTeX')
annotation('textarrow', xapf([0 yx(g(p(2)))],pos,xl), yapf([g(p(2)) g(p(2))],pos,yl), String='$p_2=g(p_1)\ $', Interpreter='LaTeX')
annotation('textarrow', xapf([0 0]+p(2),pos,xl), yapf([0 g(p(2))],pos,yl), String='$p_1$', Interpreter='LaTeX')
text(p(3), g(p(3))*1.01, '$(p_2,p_3)$', Interpreter='LaTeX', Vert='bottom', Horiz='center')
text(x(end), y(end), '$y=x$', Interpreter='LaTeX', Horiz='center', Vert='bottom')
This is an iillustration of how to use them, along with my normalisation functions ‘xapf’ and ‘yapf’. (They require ‘xl’, ‘yl’ and ‘pos’ so copy all of those.) They take (x,y) coordinates and return normalised coordinates to use with the annotation functions.
I leave the rest to you.
.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!