0% found this document useful (0 votes)
73 views31 pages

Chapter 7 PDF

This document discusses numerical methods for calculus and differential equations. It covers numerical integration techniques like the trapezoidal rule and Simpson's rule. It also discusses numerical differentiation methods like forward, backward, and central differences. Additionally, it discusses solving ordinary differential equations numerically using solvers like ode45 in MATLAB. Examples are provided for computing integrals, finding derivatives, and solving first order differential equations.

Uploaded by

pranay reddy
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)
73 views31 pages

Chapter 7 PDF

This document discusses numerical methods for calculus and differential equations. It covers numerical integration techniques like the trapezoidal rule and Simpson's rule. It also discusses numerical differentiation methods like forward, backward, and central differences. Additionally, it discusses solving ordinary differential equations numerically using solvers like ode45 in MATLAB. Examples are provided for computing integrals, finding derivatives, and solving first order differential equations.

Uploaded by

pranay reddy
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/ 31

Computational Methods and Modeling for

Engineering Applications
(GENG-8030)

Department of Electrical and Computer Engineering,


University of Windsor, ON, Canada,

By:
Dr. Mohammad Sedigh Toulabi

Lecture number:
(8)

Winter 2023
Numerical methods for calculus and differential equations:

Integral

Integral of f(x) interpreted as the area A under the curve of f(x) from x a
to x b

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Integral:

Illustration of (a) rectangular and (b) trapezoidal numerical integration.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Numerical integration functions:

Command Description
integral(fun,a,b) Uses an adaptive Simpson’s rule to compute
the integral of the function whose handle is
fun, with ‘a’ as the lower integration limit and
‘b’ as the upper limit.

trapz(x,y) Uses trapezoidal integration to compute the


integral of y with respect to x, where the array
y contains the function values at the points
contained in the array x.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Numerical integration functions:
Although the integral function is more accurate than trapz, it is restricted to
computing the integrals of functions and cannot be used when the integrand
is specified by a set of points. For such cases, use the trapz function.

Using the trapz function. Compute the integral


 sin x dx
0
First use 10 panels with equal widths of /10. The script file is

The answer is 1.9797, which gives a relative error of 100 ((2 1.9797)/2)
1%.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Numerical integration functions:
Script to integrate 2 from 0 to , create the function:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Polynomial integration:

q = polyint(p,C) returns a polynomial q representing the integral of


polynomial p with a user-specified scalar constant of integration C.

For example, the integral of is obtained from


.

Which corresponds to .

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Double integrals:
A = integral2(fun, a, b, c, d) computes the integral of from x a to
b, and y c to d.

Here is an example using an anonymous function to represent


.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Triple integrals:
computes the triple integral of
from to to , and to .

Here is an example using an anonymous function to represent.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Question:
Q1-A tank having vertical sides and a bottom area of 100 ft2 is used to store
water. The tank is initially empty. To fill the tank, water is pumped into the
top at the rate given in the following table. Determine the water height h(t)
at t = 10 min.
Time (min) 0 1 2 3 4 5 6 7 8 9 10

Flow rate (ft3/min) 0 80 130 150 150 160 165 170 160 140 120

The relation between height h and the volume inflow rate r is

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Question:
Q2-An accelerometer is used in aircraft, rockets, and other vehicles to
estimate the vehicle’s velocity and displacement. The accelerometer
integrates the acceleration signal to produce an estimate of the velocity, and
it integrates the velocity estimate to produce an estimate of displacement.
Suppose the vehicle starts from rest at time t = 0, and its measured
acceleration is given in the following table.
0 1 2 3 4 5 6 7 8 9 10
0 2 4 9 16 17 24 32 41 48 51
The estimated velocity v after 10s is.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Question:
Q3-Use the integral function to compute the integral

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Numerical differentiation:
Illustration of three methods for estimating the derivative .

A backward difference
B forward difference
C central difference

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Numerical differentiation:
MATLAB provides the diff function to use for computing derivative
estimates. Its syntax is d = diff(x), where x is a vector of values, and the
result is a vector d containing the differences between adjacent elements in
x.

