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

Lecture 3 - Solving Equations, Curve Fitting, and Numerical Techniques

This document summarizes an engineering software lecture on solving equations and curve fitting. It covers linear algebra concepts like solving systems of linear equations using matrices. It also discusses polynomials, including representing polynomials in MATLAB, evaluating polynomials at points, finding polynomial roots, and polynomial operations like addition, multiplication and division. The document concludes with an overview of polynomial curve fitting in MATLAB.

Uploaded by

Hezron gibron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture 3 - Solving Equations, Curve Fitting, and Numerical Techniques

This document summarizes an engineering software lecture on solving equations and curve fitting. It covers linear algebra concepts like solving systems of linear equations using matrices. It also discusses polynomials, including representing polynomials in MATLAB, evaluating polynomials at points, finding polynomial roots, and polynomial operations like addition, multiplication and division. The document concludes with an overview of polynomial curve fitting in MATLAB.

Uploaded by

Hezron gibron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

ENGINEERING SOFTWARE

EE 8207
LECTURE 3:

Solving Equations and Curve


Fitting

Instructor: E.Tarimo Thursday, December 22, 2022

2022/2023
OUTLINE

 Linear Algebra

 Polynomials

 Optimization

Instructor: E.Tarimo Solving Equations 2022/2023


OUTLINE

Linear Algebra
Polynomials

Optimization

Instructor: E.Tarimo Solving Equations 2022/2023


SYSTEMS OF LINEAR EQUATIONS
 Given a system of linear equations
x + 2y - 3z = 5
-3x – y + z = -8
x–y+z=0
 Construct matrices so the system is described by Ax=b
» A = [1 2 -3;-3 -1 1;1 -1 1];
» b = [5;-8;0];
 And solve with a single line of code!
» x = A\b;
➢ x is a 3x1 vector containing the values of x, y, and z

Instructor: E.Tarimo Solving Equations 2022/2023


WORKED EXAMPLE: LINEAR ALGEBRA
 Solve the following systems of equations:

Instructor: E.Tarimo Solving Equations 2022/2023


MORE LINEAR ALGEBRA
 Given a matrix
» mat = [1 2 -3;-3 -1 1;1 -1 1];
 Calculate the rank of a matrix
» r = rank(mat);
➢the number of linearly independent rows or columns
 Calculate the determinant
» d = det(mat);
➢mat must be square; matrix invertible if det nonzero
 Get the matrix inverse
» E = inv(mat);
➢if an equation is of the form A*x = b with A as square
matrix, x = A\b is (mostly) the same as x = inv(A)*b
 Get the condition number
» c = cond (mat); (or its reciprocal: c = rcond(mat);)
➢if condition number is large, when solving A*x = b,
small errors in b can lead to large errors in x (optimal c==1)
Instructor: E.Tarimo Solving Equations 2022/2023
OUTLINE

Linear Algebra
Polynomials

Optimization

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS
This part covers the following concepts:

 Polynomials in engineering.

 Matlab numeric tools for polynomials.

 Matlab symbolic tools for polynomial and other


analytic functions.

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING
 Polynomials play an important role in the analysis
and design of engineering systems.

 In various fields, the mathematical model for


electrical, electronics, mechanical, biomedical,
chemical, industrial, civil, etc. systems can be
represented by an 𝑛th order linear differential equation
of the form:

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING

Where
 𝑦(𝑡) represents the response (output) of the system at time
𝑡, and 𝑓(𝑡) is the forcing function (input) that drives the
system.
 The 𝑎𝑖 and 𝑏𝑖 coefficients are real constants.
 The solution for 𝑦(𝑡) is the sum of a natural response (𝑦𝑛𝑎𝑡)
and a forced response (𝑦𝑓)

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING
 Analytical methods are available to systematically solve the
above differential equation.
 The forced response (generally) takes the form of the input
𝑓(𝑡), and its derivatives.
 On the other hand, the natural response has the analytical
