%example.m
%Example script to calculate relevant quantities for the cable assignment.
%Start 1-7-12
clear
format long g

%User input 
f=8; %feet, initial sag of cable when installed;
L=90; %feet, horizontal span of the cable

%Begin Calculations to integration the function y
nint=200; %number of intervals to integrate the function over
x1=0;%beginning limit of integration
x2=L/2;%ending limit of integration
xinc=(x2-x1)/nint; %interval width

%Loop over the intervals
x=xinc/2; %start evaluation of the function here at midpoint of 1st interval
integral=0; %the answer for the integral of the function over the interval
for i=1:nint;    
    u=f*x^2;
    y=3*u;
    integral=integral+xinc*y;
    x=x+xinc;
end; 
integral=integral*2; %double the answer since integration only was from 0 to L/2

%Display the answer
display('The numerical integral answer:')
integral

%analytical integral answer
%(not necessary as part of your assignment, but added here to see how our
%numerical solution compares)
x=L/2;
eval2=3*f*x^3/3;
x=0;
eval1=3*f*x^3/3;
integralexact=2*(eval2-eval1)

%Numerically calculate the derivative d(integral)/df, let it equal didf
%Loop over the intervals
x=xinc/2; %start evaluation of the function here at midpoint of 1st interval
didf=0; %the answer for the derivative of the function, di/df
for i=1:nint;    
    dudf=x^2;
    dydu=3;
    didf=didf+xinc*dydu*dudf;
    x=x+xinc;
end; 
didf=didf*2; %double the answer since x ranged from 0 to L/2

%Display the answer
display('The numerical didf answer:')
didf

%analytical didf answer
%(not necessary as part of your assignment, but added here to see how our
%numerical solution compares)
x=L/2;
eval2=x^3;
x=0;
eval1=x^3;
didfexact=2*(eval2-eval1)
%End of Matlab script
