MATLAB is a powerful computational tool that can significantly enhance your understanding and application of Calculus 2 concepts. These notes provide a practical guide for using MATLAB to solve problems related to integration techniques, sequences and series, parameterized curves, and multivariable calculus.
The MATLAB interface consists of several key components:
% Variablesx = 5; % Semicolon suppresses outputy = pi; % Built-in constants% Basic operationsz = x + y;w = sin(x); % Trigonometric functions% Displaying resultsdisp(z); % Display valuefprintf('z = %f\n', z); % Formatted output% Help and documentationhelp sin % Help on specific functiondoc sin % Documentation for sin function For indefinite and definite integrals, MATLAB's Symbolic Math Toolbox provides powerful capabilities:
% Define symbolic variablesyms x% Indefinite integralF = int(sin(x), x) % Returns -cos(x)% Definite integralintegral_value = int(x^2, x, 0, 2) % Returns 8/3% Improper integralsimproper = int(1/x, x, 1, inf) % Returns Inf% Multiple integralsdouble_int = int(int(x*y, y, 0, 1), x, 0, 2) % Returns 1 Note: When using symbolic integration, you can use simplify() to simplify the result and pretty() to display it in a more readable format.
For functions without simple antiderivatives or experimental data:
% Define a function handlef = @(x) exp(-x.^2);% Numerical integrationresult = integral(f, 0, 1) % e dx% Multiple integrationf2 = @(x,y) x*y;result2 = integral2(f2, 0, 1, 0, 2) % x*y dy dx Example: Calculate the arc length of y = sin(x) from 0 to :
f = @(x) sqrt(1 + cos(x).^2);arc_length = integral(f, 0, pi); This returns approximately 3.8202.
MATLAB can generate and analyze numerical sequences:
% Generating sequencesn = 1:10;a = 1./n; % a_n = 1/nb = (n+1)./n; % b_n = (n+1)/n% Plotting sequencesfigure;subplot(2,1,1); stem(n, a);title('Sequence a_n = 1/n');xlabel('n'); ylabel('a_n');subplot(2,1,2); stem(n, b);title('Sequence b_n = (n+1)/n');xlabel('n'); ylabel('b_n'); % Symbolic summationsyms n kS1 = symsum(1/n^2, n, 1, Inf) % ^ 1/n = /6S2 = symsum(0.5^n, n, 0, Inf) % ^ 0.5 = 2% Taylor seriessyms xf = exp(x);T5 = taylor(f, x, 'Order', 6) % 5th order Taylor series for e% Testing for convergence% Ratio testan = 1/factorial(n);ratio = limit(subs(an, n, n+1)/subs(an, n, n), n, Inf) Example: Find the sum of the alternating harmonic series (-1)/n:
syms nalternating_harmonic = symsum((-1)^(n+1)/n, n, 1, Inf) This returns ln(2), confirming the mathematical result.
% Parametric equationst = linspace(0, 2*pi, 1000);x = cos(3*t);y = sin(2*t);% Plotfigure;plot(x, y);title('Parametric Curve: x=cos(3t), y=sin(2t)');xlabel('x'); ylabel('y');axis equal; grid on; % Symbolic approachsyms tx = cos(t);y = sin(t);ds = sqrt(diff(x)^2 + diff(y)^2);L = int(ds, t, 0, 2*pi); % L = 2 for a unit circle % Polar plottheta = linspace(0, 2*pi, 1000);r = 2 + cos(5*theta);figure;polarplot(theta, r);title('Polar Curve: r = 2 + cos(5)');% Converting to Cartesian for arc lengthsyms thetar = 2 + cos(5*theta);ds = sqrt(r^2 + diff(r)^2);L = int(ds, theta, 0, 2*pi); % Define symbolic variablessyms x y zf = x^2 + y^2;% Plotting 3D surface[X,Y] = meshgrid(-2:0.1:2, -2:0.1:2);Z = X.^2 + Y.^2;figure;surf(X,Y,Z);title('Surface: z = x + y');xlabel('x'); ylabel('y'); zlabel('z');% Contour plotsfigure;contour(X,Y,Z,20);title('Contour Plot: z = x + y');colorbar; % Partial derivativesfx = diff(f, x) f/x = 2xfy = diff(f, y) f/y = 2y% Second partial derivativesfxx = diff(fx, x) f/x = 2fxy = diff(fx, y) f/xy = 0fyy = diff(fy, y) f/y = 2% Gradientgradient_f = [diff(f,x), diff(f,y)] % f = (2x, 2y)% Directional derivativedirection = [1, 1]; % Direction vector uu = direction/norm(direction); % Unit directiongrad_f = subs([diff(f,x), diff(f,y)], [x,y], [1,1]);D_u_f = dot(grad_f, u) % Directional derivative at (1,1) % Double integralsf = x*y;I = int(int(f, y, 0, 1), x, 0, 2) % x*y dy dx% Triple integralsg = x*y*z;J = int(int(int(g, z, 0, 1), y, 0, 1), x, 0, 1) % x*y*z dz dy dx% Changing order of integration depends on the region% Polar coordinates integrationsyms r thetapolar_f = r^2;K = int(int(polar_f*r, r, 0, 1), theta, 0, 2*pi) % Optimization with constraintsyms x y lambdaf = x^2 + y^2; % Function to optimizeg = x + y - 1; % Constraint g(x,y) = 0% Solve f = g and constrainteq1 = diff(f,x) - lambda*diff(g,x);eq2 = diff(f,y) - lambda*diff(g,y);eq3 = g;solution = solve([eq1, eq2, eq3], [x, y, lambda]); % Define symbolic vector fieldsyms x y zF = [x^2, y*sin(z), z*exp(x)];% Quiver plot (2D vector field)[X,Y] = meshgrid(-2:0.2:2, -2:0.2:2);U = X.^2;V = Y.*sin(X); % Using X as z for 3D field visualizationfigure;quiver(X,Y,U,V);title('2D Vector Field');xlabel('x'); ylabel('y'); % Line integral of a scalar fieldsyms tr = [cos(t), sin(t)]; % Parameterization of curvedr = diff(r, t);f = x^2 + y^2; % Scalar fieldintegral_f = int(subs(f, [x,y], r).*norm(dr), t, 0, 2*pi);% Line integral of a vector fieldF = [-y, x]; % Vector fieldline_int = int(dot(F, dr), t, 0, 2*pi); % Parameterized surfacesyms u vr = [u*cos(v), u*sin(v), u]; % Paraboloid z = x^2 + y^2ru = diff(r, u);rv = diff(r, v);n = cross(ru, rv);norm_n = sqrt(sum(n.^2));% Surface integral of scalar fieldf = x + y + z;surface_int = int(int(subs(f, [x,y,z], r)*norm_n, u, 0, 1), v, 0, 2*pi); % Computing Fourier series coefficientssyms n x f Lf = piecewise(x=pi, 2*pi-x);L = pi; % Half period% Fourier coefficientsa0 = (1/L)*int(f, x, -L, L);an = (1/L)*int(f*cos(n*pi*x/L), x, -L, L);bn = (1/L)*int(f*sin(n*pi*x/L), x, -L, L);% Partial sumsN = 5;partial_sum = a0/2 + symsum(an*cos(n*pi*x/L) + bn*sin(n*pi*x/L), n, 1, N);% Plot original function and Fourier approximationfigure;fplot(f, [-2*L, 2*L]);hold on;fplot(partial_sum, [-2*L, 2*L]);legend('Original Function', sprintf('Fourier Approximation (N=%d)', N));title('Fourier Series Approximation'); % Laplace transformsyms t sf = t^2*exp(-t);F = laplace(f, t, s); % L{te}(s)% Inverse Laplace transformg = s/(s^2 + 4);G = ilaplace(g, s, t); % L{s/(s+4)}(t) MATLAB has built-in support for many special functions encountered in Calculus 2:
% Gamma functionsyms xgamma_val = gamma(sqrt(2)); % (2)% Beta functionbeta_val = beta(2, 3); % B(2,3)% Error functionerf_val = erf(1); % erf(1) vpa() for variable-precision arithmetic when needed.Tip: When working with symbolic expressions, use simplify(), expand(), or factor() to manipulate expressions into more useful forms. This is particularly helpful when dealing with integrals and derivatives.
MATLAB is a powerful tool for exploration and verification of Calculus 2 concepts. By combining analytical techniques with numerical and symbolic computation, you can gain deeper insight into integration techniques, series, parameterized curves, and multivariable calculus. The examples provided in these notes serve as a starting point for using MATLAB effectively in your Calculus 2 studies.
Remember that while MATLAB can handle complex computations, understanding the underlying mathematics remains essential. Use these computational tools to enhance, not replace, your mathematical reasoning and problem-solving skills.