form:

 𝑥𝑖 called natural frequency; in electrical engineering texts is


denoted by 𝑠𝑖
 𝑥𝑖 is one of the 𝑛 roots (here, assumed to be distinct) of the
characteristic polynomial.
Instructor: E.Tarimo Solving Equations 2022/2023
POLYNOMIALS IN ENGINEERING
 The characteristic polynomial can be determined (by
inspection) from the left hand side of the differential
𝑑𝑛 𝑦
equation by substituting 𝑥𝑛 for 𝑑𝑡𝑛
and substituting 1

for 𝑦, as follows:

 The roots of 𝑝(𝑥) are the solutions to the equation

𝑝(𝑥) = 0

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING
 Electric circuit model for a car’s ignition system

 Circuit model:

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING
 Writing KVL around the loop,

di 1
v = iR + L +
dt C
idt 
 Differentiate wrt to time and re-arrange

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS IN ENGINEERING
 Mathematical model of the discharge cycle:

 Where 𝑖𝐿 is the circuit current.

 The characteristic polynomial is given by,

 The natural frequencies of the circuit are the solutions


to;

 and are given by,

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS
 Many functions can be well described by a high-order
polynomial

 MATLAB represents a polynomials by a vector of


coefficients

 If vector P describes a polynomial

 P is a vector of length N+1 describing an N-th order


polynomial

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIAL OPERATIONS
 To get the roots of a polynomial
» r = roots (P)
➢r is a vector of length N
 Can also get the polynomial from the roots
» P = poly (r)
➢r is a vector length N
 To evaluate a polynomial at a point
» y0 = polyval (P,x0)
➢x0 is a single value; y0 is a single value
 To evaluate a polynomial at many points
» y = polyval(P,x)
➢x is a vector; y is a vector of the same size

Instructor: E.Tarimo Solving Equations 2022/2023


Polynomials
➢ Polynomials

❖ x2 + 2x – 7

❖ x4 + 3x3 -15x2 - 2x + 9

➢ In MATLAB polynomials are created by row vector i.e.

❖ s4 + 3s3 - 15s2 - 2s + 9

>>p = [ 1 3 -15 -2 9];

❖ 3x3 - 9

>>q = [3 0 0 -9]; %write the coefficients of every term

➢ Polynomial evaluation : polyval (y, x)

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS EVALUATION
Exp: Evaluate the value of polynomial y = 2x2 + 3x + 4 at x =
[1, -3]
>> y = [2 3 4];
>> x = 1;
>> value = polyval (y, x)
>> value = 9
>> x = -3;
>> value = polyval(y, x)
>> value = 13
Similarly
>> x = [1 -3];
>> value = polyval(y, s)
value = 9 13
OR
>> value = polyval(y,[1 -3])
Instructor: E.Tarimo Solving Equations 2022/2023
ROOTS OF POLYNOMIALS
Roots of polynomials: roots (p)
>> p = [1 3 2]; % p = s2 + 3s + 2
>> r = roots (p)
r = -2 -1

Try this: find the roots of s4 + 3s3 - 15s2 - 2s + 9 = 0

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIALS MATHEMATICS
 Addition
>> a = [0 1 2 1]; % s2 + 2s + 1
>> b = [1 0 1 5]; % s3 + s + 1
>> c = a + b; % s3 + s2 + 3s + 6

 Subtraction
>> a = [3 0 0 2]; % s3 + 2
>> b = [0 0 1 7]; %s+7
>> c = b - a % -s3 + s + 5
c=
-3 0 1 5
Instructor: E.Tarimo Solving Equations 2022/2023
POLYNOMIALS MATHEMATICS
 Multiplication : Multiplication is done by convolution
operation .
Syntax z = conv(x, y)
>> a = [1 2 ]; %s+2
>> b = [1 4 8 ]; % s2 + 4s + 8
>> c = conv (a, b) % s3 + 6s2 + 16s + 16
c=1 6 16 16
Try this: find the product of (s+3), (s+6) & (s+2). Hint: two at
a time

 Division : Division is done by deconvolution operation.


