What is the mathlab code for this figure?

57 ビュー (過去 30 日間)
Souhad
Souhad 2024 年 12 月 22 日 17:18
回答済み: Star Strider 2024 年 12 月 22 日 20:34
  1 件のコメント
Divyajyoti Nayak
Divyajyoti Nayak 2024 年 12 月 22 日 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.

サインインしてコメントする。

回答 (2 件)

John D'Errico
John D'Errico 2024 年 12 月 22 日 19:24
移動済み: John D'Errico 2024 年 12 月 22 日 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
fplot - Plot expression or function This MATLAB function plots the curve defined by the function y = f(x) over the default interval [-5 5] for x. Syntax fplot(f) fplot(f,xinterval) fplot(funx,funy) fplot(funx,funy,tinterval) fplot(___,LineSpec) fplot(___,Name,Value) fplot(ax,___) fp = fplot(___) [x,y] = fplot(___) Input Arguments f - Function to plot function handle xinterval - Interval for x [–5 5] (default) | two-element vector of form [xmin xmax] funx - Parametric function for x coordinates function handle funy - Parametric function for y coordinates anonymous function | function handle tinterval - Interval for t [-5 5] (default) | two-element vector of form [tmin tmax] ax - Axes object axes object LineSpec - Line style, marker, and color string scalar | character vector Name-Value Arguments MeshDensity - Number of evaluation points 23 (default) | number Color - Line color [0 0.4470 0.7410] (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... LineStyle - Line style "-" (default) | "--" | ":" | "-." | "none" LineWidth - Line width 0.5 (default) | positive value Marker - Marker symbol "none" (default) | "o" | "+" | "*" | "." | ... MarkerEdgeColor - Marker outline color "auto" (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerFaceColor - Marker fill color "none" (default) | "auto" | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... MarkerSize - Marker size 6 (default) | positive value Output Arguments fp - One or more FunctionLine or ParameterizedFunctionLine objects scalar | vector Examples openExample('graphics/PlotExpressionExample') openExample('graphics/PlotParametricCurveExample') openExample('graphics/SpecifyPlottingIntervalAndPlotPiecewiseFunctionsExample') openExample('graphics/SpecifyLinePropertiesAndDisplayMarkersExample') openExample('graphics/ModifyPlotAfterCreationExample') openExample('graphics/PlotMultipleLinesInSameAxesExample') openExample('graphics/AddTitleAndAxisLabelsAndFormatTicksExample') See also fcontour, fmesh, fplot3, fsurf, hold, title, fimplicit, FunctionLine Properties, ParameterizedFunctionLine Properties Introduced in MATLAB before R2006a Documentation for fplot doc fplot Other uses of fplot symbolic/fplot
Next, try adding one horizontal or vertical line.
help quiver
quiver - Quiver or vector plot This MATLAB function plots arrows with directional components U and V at the Cartesian coordinates specified by X and Y. Syntax quiver(X,Y,U,V) quiver(U,V) quiver(___,scale) quiver(___,LineSpec) quiver(___,LineSpec,'filled') quiver(___,Name,Value) quiver(ax,___) q = quiver(___) Input Arguments X - x-coordinates of arrow tails scalar | vector | matrix Y - y-coordinates of arrow tails scalar | vector | matrix U - x-components scalar | vector | matrix V - y-components scalar | vector | matrix LineSpec - Line style, marker, and color character vector | string scale - Arrow scaling factor nonnegative number | 'off' ax - Target axes Axes object Name-Value Arguments LineWidth - Width of arrow lines 0.5 (default) | positive value ShowArrowHead - Arrowhead display 'on' (default) | on/off logical value AutoScale - Use automatic scale factor 'on' (default) | on/off logical value AutoScaleFactor - Automatic scale factor 0.9 (default) | scalar Examples openExample('matlab/CreateQuiverPlotWindExample') openExample('matlab/DisableAutomaticScalingExample') openExample('matlab/PlotGradientAndContoursExample') openExample('matlab/SpecifyArrowColorQuiverExample') openExample('matlab/SpecifyAxesForQuiverPlotExample') openExample('matlab/ModifyQuiverPlotAfterCreationExample') See also contour, quiver3, meshgrid, Quiver Properties Introduced in MATLAB before R2006a Documentation for quiver doc 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.

Star Strider
Star Strider 2024 年 12 月 22 日 20:34
It would help to have the function for .
Lacking it, this still requires annotation objects, and that is not always straightforward.
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.
.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by