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

MATLAB Examples - Numerical Integration

The document discusses numerical integration in MATLAB. It introduces the concept of approximating the area under a curve by dividing it into rectangles and summing their areas. As an example, it calculates the integral of y=x^2 from 0 to 1 using the trapezoid rule in MATLAB. It then demonstrates using built-in functions like quad() and quadl() for numerical integration. Finally, it shows how to calculate the integral of a polynomial function from -1 to 1 using the polyint() function.

Uploaded by

Danh Bich Do
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)
129 views

MATLAB Examples - Numerical Integration

The document discusses numerical integration in MATLAB. It introduces the concept of approximating the area under a curve by dividing it into rectangles and summing their areas. As an example, it calculates the integral of y=x^2 from 0 to 1 using the trapezoid rule in MATLAB. It then demonstrates using built-in functions like quad() and quadl() for numerical integration. Finally, it shows how to calculate the integral of a polynomial function from -1 to 1 using the polyint() function.

Uploaded by

Danh Bich Do
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/ 17

MATLAB

Examples
Numerical Integration

Hans-Petter Halvorsen
Integration

The integral of a function 𝑓(𝑥) is denoted as:

'
% 𝑓 𝑥 𝑑𝑥
(
Integration
Given the function:
𝑦 = 𝑥+

We know that the exact solution is:


( -
𝑎
% 𝑥 + 𝑑𝑥 =
/ 3
The integral from 0 to 1 is:
1
1
+
% 𝑥 𝑑𝑥 = ≈ 0.3333
/ 3
Numerical Integration

An integral can be seen as the area under a curve.


Given 𝑦 = 𝑓(𝑥) the approximation of the Area (𝐴) under the curve can be found dividing the area up into
rectangles and then summing the contribution from all the rectangles (trapezoid rule):
Example: Numerical Integration
We know that the exact solution is:

We use MATLAB (trapezoid rule):


x=0:0.1:1;
y=x.^2;
plot(x,y)

% Calculate the Integral:


avg_y=y(1:length(x)-1)+diff(y)/2;
A=sum(diff(x).*avg_y)

A = 0.3350
Students: Try this example
Example: Numerical Integration
We know that the exact solution is:

In MATLAB we have several built-in functions we can use for numerical integration:
clear
clc
close all

x=0:0.1:1;
y=x.^2;

plot(x,y)

% Calculate the Integral (Trapezoid method):


avg_y = y(1:length(x)-1) + diff(y)/2;
A = sum(diff(x).*avg_y)

% Calculate the Integral (Simpson method):


A = quad('x.^2', 0,1)

% Calculate the Integral (Lobatto method):


A = quadl('x.^2', 0,1)
Numerical Integration
Given the following equation:

𝑦 = 𝑥 - + 2𝑥 + − 𝑥 + 3

• We will find the integral of y with respect to x, evaluated from -1


to 1
• We will use the built-in MATLAB functions diff(), quad() and
quadl()
Numerical Integration – Exact Solution
The exact solution is:
'
' : - +
- +
𝑥 2𝑥 𝑥
𝐼 = % (𝑥 + 2𝑥 − 𝑥 + 3)𝑑𝑥 = + − + 3𝑥 <
( 4 3 2
(
1 : 2 - 1 +
= 𝑏 − 𝑎 + 𝑏 − 𝑎 − 𝑏 − 𝑎+ + 3(𝑏 − 𝑎)
: -
4 3 2

𝑎 = −1 and 𝑏 = 1 gives:
1 2 1 22
𝐼 = 1−1 + 1+1 − 1−1 +3 1+1 =
4 3 2 3
Symbolic Math Toolbox
We start by finding the Integral using the Symbolic Math Toolbox:
clear, clc

syms f(x)
syms x

f(x) = x^3 + 2*x^2 -x +3

I = int(f)

This gives: I(x) = x^4/4 + (2*x^3)/3 - x^2/2 + 3*x

http://se.mathworks.com/help/symbolic/getting-started-with-symbolic-math-toolbox.html
Symbolic Math Toolbox
The Integral from a to b:
clear, clc
syms f(x)
syms x
f(x) = x^3 + 2*x^2 -x +3

a = -1;
b = 1;
Iab = int(f, a, b)

This gives: Iab = 22/3 ≈ 7.33

http://se.mathworks.com/help/symbolic/getting-started-with-symbolic-math-toolbox.html
clear, clc

x = -1:0.1:1;

y = myfunc(x);

plot(x,y)

% Exact Solution
a = -1;
b = 1;
Iab = 1/4*(b^4-a^4 )+2/3*(b^3-a^3 )-
1/2*(b^2-a^2 )+3*(b-a)

% Method 1
avg_y = y(1:length(x)-1) + diff(y)/2;
A1 = sum(diff(x).*avg_y)
MATLAB gives the following results:
% Method 2
A2 = quad(@myfunc, -1,1) Iab = 7.3333
A1 = 7.3400
% Method 3 A2 = 7.3333
A3 = quadl(@myfunc, -1,1)
A3 = 7.3333
Integration on Polynomials
Given the following equation:

𝑦 = 𝑥 - + 2𝑥 + − 𝑥 + 3

Which is also a polynomial. A polynomial can be written on the


following general form: 𝑦 𝑥 = 𝑎/ 𝑥 @ + 𝑎1 𝑥 @A1 + ⋯ + 𝑎@A1 𝑥 + 𝑎@

• We will find the integral of y with respect to x, evaluated from -1 to 1


• We will use the polyint() function in MATLAB
clear The solution is:
ans =
clc 0.2500 0.6667 -0.5000 3.0000 0

p = [1 2 -1 3]; The solution is a new polynomial:


[0.25, 0.67, -0.5, 3, 0]

polyint(p) Which can be written like this:

0.25𝑥 : + 0.67𝑥 - − 0.5𝑥 + + 3𝑥

We know from a example that the exact solution is:

'
' : - +
- +
𝑥 2𝑥 𝑥
% (𝑥 + 2𝑥 − 𝑥 + 3)𝑑𝑥 = + − + 3𝑥 <
( 4 3 2
(

→ So wee se the answer is correct (as expected).


Hans-Petter Halvorsen, M.Sc.

University College of Southeast Norway


www.usn.no

E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/

You might also like