Pre-Engineering 220 Introduction to MatLab ® & Scientific Programming j kiefer



Download 8.69 Mb.
Page86/128
Date02.05.2018
Size8.69 Mb.
#47255
1   ...   82   83   84   85   86   87   88   89   ...   128

MatLab® Sidelight Number One




1. Polynomials

a. Representation



In MatLab®, polynomials are represented by a vector composed of the coefficients. Thusly,

b. Evaluate

A polynomial is evaluated using the command polyval. If p is the vector of coefficients and x is the argument,


polyval(p,x) or y=polyval(p,x)
The roots command gives the roots of a polynomial, as elements of a vector.
r=roots(p)
If the roots of a polynomial are known, then the coefficient vector can be obtained by the poly command.
p=poly(r)
c. Add, multiply, & divide

Polynomials are added by adding the vectors of their coefficients. The shorter vector has to be padded with zeros to make the two vectors the same length.


Multiplication of two polynomials is done with the conv command. c(x) = a(x) * b(x)
c = conv(a,b)
Division is done with the deconv command. u(x)/v(x) = q(x) + r(x)
[q,r] = deconv(u,v)
d. derivatives

k = polyder(p)

k=polyder(a,b)

[n m] = polyder(u,v) followed by [k,r] = deconv(n,m)

2. Curve Fitting & Interpolation

a. Least squares

MatLab® fits data to a polynomial using the least squares method. Fitting an nth degree polynomial to a table of (x,y) points. If the number of data points is m, then n must be m-1 or less, and greater than 0.
p=polyfit(x,y,n)
Fitting to functions other than polynomials is done by rewriting the function in terms of a straight line, for instance by taking the log of both sides, etc.
b. Interactive fitting

In the Tools menu of the Figure Window is a Basic Fitting tool. This can be used to fit a function to data interactively. See section 8.4 in the text.


c. Interpolation

MatLab® has four interpolating techniques built-in. The command is interpl. It estimates



yi = f(xi), given a set of {x,y}.
yi = interpl(x,y,xi,’method’)

The methods available are

‘nearest’ returns the value of the nearest data point

‘linear’ carries out linear interpolation

‘spline’ carries out interpolation using a cubic polynomial based on the data points surrounding the interpolated point

‘pchip’ carries out interpolation using a cubic Hermite polynomial.





Download 8.69 Mb.

Share with your friends:
1   ...   82   83   84   85   86   87   88   89   ...   128




The database is protected by copyright ©ininet.org 2024
send message

    Main page