Skip to content

MATLAB - Calculus

MATLAB, through its Symbolic Math Toolbox, provides powerful tools for solving problems in differential and integral calculus. It allows for exact, symbolic solutions to limits, derivatives, and integrals, which is invaluable for theoretical work. You can also solve differential equations and visualize complex functions to analyze their behavior.

This chapter focuses on a fundamental concept of calculus: limits. We will explore how to compute limits of functions symbolically and verify their algebraic properties. This forms the foundation for understanding derivatives and integrals, which are covered in subsequent chapters.

The primary tool for calculating limits in MATLAB is the limit function. Before using it, you must declare your symbolic variables using the syms command. This tells MATLAB to treat these variables as symbolic entities rather than numerical values.

In its basic form, limit(f, x, a) finds the limit of the symbolic expression f as the variable x approaches the value a. If the third argument a is omitted, MATLAB calculates the limit as x approaches 0.

Example: Limit as a Variable Approaches Zero

Section titled “Example: Limit as a Variable Approaches Zero”

Let’s calculate the limit of f(x) = (x^3 + 5) / (x^4 + 7) as x approaches 0.

% First, declare x as a symbolic variable
syms x;
% Define the function expression
f = (x^3 + 5) / (x^4 + 7);
% Calculate the limit as x approaches 0
L = limit(f, x, 0)
% An alternative shorter syntax when the limit point is 0:
% L = limit(f)

MATLAB will execute the code and return the exact symbolic result:

L =
5/7

Example: Limit as a Variable Approaches a Non-Zero Number

Section titled “Example: Limit as a Variable Approaches a Non-Zero Number”

Now, let’s calculate the limit of g(x) = (x - 3) / (x - 1) as x approaches 1.

syms x;
g = (x - 3) / (x - 1);
L = limit(g, x, 1)

MATLAB returns:

L =
NaN

Why NaN? NaN stands for ‘Not a Number’. MATLAB returns this because the limit does not exist as a single finite value. As x approaches 1 from the left, the function goes to +Inf. As x approaches 1 from the right, it goes to -Inf. Since the one-sided limits differ, the two-sided limit does not exist. We’ll explore this next.

When a function has a discontinuity, the two-sided limit may not exist. However, the one-sided limits often do. A left-sided limit (x -> a-) approaches a from values less than a. A right-sided limit (x -> a+) approaches a from values greater than a.

You can compute one-sided limits by adding the 'left' or 'right' option to the limit function.

Consider the function f(x) = (x - 3) / |x - 3|. This function has a jump discontinuity at x = 3. Let’s verify this by plotting the function and computing the one-sided limits.

Best Practice: The ezplot function is no longer recommended. Use fplot for robust and customizable function plotting.

syms x;
f = (x - 3) / abs(x - 3);
% Plot the function to visualize the discontinuity
figure;
fplot(f, [-1, 7]);
title('Plot of f(x) = (x-3)/|x-3|');
xlabel('x');
ylabel('f(x)');
grid on;
% Compute the left-sided limit as x approaches 3
L_left = limit(f, x, 3, 'left')
% Compute the right-sided limit as x approaches 3
L_right = limit(f, x, 3, 'right')

Running this code first generates a plot. The plot shows a horizontal line at y = -1 for all x < 3, and another horizontal line at y = 1 for all x > 3. There is a clear vertical jump at x = 3, visually confirming the discontinuity.

The command window then displays the computed limits:

L_left =
-1
L_right =
1

Since L_left is not equal to L_right, the two-sided limit lim(x->3) f(x) does not exist.

The Algebraic Limit Theorem describes how limits interact with arithmetic operations. If lim(x->a) f(x) = L and lim(x->a) g(x) = M, then:

  • lim(f(x) + g(x)) = L + M
  • lim(f(x) - g(x)) = L - M
  • lim(f(x) * g(x)) = L * M
  • lim(f(x) / g(x)) = L / M (provided M is not 0)

We can use MATLAB to verify these properties for specific functions. Let f(x) = (3x + 5)/(x - 3) and g(x) = x^2 + 1, and let’s check the limits as x approaches 4.

syms x;
f = (3*x + 5) / (x - 3);
g = x^2 + 1;
% Define the limit point
a = 4;
% Calculate individual limits
L1 = limit(f, x, a)
M1 = limit(g, x, a)
% Verify properties
L_Add = limit(f + g, x, a)
L_Sub = limit(f - g, x, a)
L_Mult = limit(f * g, x, a)
L_Div = limit(f / g, x, a)
% Check if L_Add == L1 + M1, etc.

When you run the file, it displays the results, which you can see match the theorem’s predictions:

L1 =
17
M1 =
17
L_Add =
34
L_Sub =
0
L_Mult =
289
L_Div =
1
  • Forgetting syms: If you get an ‘Undefined function or variable’ error, you likely forgot to declare your variable with syms x;.
  • Symbolic vs. Numeric: x = 5; creates a numeric variable. syms x; creates a symbolic one. The limit function requires symbolic variables.
  • Interpreting NaN: NaN from the limit function usually means the two-sided limit doesn’t exist. Use one-sided limits to investigate further.

Working with Open-Source Alternatives (e.g., GNU Octave)

Section titled “Working with Open-Source Alternatives (e.g., GNU Octave)”

Open-source tools like GNU Octave offer a free alternative to MATLAB. For symbolic computations, Octave requires the symbolic package, which uses the SymPy library. The syntax can differ.

% In Octave
pkg load symbolic
symbols
x = sym("x");
limit((x^3 + 5)/(x^4 + 7), x, 0)

While Octave is powerful, specialized toolboxes like MATLAB’s Symbolic Math are often more robust, feature-rich, and have more optimized performance.