% File: integralPlot.m Version 1 % % A. O. Hausknecht Spring 2012 % Mathematics Department, UMass Dartmouth % % integralPlot(Finline, x0, xMin, xMax, n, plotFtoo) % % PURPOSE: Automates the plotting of a function % by an integeral, y(x)= integral(f(x), x0, x), % over an interval [xMin, xMax] % % ITS PARAMETERS: % Finline: The f(x) represented as an inline function % x0: The integals lower limit % xMin, xMax: The ploting domain % n: The number of x-values sampled % plotFtoo: plots Finline as well when true or 1 is passed % function integralPlot(Finline, x0, xMin, xMax, n, plotFtoo) % % Generate n equally spaced x-values % bewteen xMin and xMax x = linspace(xMin, xMax, n); % Create an n y-values equal to zero y = zeros([1,n ]); % Evaluate the integral at the x-values for i = 1:n % Use quadature to approximate the integral % of f(x) from xMin to x(i) y(i) = quad(Finline, x0, x(i)); i++; end % Plot the integral of FLine clf; plot(x, y,'r', 'linewidth', 3); yMin = min(y); yMax = max(y); if (plotFtoo) % Add a plot of Finline yF = Finline(x); yFMin = min(yF); yFMax = max(yF); yMin = min( [yMin yFMin]); yMax = max([yMax yFMax]); hold on; plot(x, yF, 'g', 'linewidth', 3); legend('y1(x) = integral(f(x), x)', 'y2(x) = f(x)'); hold off; end % Set a a viewing rectangle 10% larger than the plot yMin = yMin*1.1; yMax = yMax*1.1; axis([xMin xMax yMin yMax]); xlabel('x'); ylabel('y'); % Show the grid and draw axes ax=gca(); set(ax, 'xgrid', 'on', 'ygrid', 'on'); line([xMin xMax],[0 0], 'linewidth', 4); line([0 0],[yMin yMax], 'linewidth', 4); end