Question
Use R and need the R code for every question.
3. Write a program to use Newtons Method to find the minimum of f(x1,x2)--log(1-ㄨㄧ-x2)-log(x1)-log(T2) Use starting value (0.1,0.1) (a) give the value of z1, 2 where the minimum occurs (b) give the minimum function value, f(xi, 2) (c) give the value of the tolerance level you use (d) give the number of iterations to convergence
0 0
Add a comment Improve this question Transcribed image text
Answer #1

write the equation as an R function:-

func3 <- function(x) {
  -1 * log(1-x1-x2)-log(x1)-log(x2)
}

Plot the function to visualize where the roots may exist:-

curve(func3, col = 'blue', lty = 2, lwd = 2, xlim=c(-5,5), ylim=c(-5,5), ylab='f(x)')
abline(h=0)
abline(v=0)

Now a graph will be made.Select the interval in which roots are present.Suppose the interval is 0.5 and 1.5.Then

uniroot(func3, c(.5, 1.5))
newton.raphson(func3, .5, 1.5)

The numDeriv package is used to compute the derivative f′(x)f′(x), though this could also be done by taking the limit with a sufficiently small h.

newton.raphson <- function(f, a, b, tol = 1e-5, n = 1000) {
  require(numDeriv) # Package for computing f'(x)
  
  x0 <- a # Set start value to supplied lower bound
  k <- n # Initialize for iteration results
  
  # Check the upper and lower bounds to see if approximations result in 0
  fa <- f(a)
  if (fa == 0.0) {
    return(a)
  }
  
  fb <- f(b)
  if (fb == 0.0) {
    return(b)
  }

  for (i in 1:n) {
    dx <- genD(func = f, x = x0)$D[1] # First-order derivative f'(x0)
    x1 <- x0 - (f(x0) / dx) # Calculate next value x1
    k[i] <- x1 # Store x1
    # Once the difference between x0 and x1 becomes sufficiently small, output the results.
    if (abs(x1 - x0) < tol) {
      root.approx <- tail(k, n=1)
      res <- list('root approximation' = root.approx, 'iterations' = k)
      return(res)
    }
    # If Newton-Raphson has not yet reached convergence set x1 as x0 and continue
    x0 <- x1
  }
  print('Too many iterations in method')
}

Description: at a minimum of a function, the derivative is zero.

Newton-Rhapson is a root-finding algorithm, and hence is well-suited to computing zeros of functions.

Edit: We need to find the minimum. The first step of this is to find points where ∂f/x1 and ∂f/x2 are both zero.

Now you have two conditions, so you want to compute:

g(x1,x2)=def⎛⎝⎜⎜∂fx1∂fx2⎞⎠⎟⎟=(00).g(x1,x2)=def(∂f∂x1∂f∂x2)=(00).

This gives you two equations with two unknowns. Now perform Newton-Raphson on gg.

Add a comment
Know the answer?
Add Answer to:
Use R and need the R code for every question. 3. Write a program to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • question 3 please The first 5 questions refer to finding solutions to the equation exp(w) =...

    question 3 please The first 5 questions refer to finding solutions to the equation exp(w) = 3.8 ln(1+x). You will need to write it in the form f(x)-0, and use various root finding methods. 1. (10 pts) Plot the curves y- exp(Vx), and y 3.8 ln(1+x) on the same graph in the range 0 x 6. Read off intervals in which there are roots of the equation exp(k)- 3.8 In(1+x) Now find the roots to 6 decimal places using the...

  • need help with 28,29,30 Write the formula for Newton's method and use the given initial approximation...

    need help with 28,29,30 Write the formula for Newton's method and use the given initial approximation to compute the approximations X1 and x2. Round to six decimal places. 28) f(x) = e-x-ixo = In 4 Use a calculator to compute the first 10 iterations of Newton's method when applied to the function with the given initial approximation. Make a table for the values. Round to six decimal places. 29) f(x) = 3x - cos x; x0 = 1 Use Newton's...

  • Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are...

    Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are the name of the function, name of the function’s derivative function, initial guess, maximum number of iterations, and tolerance for the relative convergence error. The output is the root. Use the problem in Homework #3 to test your function.    Hw 3 that we are pulling from %Newton-Raphson method to find upward velocity of a rocket clear all; clc; u=2200; %m/s m0=160000; %kg q=2680;...

  • **Write In JAVA Please!!** 2. Write a program to apply the Modified Newton's Method Tn-1 to...

    **Write In JAVA Please!!** 2. Write a program to apply the Modified Newton's Method Tn-1 to the equation ()3r2 +4 0 starting with o 3. Use m and 2 and make separate numerical runs. In each case, set the maximum number of iterations to 25, but stop the computation when the backward error, i.e. f(n), is less than 10-12, Print all intermediate points xn and backward errors f(xn). Verify the convergence rates of your numerical solutions in both cases. (For...

  • Using Python please Problem 3 (15 pts): Using the Secant Method, for A>0, (10 pts) Write a program which finds A/m for any positive value m. Note, you need to choose a function f(r) for the Secant...

    Using Python please Problem 3 (15 pts): Using the Secant Method, for A>0, (10 pts) Write a program which finds A/m for any positive value m. Note, you need to choose a function f(r) for the Secant Method whose root is A1 'm. (5 pts) How does your choice of m effect how many iterations your program takes to converge for a given tolerance choice? Plots will help me to understand your thinking here In [11]: #present your program for...

  • Not in C++, only C code please In class, we have studied the bisection method for...

    Not in C++, only C code please In class, we have studied the bisection method for finding a root of an equation. Another method for finding a root, Newton's method, usually converges to a solution even faster than the bisection method, if it converges at all. Newton's method starts with an initial guess for a root, xo, and then generates successive approximate roots X1, X2, .... Xj, Xj+1, .... using the iterative formula: f(x;) X;+1 = x; - f'(x;) Where...

  • Questions: 1. Write a MATLAB program to find all the roots of a given, twice continuously differe...

    Questions: 1. Write a MATLAB program to find all the roots of a given, twice continuously differentiable, function f E C2[a, b]. Your program should first probe the function f(x) on the given interval to find out where it changes sign. (Thus, the program has, in addition to f itself, four other input arguments: a, b, the number nprobe of equidistant values between a and b at which f is probed, and a tolerance tol.) For each subinterval [ai,b] over...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • Page 73, as mentioned in the stated question, is provided below 1. Consider a new root-finding...

    Page 73, as mentioned in the stated question, is provided below 1. Consider a new root-finding method for solving f(x) = 0. Successive guesses for the root are generated from the following recurrence formula: Xn+1 = In f(xn) fhen + f(xn)] – F(Xn) (1) Write a user-defined function with the function call: [r, n] = Root fun (f, xl, tol, N) The inputs and outputs are the same as for the user-defined function Newton described on page 73 of Methods....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT