Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// a short code to illustrate truncation error and round-off error // Chunlei Li

ID: 3527817 • Letter: #

Question

// a short code to illustrate truncation error and round-off error

// Chunlei Liang 1-20-2013, MAE 2117

#include <stdlib.h>

#include <stdio.h>

#include <math.h>


double poly4(double xx){

double fa = -0.1*pow(xx,4)-0.15*pow(xx,3)-0.5*pow(xx,2)-0.25*xx+1.2;

return fa;

}


// compute derivative of polynomial

double dpoly4(double xx){

double da = -0.4*pow(xx,3)-0.45*pow(xx,2)-xx-0.25;

return da;

}


int main(void){

double x=0.5, h =1.0;

double exact;

int i, n =12;

double D[n],E[n],H[n];

exact = dpoly4(x);

printf("f derivative = ".20f ", exact);

printf("h = finite difference = true error= ");

for (i=0; i<n;i++){

D[i] = (poly4(x+h)-poly4(x-h))/(2*h);

// E[i] = abs(exact - D[i]);

E[i] = fabs(exact - D[i]);

H[i] = h;

h=h/10;

printf(".17f ".20f ".20f ", H[i],D[i],E[i]);

}

return (0);

}

Explanation / Answer

tedious...