0% found this document useful (0 votes)
18 views

Session4 2

Uploaded by

alginbaby989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Session4 2

Uploaded by

alginbaby989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

BMAT101P - CALCULUS LAB

Understanding Integration As Area Under The Curve

Dr. Mohit Kumar


VIT Chennai
Symbolic Integration
❖ MATLAB can integrate a wide variety of functions, including trigonometric
functions, logarithmic functions, and exponential functions.
❖ You can also integrate functions that involve combinations of these
functions such as sin(x)*cos(x) or exp(x)*log(x).
❖ However, not all functions can be integrated symbolically.

❖ 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,

✦ The variable of integration,

✦ Lower and upper limits of integration.


fi
Symbolic Indefinite Integration
%% Example 1
syms x % Declare x as a symbolic variable
f = x^2; % Define the function to be integrated
F = int(f, x); % Integrate f w.r.t x to get the antiderivative F
disp(F);

❖ 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

% Define the function and limits of integration


syms x
y = sin(x);
a = 0;
b = pi/2;
% Find the antiderivative of the function
F = int(y, x);
% Evaluate the antiderivative at the limits of integration
area = double(subs(F, x, b) - subs(F, x, a));
% Display the result
disp(area);
% We can also get this result directly by providing limits of integration
I=int(y, x, a, b);
disp(double(I));
Area between Two Curves
❖ To calculate the area between the curves y1 = x^2 and y2 = x between the limits of x = 0 and x = 1.

%% Example 8

% Define the curves and limits of integration


syms x
y1 = x^2;
y2 = x;
a = 0;
b = 1;
% Calculate the area between the curves
y_diff = y2 - y1;
area = double(int(y_diff, a, b));
% Display the result
disp(area);
Area between Two Curves
%% Example 9
% This script calculates the area between two user-defined curves and plots the curves with the area filled.
clear;
close all;
clc;

% Define symbolic variable x


syms x

% 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

% Find the intersection points of these two curves


t = solve(y1 - y2);

% Convert the intersection points to double precision and sort them


t = sort(double(t));

% 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);

% Generate x-values between the current pair of intersection points


x1 = linspace(t(i), t(i+1));
% Evaluate the curves at these x-values
yy1 = subs(y1, x, x1);
yy2 = subs(y2, x, x1);
% Create vectors for the polygon that represents the area between the curves
xx = [x1, fliplr(x1)];
yy = [yy1, fliplr(yy2)];
% Fill the area between the curves with green color
fill(xx, yy, 'g');
end
% Display the total area between the curves
fprintf('Total Area is %d \n', TA);
Problems:
1. Plot the curves and nd the area under the curves

(i) y=sin(x) in [0, 2*pi].

(ii) y=cos(x) in [-pi/2, pi/2].

(iii) y=e^x+tan(x) in [0, pi/4].

2. Find the area of the regions enclosed by the curves y=x^4-4*x^2+4 and y=x^2.
fi

You might also like