0% found this document useful (0 votes)
39 views4 pages

Example:: We Know That The Exact Solution Is

The document discusses numerical integration in MATLAB. It provides an example of using the trapezoid rule to calculate the integral of x^2 from 0 to 1, getting a value of 0.3350. It then shows how to use MATLAB's built-in quad() and quadl() functions to calculate the integral using Simpson's method and Lobatto's method respectively. Finally, it presents setting up calculating the integral of an equation from -1 to 1 using diff(), quad() and quadl().

Uploaded by

Papy Maika
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)
39 views4 pages

Example:: We Know That The Exact Solution Is

The document discusses numerical integration in MATLAB. It provides an example of using the trapezoid rule to calculate the integral of x^2 from 0 to 1, getting a value of 0.3350. It then shows how to use MATLAB's built-in quad() and quadl() functions to calculate the integral using Simpson's method and Lobatto's method respectively. Finally, it presents setting up calculating the integral of an equation from -1 to 1 using diff(), quad() and quadl().

Uploaded by

Papy Maika
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/ 4

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()

You might also like