Plotting piecewise function

后端 未结 1 899
星月不相逢
星月不相逢 2021-01-25 23:53

I have a solution to a differential solution but the issue is that I have different solutions in different intervals.

For examle:

x_1(t) when t belongs t         


        
相关标签:
1条回答
  • 2021-01-26 00:40

    You can use plot with multiple inputs to plot them altogether:

    % the functions:
    x_1 = @(t) 2.*t;
    x_2 = @(t) 5.*t;
    x_3 = @(t) 7.*t;
    % the transition points:
    t_1 = 30;
    t_2 = 60;
    t_3 = 90;
    % plotting:
    plot(0:t_1,x_1(0:t_1),t_1:t_2,x_2(t_1:t_2),t_2:t_3,x_3(t_2:t_3))
    

    Another way, that lets you define all kind of function specific visual properties is to use hold:

    f = @(t,a) a.*t;
    t = 0:30:100;
    m = 'os^'; % choosing different marker for each function
    for k = 1:numel(t)-1
        plot(t(k):t(k+1),f(t(k):t(k+1),k),m(k))
        hold on
    end
    hold off
    

    0 讨论(0)
提交回复
热议问题