How to get arrows on axes in MATLAB plot?

前端 未结 2 1568
醉梦人生
醉梦人生 2021-02-02 02:54

I want to plot something like this:

x = 0:0.01:10;
f = @(x) 50* 1.6.^(-x-5);
g = @(x) 50* 1.6.^(+x-10);
plot(x, f(x));
hold on
plot(x, g(x));

I

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 03:18

    Well, don't say I didn't warn you :)

    % Some bogus functions
    f = @(x) 50* 1.6.^(-x-5);
    g = @(x) 50* 1.6.^(+x-10);
    
    % Point where they meet
    xE = 2.5;
    yE = f(xE);
    
    % Plot the bogus functions
    figure(1), clf, hold on
    x = 0:0.2:5;
    plot(x,f(x),'r',  x,g(x),'b', 'linewidth', 2)
    
    % get rid of standard axes decorations
    set(gca, 'Xtick', [], 'Ytick', [], 'box', 'off')
    
    % Fix the axes sizes
    axis([0 5 0 5])
    
    % the equilibrium point
    plot(xE, yE, 'k.', 'markersize', 20)
    
    % the dashed lines
    line([xE 0; xE xE], [0 yE; yE yE], 'linestyle', '--', 'color', 'k')
    
    % the arrows
    xO = 0.2;  
    yO = 0.1;
    patch(...
        [5-xO -yO; 5-xO +yO; 5.0 0.0], ...
        [yO 5-xO; -yO 5-xO; 0 5], 'k', 'clipping', 'off')
    
    % the squishy wiggly line pointing to the "equilibrium" text
    h = @(x)0.5*(x+0.2) + 0.1*sin((x+0.2)*14);
    x = 2.7:0.01:3.5;
    plot(x, h(x), 'k', 'linewidth', 2)
    
    % the static texts
    text(xE-yO, -0.2, 'Q^*', 'fontweight', 'bold')
    text(-2*yO,   yE, 'P^*', 'fontweight', 'bold')
    text(-2*yO,    4, 'Price', 'rotation', 90, 'fontsize', 14)
    text(    4, -0.2, 'Quantity', 'fontsize', 14)
    text(   .5,  4.2, 'Demand', 'fontsize', 14, 'rotation', -55)
    text(   4.0,  3.3, 'Supply', 'fontsize', 14, 'rotation', +55)
    text(   3.6,  2.1, 'Equilibrium', 'fontsize', 14)
    

    Result:

    enter image description here

提交回复
热议问题