Syntax [z, r] = deconv(x, y)
Where
x = divident vector y = divisor vector
z = Quotients vector r = remainders vector
Instructor: E.Tarimo Solving Equations 2022/2023
POLYNOMIALS MATHEMATICS
>> a = [1 6 16 16]; % a = s3 + 6s2 +16s + 16

>> b = [1 4 8]; % b = s2 + 4s + 8

>> [c, r] = deconv (a, b)

c=2

r=0 0 0 0

Try this: divide s2 - 1 by s + 1

Instructor: E.Tarimo Solving Equations 2022/2023


FORMULATION OF POLYNOMIALS
 Making polynomial from given roots:
>> r = [-1 -2]; % Roots of polynomial are -1 & -2
>> p = poly(r); % p = s2 + 3s + 2
p=3 2

Instructor: E.Tarimo Solving Equations 2022/2023


POLYNOMIAL FITTING
 MATLAB makes it very easy to fit polynomials to data
 Given data vectors X = [-1 0 2] and Y = [0 -1 3]
» p2 = polyfit(X,Y,2);
 Finds the best (least-squares sense) second-order
polynomial that fits the point(-1,0), (0,-1), and (2,3)
 see help polyfit for more information
» plot(X,Y);
» hold on;
» x = -3 : .01 : 3;
» plot(x, polyval(p2,x));
Instructor: E.Tarimo Solving Equations 2022/2023
EXERCISE: POLYNOMIAL FITTING

 Evaluate y = x2 for x = -4:0.1:4.

 Add random noise to these samples. Use randn. Plot


the noisy signal with . markers

 Fit a 2nd degree polynomial to the noisy data

 Plot the fitted polynomial on the same plot, using the


same x values and a red line.

Instructor: E.Tarimo Solving Equations 2022/2023


OUTLINE

Linear Algebra
Polynomials

Optimization

Instructor: E.Tarimo Solving Equations 2022/2023


NONLINEAR ROOT FINDING
 Many real-world problems require us to solve f(x)=0
 Can use fzero to calculate roots for any arbitrary
function
 fzero needs a function passed to it.

 We will see this more and more as we delve into solving


equations.

Instructor: E.Tarimo Solving Equations 2022/2023


MINIMIZING A FUNCTION
fminbnd: minimizing a function over a bounded
interval
» x=fminbnd(fun, x1, x2);
➢fun takes a scalar input and returns a scalar output
➢fun(x) will be the minimum of fun for x1 ≤ x ≤ x2

Instructor: E.Tarimo Solving Equations 2022/2023


MINIMIZING A FUNCTION
fminsearch: unconstrained interval
» x=fminsearch(fun, x0)
➢finds the local minimum of fun starting at x = x0

 Maximize g(x) by minimizing f(x) = -g(x)


 Solutions may be local!

Instructor: E.Tarimo Solving Equations 2022/2023


OPTIMIZATION TOOLBOX
 If you are familiar with optimization methods, use the
optimization toolbox
 Useful for larger, more structured optimization
problems
 Sample functions (see help for more info)

» linprog
➢linear programming using interior point methods
» quadprog
➢quadratic programming solver
» fmincon
➢constrained nonlinear optimization
Instructor: E.Tarimo Solving Equations 2022/2023
EXERCISE: MIN-FINDING

 Find the minimum of the function

over the range–πtoπ. Use fminbnd.

 Plot the function on this range to check that this is the


minimum.

Instructor: E.Tarimo Solving Equations 2022/2023


A WORD OF CAUTION
 MATLAB knows no fear!
 Give it a function, it optimizes/differentiates/
integrates
➢That’s great! It’s so powerful!
 Numerical techniques are powerful but not magic

 Beware of over trusting the solution!

➢You will get an answer, but it may not be what


you want
 Analytical forms may give more intuition

➢Symbolic Math Toolbox

Instructor: E.Tarimo Solving Equations 2022/2023

You might also like