I want to see the values of the function y(x) with different values of x, wher开发者_如何学Goe y(x) < 5:
y = abs(x)+abs(x+2)-5
How can I do it?
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'})
You can just create a vector of values x
, then look at the vector y
, or plot it:
% 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)
If you limit x
to the range [-6 4]
, it will ensure that y
will be limited to less than or equal to 5. In MATLAB, you can then plot the function using FPLOT (like Amro suggested) or LINSPACE and PLOT (like Peter suggested):
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))
精彩评论