QuestionQuestion

Transcribed TextTranscribed Text

Q1: Root-finding methods For this question, it is fine to use the code provided in class as your starting point. You will need make changes to the functions. For the following exercise, we are going to use the polynomial: f(x) = (x + 1) (x - 4) (x - 10) = x3 - 13x² + 26x + 40 f'(x) = 3x² - 26x + 26 Clearly, the function has 3 roots: -1, 4 and 10. Also, for x < -1, f(x) < o and for X > 10, f(x) > o a. Implement a function double f (double x) that returns the polynomial value and a function double fPrime (x) that returns the derivative of the polynomial. b. Implement double findRootNewton (double x, int N) that implements the Newton Raphson root finding method for starting point x and iterating N times. That is, we're not trying to find a root to a specified precision. Rather, we want to 'loop' N times and return the estimated root. c. Implement double findRootsecant (double x, int N) that implements the secant root finding method for starting point X and iterating N times. For your second starting point, use X + 0.25. Note: your code should check whether f(x_prev) - f(x) is essentially zero. For our purposes, you can use the fabs() function for the difference and check whether it is less than 1e-10. Hint: look at the lecture notes :-) d. Implement double findRootBisection (double x, int N) that implements the bisection root finding method for starting point x and iterating N times. For your second starting point, use -2 if f(x) > o and 11 if f(x) < o. Note: the bisection function in the lecture notes uses two parameters, a & b, to define starting points. For the homework, only one starting point is passed in as a parameter. e. Create a program whose main() function calls each root finding function with starting points: 1, 3.8, 20 and -40 and N=5. Print out both the estimated root, r, each function finds and the value of f(r). Include these values in your homework text file. Hint: you may want to create a function that takes starting point X and number of iterations N as parameters and then calls the root-finding methods and prints out results. f. Repeat step e but use N = 10 Q2 : Arrays and Pointers Start with an array of doubles: double A[] = {5.6, 3.2, 1.0, 199, 32, 5.7, 9.9, 11.0, 999, 0.0001} ; We want to copy the array values into a new array and then sort the values in the new array. We will use a simple bubble sort with pseudocode below: procedure bubbleSort ( A : list of sortable items ) n = length (A) repeat swapped = false for i = 1 to n-1 inclusive do / * if this pair is out of order */ if A[i-1] - > A[i] then swap them and remember something changed */ swap ( A [i-1], A[i] ) swapped = true end if end for until not swapped end procedure https://en.wikipedia.org/wiki/Bubble: sort#Pseudocode implementation Note: pseudocode is not "real" code. Rather, it describes how to implement an algorithm. Notice, for example that the pseudocode starts indexing at 1 whereas C++ arrays begin indexing at o. What to do in main(): 1. Create A as described above 2. Create an array B with 10 elements 3. Copy all the elements of A into B 4. Sort the elements of B using the bubble sort described above 5. Create a pointer C that points to B 6. Using a loop and pointer arithmetic, print out the values of B by dereferencing C. That is, don't use B directly to print the values, use C and pointer arithmetic (either ++ or +). Q3: Simulation - Valuing Calls and Puts This question requires the lecture notes from Lectures 6&7. For this question, we are going to value at-the-money European calls and puts with one (1) year until - expiration. Remember, a European call's value at expiration is max(o, S-K) and a European put's value at expiration is max(o, K-S) where K = strike price and S = spot price. Assume that at T=0, spot and strike price are $100. Further, assume that the risk-free rate (interest rate) is 0%, that is the present value of $1 is equal to the future value of $1. Finally, assume that the stock in question has a drift of 0% and volatility of 20% and has lognormal returns. We simulate the stock's stochastic process as: as = udt + odZ After solving using Ito's Lemma and with our horizon as 1 year, we generate log returns as 1 r = 1 + oZ 0.5 0.2 0.2) 0.2 Z -0.02 + Z = - * * + * = - where Z is a standard normal variable. We then can estimate our stock price as S1 = Se" We want to simulate 100,000 1-year stock returns. Refer to class notes (Lectures 6+7) for how to generate standard normal random variables 1. For each simulated return, calculate the expected stock price, the value of call option and value of a put option 2. Keep a running sum of each of these values so that you can calculate average (expected) values 3. Print out the average stock price, call price and put price at the following iterations: 100, 1000, 10000, 100000. That is, after 100 iterations, calculate the average stock, call and put price and print it out. After 1000 iterations, do the same, etc. 4. Using Excel, R or some other handy tool (not C++), create a chart that shows expected call and put values for these iterations. That is, iteration is the X-axis, value is the Y-axis. Include the chart in your homework write-up. Remember: we are calculating values for at-the-money calls at puts. S = 100, K = 100 and thus the value of the call at T=1 will be max(o, S1-100) and the value of the put will be max(o, 100-S1) where S1 is the simulated stock price after 1 year. Lecture Notes that are related to question 3: C++ - Lecture 6 Simulation For our purposes, simulation refers to randomly repeating a task multiple times and recording the results. This is often called Monte Carlo Simulation We will focus on generating random price paths using a lognormal distribution When we couple these price paths with something like asset values, we are integrating to find an expected value (like in the homework) A (Very) Brief Foray into Stochastic Calculus We want to model equity returns with an expected return (drift) and volatility o When we say volatility, we are talking about the standard deviation of (natural) log returns Note: in C++, log() computes the natural log There is a lot going on here, so we are going to stick to the parts that get us to implementing simulation

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

//
// Problem1.cpp
// HomeworkCplusplus
//
//
//
//
#include <iostream>
#include "Problem1.hpp"
#include "math.h"

double f(double x){
    double y=pow(x,3)-13*pow(x,2)+26.0*x+40.0;
    return y;
   
};

double fPrime(double x){
    double y=3.0*pow(x,2)-26.0*x+26.0;
    return y;
};

double findRootNewton(double x, const int N){
    double xold=x;
    double xnew=0.0; int i;
    for (i=0; i<N; i++){
       xnew=xold-f(xold)/fPrime(xold);
       xold=xnew;
    }
    return xnew;
};

double findRootSecant(double x, const int N){
    const double tolerance=1e-10;
    double xnminustwo=x;   double xnminusone=x+0.25; double error=1.0;
    double xn=0.0; int i;
    for (i=0; i<N; i++){
       error = fabs(f(xnminusone)-f(xnminustwo));
      
       if (error >=tolerance...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$88.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats