The following is a plot of the function cosh(x) as a function of x: In order to
ID: 3664417 • Letter: T
Question
The following is a plot of the function cosh(x) as a function of x: In order to illustrate the effects of the two major error sources, rounding and truncation, attempt to determine an approximation to the derivative of cosh(x) at x = 0.881373587019543 radians using the difference approximation given below. (The true answer is about 1.0).
Use the formula: f'(x) ~ (f(x+h) - f(x)) / h with h=1, 0.5, 0.25, ... 1.8189894035459e-12
(i.e., keep halving h until it is less than 2.0e-12.)
Print out the values of h, your approximation to f'(x), and the error in the approximation for each value of h used. This error will include the effects of both truncation and rounding. Write down (on your output) any conclusions that you can make from these experiments .
Explanation / Answer
package coshx;
public class CosHx {
public static void main(String[] args) {
double x=0.881373587019543;
double ans=Math.cosh(x);
System.out.println("Orignal value:"+ans);
double estimated=0;
double h;
int sign=1;
System.out.println("h f'(x) Error in Approximtion");
for(h=1;h>2.0E-12;h/=2)
{
estimated =(f(x,h,sign) - f(x,0,sign)) / h;
double Error=ans-estimated;
System.out.println(h+" "+estimated+" "+ Error);
x=estimated;
sign=sign*-1; // alternating between sinhx and coshx
}
}
private static double f(double x, double h,int sign)
{
double estimate=Math.round((Math.exp(x+h)+sign*Math.exp(-1*(x+h))/2)); //rounding
estimate=Math.floor(estimate * 100) / 100; //truncation
return estimate;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.