In: Computer Science
In MATLAB:
(a) Insert a comment in your script that says Problem 1:
(b) Below this comment, generate a vector of values called x that starts at −10, ends at 8, and has a step size of 0.1 between points.
(c) Define the function f(x) = (x − 6)(x 2 − 1)(x + 8) in your script, then plot f(x) against x.
(d) Plot the x- and y-axes on the plot in part (c). Mark each of the x-intercepts on this plot (you will need the hold on command).
(e) Label the x- and y-axes (you will need the xlabel and ylabel commands).
% Problem 1:
% Matlab script to calculate and plot the function f(x) for
given values of
% x and plot the x-intercepts points for this function
% define the vector x that starts from -10 and end at 8 with steps
of 0.1
x = (-10:0.1:8);
%define the function
f = @(x) (x-6).*((x.^2)-1).*(x+8);
y = f(x); % calculate the value of f at x points
plot(x,y); % plot y vs x
hold on; % used to plot in the same graph
% find is used to return the indices corresponding to the
entries
% satisfying the given condition
% Here it will return the indices of y vector where value of
function is 0
I = find(y==0);
% plot x-axes and y-axes
xL = xlim; % get the x limits for the current axes
yL = ylim; % get the y limits for the current axes
line([0 0],yL); % plot y-axis
line(xL,[0 0]); % plot x-axis
%plot the x-intercepts with red circles
plot(x(I),y(I),'ro');
hold off; % release the current plot
xlabel('x'); % label the x-axes
ylabel('f(x)'); % label the y-axes
% set the title
title('Plot of f(x) v/s x marking each of x-intercepts');
%end of script
Output: