首页 文章

如何在Matlab中绘制三角函数和其他阶梯函数[关闭]

提问于
浏览
-1

正如 Headers 所示,我想知道如何在Matlab中绘制三角函数 . 例如

f(x) = 1-|x| for |x| < 1 and f(x) = 0 otherwise

以及功能:

Af(x) = A for x >= 0 and Af(x) = 0 for x < 0; -f(x) = -1 for x >= 0 and -f(x) = 0 for x < 0

1 回答

  • 1

    我假设您没有使用符号变量 . 对于matlab中的2D绘图,您需要两个相等大小的矢量,每个轴一个,因此您需要创建一个x轴矢量和y轴矢量 . 在你的例子中f(x)= 1- | x | for | x | <1你可以这样做:

    x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
    y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
    y(abs(x) < 1) = 1- abs(x(abs(x)<1)); %the points corresponding to |x|< 1 are set to |x|
    
    figure() %new figure
    plot(x,y) %plot
    box off  %removing box
    grid on  %adding grid
    xlabel('x axis', 'FontSize', 15) %label of x axis
    ylabel('y axis', 'FontSize', 15) %label of y axis
    axis([x(1), x(end), -0.5, 1.5])  %axis limits
    

    有了这个你得到这样的情节:

    enter image description here

    对于其他功能,您必须像这一样进行, Build x轴向量和y轴向量 .

    UPDATE: 在另一个例子中: f(x) = A for x >= 0 and f(x) = 0 for x < 0

    A = 3;
    x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
    y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
    y(x >= 0) = A; %the points corresponding to x >= 0 are set to A
    
    figure() %new figure
    plot(x,y) %plot
    box off  %removing box
    grid on  %adding grid
    xlabel('x axis', 'FontSize', 15) %label of x axis
    ylabel('y axis', 'FontSize', 15) %label of y axis
    axis([x(1), x(end), -0.5, 3.5])  %axis limits
    

    enter image description here

相关问题