lab 10

.pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

130

Subject

Mechanical Engineering

Date

Feb 20, 2024

Type

pdf

Pages

11

Uploaded by PrivateRook16679

Report
Lab 10: Solving Nonlinear Equations |l © Matthew Leineweber, PhD BME 130 - Numerical Methods in Biomedical Engineering San Jose State University Instructor:Prof. Abdulmelik Mohammed Lab TA: Nihar Prakash, Ammar Babiker By: Philippe Moffet, Lab Group #: 03, Year: Fall 2023 Table of Contents Introduction Anonymous Functions Activity 1 Activity 2 Newton Raphson Method Activity 3 Activity 4 Using MATLAB's fzero to Solve Nonlinear Equations Activity 5 Activity 6 Using fsolve Activity 7 Post Lab References and Documentation Function Definitions Newton Raphson Function (to be completed by student) Introduction So far we have been exploring “bracketing methods” for solving nonlinear equations, namely the method of False Position (a.k.a Interpolation) and the Bisection method. These techniques are so called because they converge on a solution by successively iterating a pair of boundary points that lie on either side of the root until the range between the boundaries is negligibly small. Of these two methods, the False Position technique will typically converge faster than the Bisection method for functions with steep slopes near the root, but may take significantly longer if the slope is very shallow. We also saw how we can use function handles to pass functions as input arguments into other functions. This capability becomes extremely useful trying to iteratively solve equations, since it allows us to write a single solver function (e.g. FalsePosition2) to be used with any expression to be solved. However, defining standalone function files or sub-functions can be somewhat cumbersome when all our function needs to do is evaluate a single mathematical expression. Luckily, MATLAB allows us to define anonymous functions that can streamline this process allow us to define simple functions on the fly.
In today’s lab we will build off the foundation we laid last week to introduce a new technique for solving nonlinear equations, and show how we can use anonymous functions to simplify the solving process. This new solver, the Newton Raphson method, will often converge much faster than our previous methods (i.e. require fewer iterations). We will also explore how we can use some of MATLAB'’s pre-built functions to solve equations as well. Anonymous Functions We've already seen how writing functions and sub-functions makes our lives much easier by removing the need to customize every section of code to tailor a specific problem. However, if we only want our function to perform one or two steps, such as evaluating the result of a mathematical expression, writing an entire function m-file or sub-function can be tedious a little cumbersome. Enter the anonymous function. Anonymous functions can accept inputs and return outputs, like a regular function, but they contain only one or two executable lines of code, and can be defined at the command line, in a script, or within another user-defined function. These special functions are defined with the format: fun = @(inputArguments) expr Here, fun is the name of the anonymous function, inputArguments is the list of input arguments, and expr is the single line of code (expression) for the function. In this anonymous function definition, the ‘@ symbol tells MATLAB that fun will be a function with input variable: inputArguments. You may recognize the use of the ‘@ symbol from our discussion of function handles last week. Anonymous functions basically combine a function definition and a function handle into a single statement. For example, if we were to write an anonymous function that takes an input xand returns its cube, x3, we would write: cube = @ (x) x"3 cube = function_handle with value: @(x)x"3 Once an anonymous function is defined, it can be used to evaluate the function when the input variable is assigned a specific value: cube(2) ans = 8 Which is just the value evaluated at x = 3. As shown above, to call (use) an anonymous function, simply type the name of the anonymous function, followed by the value for the input variable in parentheses. If you wish to assign an array to the input variable rather than a single scalar value, you must first define the anonymous function with element-by-element powers instead: n = I1x3
cube = @ (x) x.”3 cube = function _handle with value: @(x)x.”"3 cube(n) ans = 1x3 1 8 27 Activity 1 Define an anonymous function to perform the following calculation f(x) = X +2x+2— 10&3_2’(2 Then use the function to evaluate f(x) for x=5 andfor x=[-1 4 9] % Define the anonymous function x=@(x)x."3+(2*x)+2- (10*exp(-2*x.72)) fx = function_handle with value: @(x)x."3+(2*x)+2-(10*exp(-2*x."2)) Run the function to perform the calculation on the following inputs % Define the x-values to be tested X1l = 5; xr = [-1 4 9]; Assign the result of f(x) to variables f1 and f2, respectively. % Evaluate fx1 = f(x1) and fx2 = f(x2) f1 = fx(x1) f1 = 137 2 = fx(xr) f2 = 1x3 -2.3534 74.0000 749.0000 So far we have explored anonymous functions with only one input variable, however, it's possible to have several input variables (e.g. x, y, z, etc.). To write an anonymous function for the Pythagorean Theorem, which we’ll call hypotenuse, we would have two input variables, a and b, representing the lengths of the sides of a right triangle, and the output will be the length of the hypotenuse. hypotenuse = @ (a,b) sqrt(a”2+b”2) hypotenuse = function handle with value: @(a,b)sgrt(a*2+b”2)
To use multiple input variables in an anonymous function, just separate them by commas in parentheses after the “@”. Let’s evaluate the function for ¢ = 3 and » = 4: hypotenuse(3,4) ans = 5 What if you would like to evaluate several values in a multivariable function? The first step, just like with single variable functions, is to define the anonymous function element-by-element: hypotenuse = @ (a,b) sqrt(a.”2+b.”2) hypotenuse = function_handle with value: @(a,b)sgrt(a.”2+b.”2) Now, let’s find the hypotenuse length for two different triangles, one with sides length 5 and 12, and the other with sides length 7 and 24 hypotenuse([5,7],[12,24]) ans = 1x2 13 25 Where 13 is the output corresponding to the |5, 7] input, and 25 is the output corresponding to the [12,24] input. You'll notice that square brackets were used for each of the two input variables, a and b. It looks confusing that terms from different triangles are grouped together, but in order for MATLAB to compute the function as desired, this format is necessary. Note, that the previous line of code is equivalent to the following set of commands: A= [5,7]; B [12,24]; hypotenuse(A,B) ans = 1x2 13 25 You can also use pre-defined variables in an anonymous function’s mathematical expression, but note that if you assign new values to variables used in your anonymous function, the original values will still be used unless you redefine your anonymous function. Activity 2 Let’s illustrate this last point using the code below. Before running the code, write down what you think the values for output? and output2 should be. % Define Variables 'a' and 'b': a = 3; b = 4; % Define the anonymous function fun = @(x,y) a*x + b*y;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help