%DEFINING A FUNCTION, INTEGRATING and PLOTTING
%(Selcuk Bayin, METU-NCC, bayin@metu.edu.tr)
%TYPE I
clc
f=inline('sin(x).*cos(x).^2','x')
%%
%Value of f(x) at x:
f(2)
%%
y=1:2:13
%%
f(y)
%%
%TYPE II
clc
clear x
x=0:1/10:10;
g=sin(x).*cos(x).^2;
plot(x,g)
grid
%%
%TYPE III:
clc
fplot(@(x)[3*sin(x)*cos(x)^2],[0,10])
%INTEGRATION of f(x)BETWEEN [a, b]
%via the RECTANGULAR RULE:
%%
clc
f=inline('3*sin(x).*cos(x).^2','x')
a=1;
b=11;
N=10000;
[a b N]
dx=(b-a)/N
%USING the RIGHT END POINTS:REP
%USING the LEFT END POINTS:LEP
%USING the MIDPOINTS:MIDPTS
%%
REP=(a+dx):dx:b;
LEP=a:dx:(b-dx);
MIDPT=(a+dx/2):dx:(b-dx/2);
%NUMERICAL INTEGRATION:
%With the REPs
%%
area=f(REP)*dx;
IntwithREPs=sum(area)
%%
area=f(LEP)*dx;
IntwithLEPs=sum(area)
%%
area=f(MIDPT)*dx;
IntwithMIDPTS=sum(area)
%%
%COMPARE THE THREE METHODS
[IntwithREPs IntwithLEPs IntwithMIDPTS]
%%
%INTEGRAL via the TRAPOZOIDAL RULE
clc
endpoints=a:dx:b;
fendpoints=f(endpoints(1:end));
IntViaTrapozoid=((1/2)*(fendpoints(1)+fendpoints(N+1))+sum(fendpoints(2:N)))*dx
%%
SUMMARY=[IntwithREPs IntwithLEPs IntwithMIDPTS IntViaTrapozoid]
%%
%INTEGRAL VIA MATHLAB
clc
syms x
MATHlabVAL=int(3*sin(x).*cos(x).^2,1,11)
SUMMARY
MATHlabVAL
%%
clc
%syms x
int(sin(2*x),0,pi/2)