我想查看具有不同 x 值的函数 y(x) 的值,其中 y(x) < 5:
y = abs(x)+abs(x+2)-5
我该怎么做?
fplot(@(x) abs(x)+abs(x+2)-5, [-10 10])
hold all
fplot(@(x) 5, [-10 10])
legend({'y=abs(x)+abs(x+2)-5' 'y=5'})
您可以只创建一个值向量x
,然后查看向量y
,或绘制它:
% Create a vector with 20 equally spaced entries in the range [-1,1].
x = linspace(-1,1,20);
% Calculate y, and see the result
y = abs(x) + abs(x + 2) - 5
% Plot.
plot(x, y);
% View the minimum and maximum.
min(y)
max(y)
如果您限制x
范围[-6 4]
,它将确保y
限制在小于或等于 5。在 MATLAB 中,您可以使用FPLOT(如Amro 建议)或LINSPACE和PLOT(如Peter 建议)绘制函数:
y = @(x) abs(x)+abs(x+2)-5; % Create a function handle for y
fplot(y,[-6 4]); % FPLOT chooses the points at which to evaluate y
% OR
x = linspace(-6,4,100); % Create 100 equally-spaced points from -6 to 4
plot(x,y(x)); % PLOT will plot the points you give it
% create a vector x with values between 1 and 5
x = 1:0.1:5;
% create a vector y with your function
y = abs(x) + abs(x+2) - 5;
% find all elements in y that are under 5
y(find(y<5))