That is, if has n elements, d will have elements, where

For example, if x = [5, 7, 12, -20], then diff(x) returns the vector [2, 5, -32].

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Polynomial differentiation functions:

Command Description
b = polyder(p) Returns a vector b containing the coefficients
of the derivative of the polynomial
represented by the vector p.

b = polyder(p1,p2) Returns a vector b containing the coefficients


of the polynomial that is the derivative of
(p1)(p2).

[num, den] = polyder(p2,p1) Returns the vectors num and den containing
the coefficients of the numerator and
denominator polynomials of the derivative
p2/p1

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Polynomial differentiation functions:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Solving first order differential equations:
An ordinary differential equation (ODE) is an equation containing
ordinary derivatives of the dependent variable. An equation containing
partial derivatives with respect to two or more independent variables is a
partial differential equation (PDE). Solution methods for PDEs are an
advanced topic, and we will not treat them in this text.

The Euler method is the simplest algorithm for numerical solution of a


differential equation.

MATLAB provides several functions, called solvers, such as ode45 and


ode15s functions to solve ODE.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Solving first order differential equations:
ode45 is used as follows to solve

Syntax is: [t,y] = ode45(@ydot,tspan,y0)

where @ydot is the handle of the function file whose inputs must be and ,
and whose output must be a column vector representing ; that is,
. The number of rows in this column vector must equal the order of
the equation.

• The array tspan contains the starting and ending values of the independent
variable t, and optionally any intermediate values.

• The array y0 contains the initial values of . If the equation is first order,
then y0 is a scalar.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Response of an RC circuit:
The circuit model for zero input voltage v is

Analytical solution :

1.5

0.5

0
0 0.1 0.2 0.3 0.4 0.5
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Nonlinear example:
The equation for the height is

dh 0.0344 h

dt 10h  h 2

Draining of a
spherical tank
10

-2
0 500 1000 1500 2000 2500
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Extension to higher order equations:
To use the ODE solvers to solve an equation higher than order 2, you must
first write the equation as a set of first-order equations.
For example, consider the equation

5 
y  7 y  4 y  sin(t )

Define and . Then the above equation can be expressed as


two equations:

x1  x2
1 4 7
x2  sin(t )  x1  x2
5 5 5
This form is sometimes called the Cauchy form or the state-variable form.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Extension to higher order equations:
x1  x2
1 4 7
x2  sin(t )  x1  x2
5 5 5
Suppose we want to solve the equation for 0
t 6 with the initial conditions x1(0) 3,
x2(0) 9.

10

4 Plot of x2
2

-2

-4
0 1 2 3 4 5 6

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Pendulum example:

The pendulum shown consists of a


concentrated mass m attached to a rod
whose mass is small compared to m. The
rod’s length is L. The equation of motion for
this pendulum is

Suppose that and .


Use MATLAB to solve this equation for
i rad.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Pendulum example:

1.5

0.5

-0.5

-1

-1.5

-2
0 1 2 3 4 5

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
A mass and spring:
A mass and spring with viscous surface friction. Its equation of motion is

my  cy  ky  f (t )

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
A mass and spring:
The equation of motion can be put into the state
variable form. Assuming and we have

These can be put into matrix form as shown below.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
A mass and spring:

Plot of x1
For and
The equations can be solved and the
solution plotted follows.
Plot of x2

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q4-Use MATLAB to evaluate the following double integral:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Question:
Q5- Use MATLAB to evaluate the following triple integral:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Question:
Q6- Use MATLAB to compute and plot the solution of the following
equation.

at is around:

(correct) 21

20

19

18

17

16

15
0 10 20 30 40 50 60 70

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q7- Plot the position and velocity of a mass with a spring and damping,
having the parameter values , , and . The applied force is
, the initial position is , and the initial velocity is
.
The displacement at is

(correct) 6

5.5

4.5

3.5

2.5

1.5
0 1 2 3 4 5 6

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31

You might also like