Download presentation
Presentation is loading. Please wait.
Published byJeffrey Potter Modified over 6 years ago
1
Introduction to Programming for Mechanical Engineers
Lecture 8.1
2
Things you should have known so far!
Write long programming codes with many conditions and loops. Disband the complicated problems into smaller segments and gear them up to integrate the whole process. You should have built the proper tools to help in tackling most of the problems.
3
Polynomials polynomial MATLAB expression
MATLAB recognizes polynomials when entered as arrays. For instance, x^3 + 3*x^2 -7*x + 10 can be entered as [ ]. By default, MATLAB calculates the order of the polynomial by finding the number of elements of the input array. In this example, the number of elements is 4, so the order is 4-1 = 3, which is x^3. polynomial MATLAB expression p1 = [ ] p2 =[ ] p3 =[ ]
4
Polynomials evaluation: polyval
MATLAB also helps in evaluating the polynomial at certain points. The command polyval(p,x) is useful in evaluating the polynomial (defined by the array of coefficients p) at points given in x (could be single or multiple points). polynomial p Evaluation p1 = [ ] >> polyval(p1,2) ans = 5 p2 = [ ] >> polyval(p2,[1 2 -3]) p3 = [ ] >> polyval(p3,p1)
5
Roots of polynomials: roots
MATLAB also helps in finding the roots of polynomials. The command roots(p) finds all the roots of the polynomial whose coefficients are defined in p. polynomial p Evaluation p1 = [ ] >> r1 = roots(p1) r1 = i i p2 = [ ] >> r2 = roots(p2) r2 = 1.3604 1.1622i i
6
Polynomial of roots: poly
MATLAB also helps in finding the polynomials of roots. The command poly(r) finds the coefficients of the polynomial whose roots are defined in the array r. roots p polynomial r1 = [ i i ] >> p1 = poly(r1) p1 = r2 = [ 1.3604 1.1622i i] >> p2 = poly(r2) p2 =
7
Polynomials addition, subtr., multip., and division
MATLAB is very useful in handling polynomials and performing all kinds of mathematical operations on them. You can add polynomials, subtract, multiply or divide them. Define x = [1 0 3], y = [ ] Operation Command Result Addition Subtraction >> A = y + [0 x] >> B = y - [0 x] A = B = Multiplication >> C = conv(x,y) C = Division >> [D E] = deconv(y,x) D = (D - quotient) E = (E - remainder)
8
Polynomials derivation: polyder
MATLAB can find the derivative of polynomials, product of polynomials, and most importantly the division of polynomials using polyder. Define f1 = [ ]; f2 = [ ] command description Result >> A = polyder(f1) Find the derivative of f1 A = >> B = polyder(f1,f2) Find the derivative of f1*f2 B = >> [NUM DEN] = polyder(f1,f2) Find the derivative of NUM = DEN =
9
Polynomial curve fitting (regression): polyfit
MATLAB has the capability of fitting a function to a set of data points using polyfit. By this, you can find the best function representation for these points. The function could be linear, quadratic, cubic, exponential, logarithmic, power, …etc. As a general rule, if you have a set of n points, you will need a polynomial of order n-1 to pass through all these points. points Description Result x = [1 5] y = [4 -2] Linear polynomial is guaranteed to pass through all points x = [2 -3 0] y = [ ] Quadratic polynomial is guaranteed to pass through all points x = [ ] y = [ ] Cubic polynomial is guaranteed to pass through all points x = [x1 x2 …… xn] y = [y1 y2 …… yn] n-1 polynomial is guaranteed to pass through all points
10
Polynomial curve fitting (regression): polyfit
It is not necessarily always that we need a function that passes through all the points, especially when working with practical problems that generate approximate results that follow some pattern. Say we are studying the relation between the velocity and the stopping distance of a vehicle. The following relation will apply Kinetic – Friction = 0, or 0.5*m*V^2 – μ*m*g*d = 0, so d = 0.5*V^2/(μ*g). We know from physics that the relation is quadratic, but due to uncertainties not all the points will lie on the quadratic polynomial. The method used in this curve fitting is the least square method, which searches for the minimum summation of residuals between the f(xi) and yi. Syntax: p = polyfit(x,y,n) Where x is a vector with the horizontal coordinate of the data points. y is the dependent vector that has the vertical coordinates, and n is the order of the polynomial. p will be a vector that holds the fitted polynomial’s coefficients.
11
Polynomial curve fitting (regression): polyfit
It is easy to fit any polynomial function, but sometimes we need to fit power, exponential, logarithmic,..etc. How to do it? The best option is to write it in the form y = m*x+b. Examples: Function Representation Description p1 = polyfit(log(x),log(y),1) p2 = polyfit(x,log(y),1) p3 = polyfit(log(x),y,1) p4 = polyfit(x,y.^-1,1)
12
Polynomial curve fitting and interpolation: interp1
Sometimes we need to study a point of interest that is not represented in the given points, but we have some information about the points before and after it. Interpolation is useful in studying intermediate points, use interpl. Syntax: yi = interp1(x,y,xi,'method') % xi is a vector of points to be interpolated % yi is the vector of interpolated values Define x = [ ]; y = [ ]; xi = [ ] Method Representation Output 'linear' y1 = interp1(x,y,xi,'linear') y1 = 'spline' y2 = interp1(x,y,xi,'spline') y2 = 'cubic' y3 = interp1(x,y,xi,'cubic') y3 =
13
Plots curve fitting MATLAB has the property of fitting plotted curves as well. You need to plot your results first, and choose from Tools, Basic Fitting. x = [0:0.5:4]; y = [ ];
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.