문제

제목에서 알 수 있듯이,나는 삼각형을 그리는 방법을 알고 싶다.예를 들어

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 당신은 이것을 할 수 있습니다:

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

다른 함수들에 대해서는 이 함수처럼 진행해야 합니다.엑스-축 벡터와 와이-축 벡터를 만들어야 합니다.

업데이트: 다른 예에서: 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top