Session4 2
Session4 2
❖ In such cases, MATLAB will return the original function instead of the
antiderivative.
❖ MATLAB can perform de nite as well as inde nite integrals involving
symbolic expressions.
fi
fi
Symbolic Integration
❖ Symbolic integration is the process of nding the antiderivative of a
function using symbolic manipulation.
❖ In MATLAB, you can perform symbolic integration using the Symbolic Math
Toolbox.
❖ To perform symbolic integration in MATLAB, you can use the int function.
❖ The int function takes the arguments:
✦ The function to be integrated,
❖ Note that the int function returns a symbolic expression, which can be simpli ed using the
simplify function
%% Example 2
syms x % Declare x as a symbolic variable
f = (x^3+x)^5*(3*x^2+1); % Define the function to be integrated
F = int(f, x); % Integrate f w.r.t. x to get the antiderivative F
disp(F);
G = simplify(F); % Simplify the antiderivative F
disp(G)
fi
Symbolic Indefinite Integration
❖ You can use the di function to check that the antiderivative you have found is correct.
%% Example 3
syms x % Declare x as a symbolic variable
f = (x^3+1); % Define the function to be integrated
F = int(f, x); % Integrate f w.r.t. x to get the antiderivative F
disp(F);
dF=diff(F); % Calculate the derivative of F with respect to x
simplify(dF - f) % Simplify the expression dF - f to check if it is zero
❖ The output of the simplify function should be zero, which indicates that F is indeed an
antiderivative of f.
f
Symbolic Indefinite Integration
❖ You can use the assume function to specify any assumptions about the variables in your
function, such as that they are real, positive, or integer.
❖ These assumptions can help MATLAB simplify the expressions and nd the correct
antiderivative.
%% Example 4
syms x % Declare x as a symbolic variable
assume(x, 'positive') % Assume that x is positive
f = sqrt(x); % Define the function to be integrated
F = int(f, x); % Integrate f w.r.t. x to get the antiderivative F
disp(F);
%% Example 5
syms x t % Declare x and t as a symbolic variables
assume(t>1) % Assume that t is greater than 1.
f = x*exp((1-t)*x); % Define the function to be integrated
F = int(f, x, 0, inf); % Integrate f w.r.t. x to get the antiderivative F
disp(simplify(F));
fi
Area under the Curve (Definite Integration)
❖ You can also use the int function to perform de nite integration (i.e., integration over a speci c
interval) by specifying the interval as the third argument.
❖ For example, to nd the de nite integral of f(x) = x^2 from x=0 to x=1, ( i.e., to calculate the area
under the curve of the function y = x^2 between the limits of x = 0 and x = 1).
%% Example 6
syms x % Declare x as a symbolic variable
f = x^2; % Define the function to be integrated
a = 0; % Lower limit of integration
b = 1; % Upper limit of integration
I = int(f, x, a, b); % Integrate f with respect to x from a to b
disp(I); % Area under the curve
fi
fi
fi
fi
Area under the Curve
%% Example 7
%% Example 8
% Prompt the user to enter the upper and lower curves as functions of x
y1 = input('Enter the first curve as a function of x: '); % e.g., x^2 - 2*x
y2 = input('Enter the second curve as a function of x: '); % e.g., x
% Define the x-axis limits for the plot, slightly extending beyond the intersection points
D = [min(t) - 0.2, max(t) + 0.2];
% Plot the first curve (y1) in red over the defined x-axis limits
fplot(y1, D, 'r', 'LineWidth', 2);
hold on
% Plot the second curve (y2) in blue over the defined x-axis limits
fplot(y2, D, 'b', 'LineWidth', 2);
grid on
Area between Two Curves (Cont.)
% Initialize total area to zero
TA = 0;
% If there are more than two intersection points, calculate the area between each pair of points
for i = 1:length(t)-1
% Calculate the area between the curves from t(i) to t(i+1)
A = int(y1 - y2, t(i), t(i+1));
% Add the absolute value of the area to the total area
TA = TA + abs(A);
2. Find the area of the regions enclosed by the curves y=x^4-4*x^2+4 and y=x^2.
fi