% LINE INTEGRALS OF FUNCTIONS 
%(Selcuk Bayin, METU-NCC, bayin@metu.edu.tr)
clc
disp('f(x,y) = [(x^2-y),x*y] calculate line integral around the circle r(t) = [cos(t) sin(t)]')
%We calculate only the Int[(x^2-y)*dx] part Int[(x*y)*dy] is similar
%%
% Function to be integrated
f = inline('(x.^2-y)', 'x', 'y') 
%%
% Parameterization of the curve
x = inline('cos(t)', 't') 
y = inline('sin(t)', 't') 
%%
clc
g= inline('(cos(t).^2-sin(t)).*(-sin(t))','t')
%%
% Bounds for the parameter t
a = 0;                               
b = pi;
endpoints=[a b]
%%
% Number of steps and step size in t
n = 100;                         
dt = (b-a)/n;
nSteps_Stepsize=[n dt]
%%
%Endpoints and midpoints of segments
endpoints = a:dt:b ;                       
midpoints = a+dt/2:dt:b-dt/2;  
%%
clc
%Line integral in t: int f(t) dt
LineIntWith_t=sum(g(midpoints)*dt)
%%
%INTEGRAL VIA MATHLAB
clc
syms t
MATHlabVAL=int(g(t),t,a,pi)
%%
%Compare with
LineIntWith_t
%%
%INTEGRAL OVER THE ARCLENGTH
%Calculate the line integral Int[h(x,y)*ds] over the arclength ds
%Widths in x&y-coordinates and then the Arclength ds
clc
h = inline('(x.^2-y)', 'x', 'y')
%%
dx = x(endpoints(2:end)) - x(endpoints(1:end-1));  
dy = y(endpoints(2:end)) - y(endpoints(1:end-1));  
ds = sqrt( dx.^2 + dy.^2 );                        
%Line integral: Int[h(x,y)]*ds
line_integral = sum( h(x(midpoints), y(midpoints)).*ds )
%%
 
 


