homework5

pdf

School

University of California, Los Angeles *

*We aren’t endorsed by this school

Course

102A

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

4

Uploaded by ShashvatPatel1234

Report
Stats 102A HW5 Due March 23, 2023 General Notes You will submit a minimum of three files, the core files must conform to the following naming conventions (including capitalization and underscores). 123456789 is a placeholder, please replace these nine digits with your nine-digit Bruin ID. The files you must submit are: 1. 123456789 stats102a hw5.R : An R script file containing all of the functions you wrote for the homework. The first line of your .Rmd file, after loading libraries, should be sourcing this script file . 2. 123456789 stats102a hw5.Rmd : Your markdown file which generates the output file of your submission. 3. 123456789 stats102a hw5.html/pdf : Your output file, either a PDF or an HTML file depending on the output you choose to generate. 4. included image files : You may name these what you choose, but you must include all of the image files you generated for your structured flowcharts, otherwise your file will not knit. One way to attach an image file is to use ![ title ]( Images/example1.png ). 5. Please place all of your files into a single folder named 123456789 stats102a hw5 and compress the folder into 123456789 stats102a hw5.zip. If you fail to submit any of the required core files you will receive ZERO points for the assignment. If you submit any files which do not conform to the specified naming convention, you will receive (at most) half credit for the assignment. Your .Rmd file must knit. If your .Rmd file does not knit you will receive (at most) half credit for the assignment. The two most common reason files fail to knit are because of workspace/directory structure issues and because of missing include files. To remedy the first, ensure all of the file paths in your document are relative paths pointing at the current working directory. To remedy the second, simply make sure you upload any and all files you source or include in your .Rmd file. Your coding should adhere to the tidyverse style guide: https://style.tidyverse.org/ . All pseudocode or flowcharts should be done on separate sheets of paper, but be included, inline as images, in your final markdown document. Any functions/classes you write should have the corresponding comments as the following format. my function = function(x, y, ...) { #A short description of the function #Args: #x: Variable type and dimension #y: Variable type and dimension #Return: #Variable type and dimension Your codes begin here } 1
Stats 102A HW5 Due March 23, 2023 NOTE: Everything you need to do this assignment is here, in your class notes, or was covered in discussion or lecture. DO NOT look for solutions online. DO NOT collaborate with anyone inside (or outside) of this class. Work INDEPENDENTLY on this assignment. EVERYTHING you submit MUST be 100% your, original, work product. Any student suspected of plagiarizing, in whole or in part, any portion of this assignment, will be immediately referred to the Dean of Student’s office without warning. Homework Requirements 1. For those main functions (those written in bold letters), please provide flowchart OR algorithms sufficiently complete, clear, and concise enough to enable a person to accurately implement the function in any programming language they are adept with using. 2. Write the function(s) which accurately implements the algorithm(s) as described or requested. 3. Include the error-handling to ensure your function(s) work properly. 1: Root-Finding Problem This problem accounts for 60% of this assignment. (1) Write R code using the Bisection method to solve the following equations within an error of 10 8 . (a) x 3 + 23 = 0 (b) x x 18 = 0 (c) e x 2 1 10 = 0 Note: For all three cases, please state your starting interval and the estimated number of iterations required. (2) Please write R code using the Fixed point iteration method to solve the equation (b). Explain your algorithm prior to the code. Note: You may answer and write code for above two questions directly on Rmd file. (3) Use the Newton’s method to compute the square root by using only simple arithmetic operations 1 . (a) Please write a function, get sqrt() , that computes the square root of an arbitrary positive number. The function should take four arguments, the last three have default values: 1 +, -, / and *. 2
Stats 102A HW5 Due March 23, 2023 a a non-negative number to compute the square root of. tol determines when you consider two iterates to be equal. iter max gives the maximum number of iterations. verbose determines if you want to display intermediate results 2 . (b) Give an example of your function by computing the square root of 5. (c) How would the program change to compute an arbitrary, positive integer, root? Please use calculus to determine an appropriate iterative equation for approximating this arbitrary root. (d) Write an R function, get abroot() , like the one for square root which calculates the arbitrary root of a number. I.e., the root argument is required for the function. (e) Give an example of your function by computing the seventh root of 13. (f) Let e k = | x k 7 13 | , where k indicates the k th iteration. Please print from e 1 to e 4 , and specify your findings. 2: Optimization Problem This problem accounts for 40% of this assignment. (a) Using calculus, find the minimum of the function f ( x ) = x n log( x ), n = 1 , 2 , ... and α > 0 . (b) Using your formulas for the derivatives of f , write down Newton’s method to find the minimum. (c) Use R to write a function, get min() , which takes the following arguments: f an expression to minimize with respect to x x an initial guess ... any additional variables needed by f Choose an appropriate stopping rule and, using your function, return the minimum of f for n = 2 and α = 3 (d) Plot the function and the steps taken by your method starting from x 0 = 1. 2 There is no restriction on this formatting, but please make sure the print of your intermediate results is clear. 3
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
Stats 102A HW5 Due March 23, 2023 Symbolic Differentiation in R R is able to do symbolic differentiation for certain types of functions. One way is to use the stats::D() function . To understand how to use it, we need to introduce expression objects. An expression object, roughly speaking, is an unevaluated representation of a mathematical expression. The expression() function can be used to create expression objects. The syntax inside the expression should be written with correct R syntax in terms of a symbolic dummy variable (like x ). f_expr <- expression(x^2 + exp(-x)) f_expr ## expression(x^2 + exp(-x))} Note that f expr is not a function object in R , but it represents a mathematical function symbolically. The eval() function can be used to evaluate an expression object at a specific value. If the expression object is written as a function of x , then eval() will evaluate the expression using the variable x defined in its calling environment (usually the global environment). For example, to evaluate f expr at the value x = 3, we can type: x <- 3 eval(f_expr) ## [1] 9.049787 The numeric vector x can have more than one value, and the eval() function will be vectorized. x <- 0:4 eval(f_expr) ## [1] 1.000000 1.367879 4.135335 9.049787 16.018316 The D() function in the stats package inputs an expression type object that represents a mathematical function and outputs an expression object corresponding to its derivative. The second required argument name specifies the character/symbol with respect to which the derivative will be computed. With the D() , we can compute the derivative of f ( x ) = x 2 + e x . f_prime <- D(f_expr,name="x") f_prime ## 2 * x - exp(-x) Notice that the output of D() is again an expression object, so D() can be applied iteratively to compute higher order derivatives. Using the eval() function again, we can evaluate the derivative expression f prime in the same way as f expr . eval(f_prime) ## [1] -1.000000 1.632121 3.864665 5.950213 7.981684 Note : Please check the R documentation for more detail about the D() function. 4