1

我想查看具有不同 x 值的函数 y(x) 的值,其中 y(x) < 5:

y = abs(x)+abs(x+2)-5

我该怎么做?

4

4 回答 4

2
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'})
于 2009-10-10T01:26:30.177 回答
1

您可以只创建一个值向量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)
于 2009-10-09T22:17:34.453 回答
1

如果您限制x范围[-6 4],它将确保y限制在小于或等于 5。在 MATLAB 中,您可以使用FPLOT(如Amro 建议)或LINSPACEPLOT(如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 
于 2009-10-10T03:05:15.330 回答
0
% 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))
于 2009-10-10T01:08:11.863 回答