% Matlab script int_script_template % Integrates y = x.*2 % Steve Martel % Date 8_26_04 % Set the increment dx dx = 1; %dx = 0.1; %dx = 0.01; % Set the domain of x (let x range from 0 to 10) x = 0:dx:10; % Determine how many values of x there are n = length(x); % Break the set of x-values into two sets, % one that has all values but the last, % the other that has all values but the first x_beg = x(1:n-1); x_end = x(2:n; % Find the x-midpoints x_mid = (x_beg + x_end)/2; % Calculate y at the values of x and x_mid y = x.^2; y_mid = x_mid.^2; y_beg = x_beg.^2; % Open up a figure window called "figure 1" figure(1) clf % Plot the curve y = x.^2 on figure 1 plot(x,y) hold on stairs(x,y) % Sum up the area of the rectangles to get the cummulative area area_mid = cumsum(y_mid.*dx)); area_beg = cumsum(y_beg.*dx); % Open up a figure window called "figure 2" figure(2) clf % Plot the area under the curve y = x.^2 on figure 2 plot(x_mid,area_mid) hold on plot(x,(1./3)*x.^3,'r')