From 92e796df06808889f622cb84ea94a52ca622a9c8 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Wed, 10 Oct 2018 12:27:11 -0400 Subject: [PATCH] From Numerical Analysis 10-10-18 --- LagrangeInterpolation.m | 24 ++++++++++++++++++++++++ LagrangePolynomials.m | 30 ++++++++++++++++++++++++++++++ TestLagrangePolynomials.m | 16 ++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 LagrangeInterpolation.m create mode 100644 LagrangePolynomials.m create mode 100644 TestLagrangePolynomials.m diff --git a/LagrangeInterpolation.m b/LagrangeInterpolation.m new file mode 100644 index 0000000..7e55bbf --- /dev/null +++ b/LagrangeInterpolation.m @@ -0,0 +1,24 @@ +function [value] = LagrangeInterpolation(x_data, y_data, x) +%LagrangeInterpolation evaluates the lagrange interpolation polynomial for +%given set of data points +% +% Input +% - x_data: One-dimensional list of x-coordinates of point through +% which the interpolating polynomial will pass. The degree of the +% interpolating polynomial, n, will be inferred from the number of +% entries in x_data +% - y_data: One-dimensional list of y-coordinates of point through +% which the interpolating polynomial will pass +% - x: One-dimensional list of values for which the Lagtrange +% polynomial will be evaluated +% +% Output +% - value: Value(s) of interpolation polynomial + + %Ensure that the y_data list is a row vector + y_data = y_data(:)'; + + %Do the interpolation + value = y_data*LagrangePolynomials(x_data, x); +end + diff --git a/LagrangePolynomials.m b/LagrangePolynomials.m new file mode 100644 index 0000000..07c6e3e --- /dev/null +++ b/LagrangePolynomials.m @@ -0,0 +1,30 @@ +function [Ln] = LagrangePolynomials(x_data, x) +%LagrangePolynomials evaluates the entire collection of Lagrange +%polynomials +% +% Input: +% - x_data: One-dimensional list of x-coordinates of points through +% which the interpolating polynomial will pass. The degree of the +% interpolating polynomial, n, will be inferred from the number of +% entries in x_data +% - x: One-dimensional list of values for which the Lagrange +% polynomial will be evaluated +% +% Output: +% -Ln: Matrix of values of Lagrange polynomials of degree n. The +% matrix has a row for each polynomial and a column for each value of +% x + + %Allocate space for the output matrix by initializing it to zero + + value_count = length(x); + n = length(x_data) - 1; + Ln = zeros(n + 1, value_count); + + %Build each row of the output matrix by evaluating the corresponding + %polynomial at the x value(s) + for(k = 0:n) + Ln(k + 1, :) = LagrangePolynomial(x_data, k, x); + end +end + diff --git a/TestLagrangePolynomials.m b/TestLagrangePolynomials.m new file mode 100644 index 0000000..07446e8 --- /dev/null +++ b/TestLagrangePolynomials.m @@ -0,0 +1,16 @@ +close all +clear all +format long + +%Set your values +x_data = [1, 1.3, 1.6, 1.9, 2.2]; +y_data = [0.7651977, 0.6200860, 0.4554022, 0.2818186, 0.1103623]; +x = [1.1, 1.5]; + +%Get the values for x +values = LagrangeInterpolation(x_data, y_data, x); + +%Display nicely +for(k = 1:size(x) + 1) + disp(['P(' num2str(x(k)) ') = ' num2str(values(k), '%11.7f')]) +end