Lecture Notes 3/4/13 Review of Matlab: Issues that showed up on exam 1 problems with variable names and functions Variable names have some limitations. These are outlined on page 11 of Palm. must begin with a letter can only contain letters, numbers and underscore _ Case Sensitive (A and a are not the same in variable names) no more than 63 characters defining a variable follows a pattern variable name = expression or value trying to do things the other way will cause problems. Functions programming functions are not the same as mathematical functions. In programming a function is a piece of code that we want to easily reuse. This is somewhat similar to a unit operation in Pro/II. For example a pfd in Pro/II might look like this: Here we are sending some streams to a unit operation which produces some output, which goes to another unit operation, which produces some output. Matlab can be thought of in a similar way: Here we are sending variables into functions to produce new variables as outputs. There are both user-defined and built-in functions. User-defined functions are ones we write. Built-in functions are ones like plot(), polyfit(), rref() in Matlab, or average() in Excel. A function in matlab can contain a mathematical functions. I have seen this called a ‘function function’. I suppose if the mathematical function was to calculate friction factors we would have a ‘friction factor function function’ which frankly just makes me think of Fox in Socks by Dr. Suess and we should stop before we make up a tweedle-beatle-puddle-paddle-bottle-battle function. Solving Non-linear Equations Numerically built in functions in matlab: (1 variable only) fzero – finds roots for any function. According to the matlab documentation this uses a combination of bisection, secant and inverse quadratic interpolation methods. roots – finds roots for polynomials. According to the matlab documentations this ‘simply calculates the eigenvalues of the companion matrix’. Algorithms for solving non-linear equations: Bisection, Regula-Falsi, Newton’s (or Newton-Raphson), Secant and FixedPoint Iteration. Newton and Fixed-Point can be extended to systems fairly easily. What follows is a graphical demonstration of what each method does, and a brief summary. Bisection Select a range (points A and B). Find the midpoint. Determine which side the root is on. set the new range repeat until changes in A and B are within our desired tolerance. In this example, the first midpoint becomes our new value for B. The new midpoint would become A, etc. This method is really only set up to work when the line we are working with crosses the x axis. When this is the case, multiplying f(A) and f(B) will result in a negative number. so multiplying f(midpoint) by f(a) and f(b) will tell us which half of our range contains the root. Regula-Falsi Select a range (points a and b) Draw a straight line between a and b. Where this line crosses the x-axis, set a new range endpoint. repeat until changes in A and B are within our desired tolerance. As with bisection, the function we are evaluating must cross the x axis, and we can use the same method to determine which endpoint to shift in order to maintain this condition. In the example I sketched above, point b is shifted. We can interpolate between points a and b to produce a straight line. our new point b is at the intercept of this line. We use this new point to generate a new line, etc. Newton’s Method (also called Newton-Raphson) Pick a point (yes just one with this method) Draw a line tangent to your function (i.e. a line with a slope equal to that of your function at that point, or a line with a slope equal to the value of the derivative of your function at that point.) set a new point where this line intercepts the x axis and f(intercept) repeat until your x values are changing less than your tolerance. Secant Method This is a modification of Newton’s method. It uses 2 points to generate a slope rather than taking a derivative. Pick 2 points (x1 and x2) Straight line between x1 and x2 at intercept of line get x3 straight line between x2 and x3 repeat until change is within tolerance. Fixed Point Iteration 1. Starting with your equation in the form f(x)=0, rewrite as x=g(x). There are very likely multiple options for what g(x) will actually look like. For example: if abs(g’(x))<1, it should converge, pick a g(x) accordingly. 2. plot y=g(x) and y=x. The intersection occurs at the root we are searching for. 3. pick a point 4. draw a line in the x direction from the point to y=x, then in the y direction back to y=g(x), this becomes our new point. 5. repeat step four until the change in x is within tolerance.
© Copyright 2025 Paperzz