Search code examples
matlabplotlinevisualizationmatlab-figure

Drawing a discontinuous line


How can I get this graph in MATLAB? This is the payoff of down-and-out call option with barrier B=120, strike K=100. Should I somehow combine two graphs in one picture?

enter image description here


Solution

  • You can do it in various ways. Here are two of them:

    Option 1:

    function q54615569(B, K)
    y = @(x)(x>=B).*(x-K);
    x = 0:2*K;
    figure(); plot(x, y(x));
    xlabel('S(T)');
    

    enter image description here

    Option 2:

    function q54615569(B, K)
    y = @(x)x-K;
    x = 0:2*K;
    figure(); hL = plot( x(x<=B), 0.*x(x<=B), x(x>=B), y(x(x>=B)) );
    set(hL, 'Color', lines(1), 'Linestyle', 'none', 'Marker','o','MarkerSize', 2,...
      'MarkerFaceColor', lines(1));
    xlabel('S(T)');
    

    enter image description here