正如标题所示,我想知道如何在Matlab中绘制三角函数。 e.g。
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
答案 0 :(得分:1)
我假设你没有使用符号变量。对于matlab中的2D绘图,您需要两个相等大小的矢量,每个轴一个,因此您需要创建一个x轴矢量和y轴矢量。在你的例子中f(x)= 1- | x | for | x | &LT; 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
有了这样你就得到了这样的情节:
对于其他功能,你必须像这一个一样,建立x轴向量和y轴向量。
更新:在另一个示例中: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