source('utils.R') ## 2.3-1: ## xs = c(1.0, 1.5, 2.0, 3.0, 3.5, 4.0) ds = log10(xs) ## Get the Newton form coefficients: ## as = coefficients_of_newton_form(xs, ds) xs = rev(xs) ## convert into the orderning needed by newton_form_nested_multiplication as = rev(as) ## 2.3-2: ## ## Evaluate our interpolating polynomial at some points: ## x_test = c(2.5, 1.25, 3.25) f_hat_test = c() for( x in x_test ){ res = newton_form_nested_multiplication(as, xs[-length(xs)], x) f_hat_test = c( f_hat_test, res$as_prime[1] ) } ## Package results: ## error = log10(x_test) - f_hat_test res = data.frame(x=x_test, log10_x=log10(x_test), log10_x_hat=f_hat_test, error=error) print(res) ## ## Duplicate the results above using the "short cut" function: ## xs = c(1.0, 1.5, 2.0, 3.0, 3.5, 4.0) ## x values ds = log10(xs) ## y values x_test = c(2.5, 1.25, 3.25) ## where to evaluate our interpreting polynomial print(newton_form_interpolating_polynomial(xs, ds, x_test)) ## duplicate ## 2.3-3: ## y = log10(2) + log10(1.25) y_hat = res[1, 3] print(sprintf('y= %f; y_hat= %f; abs_err= %f; rel_err= %f', y, y_hat, y-y_hat, (y-y_hat)/y))