MATLAB: How to get the integral of this function I created

functionintegrationMATLABmatlab function

x=-6:6;
[y]=f(x);
plot(x,f(x))
grid on
% (b)
%Determine f(3)-f(5)
[y]=f(x);
ans = f(3) - f(5);
%Determine definite integral
[y]=f(x);
int_ans=integral(f(x),-6,6);
with my function file f.m looking like this:
function [y] = f(x)
y = (x.^3 - x.^2 + 2*x + 1);
end

Best Answer

x=-6:6;
y=f(x);
plot(x,y)
grid on
%Determine f(3)-f(5)
ans = f(3) - f(5)
%Determine definite integral
int_ans=integral(@f,-6,6)
%with my function file f.m looking like this:
function y = f(x)
y = (x.^3 - x.^2 + 2*x + 1);
end
NOTE: Use function handle @f.