es117 - introduction to scientific computing with...
TRANSCRIPT
LECTURE 12: APPLICATIONS IN SCIENCE
AND ENGINEERING
ES117 - Introduction to Scientific Computing
with Matlab
MATLAB Applications
Polynomials
Curve fitting
Linear equations
Interpolation
Polynomials
Polynomial anxn + an-1 xn-1 ... + a1x + a0
(n is the degree of the polynomial)
A polynomial in MATLAB is represented by a row
vector in which the elements are the coefficients.
Example: polynomial y=4x2 + 10x + 3
x=1 y=4+10+3=17
x=2 y=16+20+3=39
x=3 y=36+30+3=69 .......
Polynomials
A polynomial in MATLAB is represented by a row
vector in which the elements are the coefficients.
Example: To define the polynomial y=4x2 + 10x + 3
a = [4 10 3]
a =
4 10 3
Polynomials
To evaluate a polynomial at point x: y= polyval(a,x)
Example: To evaluate the polynomial y=4x2 + 10x + 3 at x=2 a = [4 10 3]
y=polyval(a,2)
y =
39
Polynomials
To find the roots of a polynomial (values for which the polynomial is equal to 0) r=roots(a)
Example: To find the roots of the polynomial 4x2 + 10x + 3 a = [4 10 3]
r=roots(a)
r =
-2.1514
-0.3486
Polynomials
To plot a polynomial x = d:e:f;
y = polyval(a,x);
plot(x,y)
Example: To plot the polynomial 4x2 + 10x + 3 in the interval [0 25] in 1 unit increments a = [4 10 3];
x = 0:25;
y = polyval(a,x);
plot(x,y)
Polynomials
a = [4 10 3];
x = 0:25;
y = polyval(a,x);
plot(x,y)
Curve fitting – Basic fitting
To find an algebraic relationship that best fits a given
set of data
To explore the best possible
fit:
Basic Fitting in the Figure
window’s Tools menu lets
you fit a polynomial curve
(upto 10th order) to your
data.
Curve fitting
Straight line (linear fit)
Step 1: Plot raw data
x=[5 10 20 50 100]
y=[15 33 53 140 301]
plot(x,y,'o')
Curve fitting
Step 2: Use built in Basic Fitting to do a linear fit
Figure window – Tools – Basic Fitting
Curve fitting
Step 3: Fit a linear curve and display the equation
Curve fitting
Comparing different fits
Step 1: Plot raw data
x=0:pi/30:pi/3;
y = sin(x) + rand(size(x))/100;
plot(x,y,'o')
Curve fitting
Step 2: Use Basic Fitting to do a quadratic and cubic
fit.
Curve fitting
Step 2: Use Basic Fitting to do a quadratic and cubic
fit.
Curve fitting
Norm of residuals
for the cubic fit is
lower than that
for quadratic fit
cubic fit should be
chosen.
Curve fitting – Polynomial functions
Given two vectors x and y, the command
a=polyfit(x,y,n) fits a polynomial of order n through
the data points (xi,yi) and returns n+1 coefficients (in
the decreasing order) for the powers of x in the row
vector a.
Curve fitting – Polynomial functions
x=0:5;
y=[0,20,60,68,77,110];
coef=polyfit(x,y,1) first order Output: What is the equation that describes the best fit linear model for this data set? y = 20.8286 x + 3.7619
Curve fitting – Polynomial functions
How can you calculate the y values predicted by this
best fit polynomial for the given x values?
(20.8286x) + 3.7619 a=[20.826 3.7619] y=polyval(a,2) y=20.826x2+3.7619 y= 45.4190
Matrix multiplication
Using (.) means ‘do the operation elementwise’ a*b is matrix multiplication. a.*b is elementwise multiplication. >> a=[2 3;1 5]; b=[0 3;2 0]; >> a*b ans = 6 6 10 3 >> a.*b ans =0 9 2 0
Solving linear equations
Solution of a linear systems of equations
3x + 2y − z = 10
−x + 3y + 2z = 5
x − y − z = −1
If we define the matrix A as
And the vectors x and b as
Solving linear equations
3x + 2y − z = 10
−x + 3y + 2z = 5
x − y − z = −1
A x = b
The solution may then be written as
where A-1 is the matrix inverse of A (the matrix which
gives the identity matrix when multiplied by A)
Solving linear equations
Using matrix division
3x + 2y − z = 10
−x + 3y + 2z = 5
x − y − z = −1
A x = b
x=-2
y=5
z=-6
Solving linear equations
Using matrix division
Interpolation
In some applications we want to estimate a variable’s
value between the data points. This process is called
interpolation.
Suppose we have the following temperature
measurements. The measurements at 8 and 10 A.M.
are missing for some reason, perhaps because of
equipment malfunction.
Interpolation
53 ???
64 ???
Interpolation
Linear interpolation in MATLAB is obtained with the interp1 and
interp2 functions.
For example, the following session produces an estimate of the
temperatures at 8 and 10 A.M. from the preceding data. The
vectors x and y contain the times and temperatures, respectively.
>>x = [7, 9, 11, 12];
>>y = [49, 57, 71, 75];
>>x_int = [8, 10];
>>interp1(x,y,x_int)
ans =
53
64
Interpolation
The values of the independent variable in the vector x (time values in the example) must be in ascending order.
The values in the interpolation vector x_int must lie within the range of the values in x. Thus, we cannot use the interp1 function to estimate the temperature at 6 A.M., for example.
Interpolation
The interp1 function can be used to interpolate in a
table of values by defining y to be a matrix instead of a
vector.
For example, suppose that we now have temperature
measurements at three locations.
Interpolation
We define x as before, but now we de ne y to be a matrix whose three
columns contain the second, third, and fourth columns of the
preceding table. The following session produces an estimate of the
temperatures at 8 and 10 A.M. at each location.
>>x = [7, 9, 11, 12]’;
>>y(:,1) = [49, 57, 71, 75]’;
>>y(:,2) = [52, 60, 73, 79]’;
>>y(:,3) = [54, 61, 75, 81]’;
>>x_int = [8, 10]’;
>>interp1(x,y,x_int)
ans =
53.0000 56.0000 57.5000
64.0000 65.5000 68.0000
The estimated temperatures at 8 A.M. at each location are 53, 56, and 57.5, respectively. At 10 A.M. the estimated temperatures are 64, 65.5, and 68.
Two-Dimensional Interpolation
Now suppose that we have
temperature measurements
at four locations at 7 A.M. These
locations are at the corners of a
rectangle 1 mile wide and 2 mile
long.
The temperature is a function of
two variables, the coordinates x
and y.
Two-Dimensional Interpolation
MATLAB provides the interp2 function to
interpolate functions of two variables.
If the function is written as z f(x,y) and we wish to
estimate the value of z for x=xi and y=yi, the syntax is:
interp2(x,y,z,x_i,y_i)
Two-Dimensional Interpolation
Suppose we want to estimate
the temperature at the point
whose coordinates are (0.6,
1.5). Put the x coordinates in
the vector x and the y
coordinates in the vector y.
Then put the temperature
measurements in a matrix z
such that going across a row
represents an increase in x and
going down a column
represents an increase in
y.
>>x = [0,1];
>>y = [0,2];
>>z = [49,54;53,57]
z =
49 54
53 57
>>interp2(x,y,z,0.6,1.5)
ans =
54.5500