Introduction to MATLAB for Calculus
MATLAB is a powerful computational tool that provides extensive functionality for calculus operations. This cheat sheet covers essential MATLAB commands and techniques for performing calculus computations, from differentiation to solving differential equations.
Note: To perform symbolic mathematics in MATLAB, you need to use the Symbolic Math Toolbox. Make sure you have this toolbox installed and use syms to declare symbolic variables.
syms x % Declare x as a symbolic variable
syms y z % Declare multiple symbolic variables
Basic MATLAB Syntax
Understanding basic MATLAB syntax is essential for performing calculus operations efficiently.
% Comments start with %
a = 5; % Assign value to variable (semicolon suppresses output)
b = [1 2 3]; % Create a row vector
c = [1; 2; 3]; % Create a column vector
d = 1:5; % Create vector [1 2 3 4 5]
e = 0:0.1:1; % Create vector from 0 to 1 with step 0.1
Tip: Use up arrow in the command window to recall previous commands. Use Tab for auto-completion of function names.
Mathematical Functions
MATLAB provides numerous built-in mathematical functions useful for calculus.
| Function | Description |
sin(x), cos(x), tan(x) | Trigonometric functions |
asin(x), acos(x), atan(x) | Inverse trigonometric functions |
exp(x) | Exponential function (e^x) |
log(x) | Natural logarithm (ln(x)) |
log10(x) | Base 10 logarithm |
sqrt(x) | Square root |
abs(x) | Absolute value |
power(x,n) or x.^n | x raised to power n |
Differentiation in MATLAB
MATLAB provides several methods for computing derivatives.
Symbolic Differentiation
syms x f = x^3 + 2*x^2 + x; df = diff(f, x) % First derivative with respect to x d2f = diff(f, x, 2) % Second derivative
Numerical Differentiation
x = 0:0.1:10; y = sin(x); dy = gradient(y, x); % First derivative using finite differences
Partial Derivatives
syms x y f = x^2*y + x*y^2; df_dx = diff(f, x) % Partial derivative with respect to x df_dy = diff(f, y) % Partial derivative with respect to y
Integration in MATLAB
MATLAB offers both symbolic and numerical integration capabilities.
Symbolic Integration
syms x f = x^2; F = int(f, x) % Indefinite integral result = int(f, x, 0, 1) % Definite integral from 0 to 1
Numerical Integration
f = @(x) x.^2; % Function handle result = integral(f, 0, 1) % Numerical integration from 0 to 1 % For 2D integration: f2d = @(x,y) x.*y; result2d = integral2(f2d, 0, 1, 0, 1)
Note: For numerical integration, use integral instead of quad or quadl, as they are older functions with less accuracy.
Limits in MATLAB
You can compute limits of functions using symbolic computation.
syms x f = sin(x)/x; limit(f, x, 0) % Limit as x approaches 0 limit(f, x, 0, 'left') % Left-hand limit limit(f, x, 0, 'right') % Right-hand limit limit(f, x, Inf) % Limit as x approaches infinity
Tip: Use simplify or pretty to simplify or display the result in a more readable format if needed.
Series in MATLAB
MATLAB can work with Taylor series and other series expansions.
Taylor Series
syms x f = exp(x); taylor(f, x, 'Order', 5) % Taylor series expansion up to 5th order taylor(f, x, 'ExpansionPoint', 1, 'Order', 3) % Expansion around x=1
Summation
syms k S = symsum(k^2, k, 1, 10) % Sum of k^2 from k=1 to 10 S_inf = symsum(1/k^2, k, 1, Inf) % Sum from k=1 to infinity
Plotting Functions in MATLAB
Visualization is crucial in calculus. MATLAB offers various plotting functions.
% Basic 2D plot x = linspace(0, 2*pi, 100); y = sin(x); plot(x, y); xlabel('x'); ylabel('sin(x)'); title('Plot of sin(x)'); grid on;
% Multiple functions on the same plot y2 = cos(x); figure; plot(x, y, 'b-', x, y2, 'r--'); legend('sin(x)', 'cos(x)');
% 3D surface plot [X, Y] = meshgrid(-2:0.1:2, -2:0.1:2); Z = X.*exp(-X.^2 - Y.^2); surf(X, Y, Z); xlabel('x'); ylabel('y'); zlabel('z'); title('3D Surface Plot');
Multivariable Calculus in MATLAB
Working with functions of multiple variables is straightforward in MATLAB.
syms x y z f = x^2*y + sin(x*y*z); % Partial derivatives df_dx = diff(f, x); df_dy = diff(f, y); % Gradient vector grad_f = [diff(f, x), diff(f, y)]; % Hessian matrix hess_f = jacobian(grad_f, [x, y]); % Directional derivative in direction of vector v = [2, 1] v = [2, 1]; dir_deriv = sum(grad_f .* v/ norm(v));
% Double integral f = @(x,y) x*y; result = integral2(f, 0, 1, 0, 2) % Triple integral f3d = @(x,y,z) x*y*z; result3d = integral3(f3d, 0, 1, 0, 1, 0, 1)
Differential Equations in MATLAB
MATLAB provides powerful tools for solving various types of differential equations.
Symbolic Solution of ODEs
syms y(t) eqn = diff(y, t) == 2*y; sol = dsolve(eqn); % General solution % Initial value problem cond = y(0) == 5; sol_ivp = dsolve(eqn, cond);
Numerical Solution of ODEs
% Solve dy/dt = -2*y, y(0) = 1 f = @(t, y) -2*y; [t, y] = ode45(f, [0 5], 1); plot(t, y);
Higher Order and System of ODEs
% Second order ODE: y'' + y = 0, y(0)=1, y'(0)=0 % Convert to system: % y1 = y, y2 = y' % y1' = y2, y2' = -y1 syms y1(t) y2(t) eqns = [diff(y1, t) == y2, diff(y2, t) == -y1]; conds = [y1(0)==1, y2(0)==0]; [y1Sol(t), y2Sol(t)] = dsolve(eqns, conds);
Optimization in MATLAB
Finding extrema of functions is a common calculus task that MATLAB can perform efficiently.
Finding Critical Points
syms x f = x^3 - 6*x^2 + 9*x + 1; df = diff(f, x); critical_points = solve(df == 0, x); disp(critical_points);
Numerical Optimization
% Find minimum of a function f = @(x) x^2 + 4*x + 6; x_min = fminbnd(f, -10, 10); % Find minimum in interval [-10,10] min_value = f(x_min); % Find maximum (minimize the negative of function) g = @(x) -(x^2 + 4*x + 6); x_max = fminbnd(g, -10, 10); max_value = -g(x_max);
Multivariable Optimization
% Find minimum of multivariable function fun = @(x) x(1)^2 + x(2)^2; % Function with variables x1, x2 x0 = [1, 1]; % Initial guess [x_opt, fval] = fminsearch(fun, x0);
We use cookies to enhance your browsing experience and analyze site traffic. By clicking 'Accept all cookies', you agree to the use of these cookies. You can manage your preferences or learn more in our [Privacy Policy/Cookie Policy.