```python
%pylab inline
%config InlineBackend.figure_format = 'retina'
from ipywidgets import interact
```

# Question 1
The Lagrange interpolating polynomial is
$$ p(x) = \sum_{j=0}^{n}y_j L_j(x).$$
Show that the identity ,
$$ \sum_{j=0}^{n} L_j(x) = 1,$$
is true for all $x$.

**Hint: The answer requires no algebra. Use the fact that $f(x) = 1$ is a polynomial of degree zero and a Lagrange polynomial.**

------------------------------------------------------------------



# Question 2

## A. 
Write a function for computing the barycentric weights 
$$w_j = \left[\prod_{\substack{i=0 \\\\ i\neq j}}^{n}(x_j - x_i)\right]^{-1}.$$
Your function should take as input a vector containing the nodes $x_j$ and output the weights $w_j$ . Call your function `baryfit`. Write another function for evaluating the barycentric interpolant 
$p(x).$ 
This function should take as input a vector containing the nodes $x_j$, a vector containing the corresponding barycentric weights $w_j$ (generated from your `baryfit` function), a vector containing the corresponding function values $y_j = f(x_j)$, and the location (or a vector of locations) of where the interpolant should be evaluated. The output of the function should be the value of the interpolating polynomial at all the evaluation points. Call this function `baryeval`.

------------------------------------------------------------------

## B. 
Using your `baryfit` function from part a, generate the barycentric weights for the following two sets of nodes:
  1. $x_j = -1 + \frac{2j}{8}$, $j=0,1,\ldots, 8$
  2. $x_j = -\cos(\frac{j\pi}{8})$, $j=0,1,\ldots, 8$

Plot the values of the weights versus the corresponding values of the nodes (i.e plot ($x_j$ , $w_j$ )) for each of the node sets. Comment on the results.

------------------------------------------------------------------

## C. 
For the two node sets from part B, use your `baryeval` function to evaluate the 8th degree polynomial interpolant of the function $f(x) = \vert x \vert$ at 101 equally spaced points between $[-1, 1]$. Plot the error ($p(x) − \vert x \vert$) in the polynomial interpolant at these evaluation points for each of the two node sets. Which node set seems to produce the best result? What criteria did you use to determine what ‘best’ means?

------------------------------------------------------------------

## D. 
For certain sets of nodes $x_j$, it is possible to give explicit formulas for the barycentric weights $w_j$. The easiest case is when the nodes are equally spaced between $[−1,1]$, (i.e., $x_j =−1+\frac{2j}{n}$, $j=0,1,...,n$). Show that for these nodes
$$w_j = \frac{\left(\frac{n}{2}\right)^n(-1)^{n-j}}{n!}\binom{n}{j}$$
Note that since $w_j$ appear both in the numerator and denominator of the barycentric formula for $p(x)$, any factors common to all $w_j$ can be factored out. Thus, we can reduce the above expression for the barycentric weights to $w_j =(−1)^j\binom{n}{j}$.

------------------------------------------------------------------


```python

```

# Question 3
Suppose you are given the following experimental measurements:
\begin{align}
	x_i &\qquad f(x_i) \\\\
	0.08 &\qquad 0.6739 \\\\
	0.46 &\qquad 2.4306 \\\\
	1.00 &\qquad 0.0000 \\\\
	1.51 &\qquad -1.0621 \\\\
	2.05 &\qquad 0.0986
\end{align}

## A. 
Write a python script (using the code from Q2) that approximates $f(0.75)$ using the following Lagrange polynomials:
\begin{align}
	&\text{$P_1$ using $x_0 = 0.46$, and $x_1 = 1.00$;} \\\\
	&\text{$P_2$ using $x_0 = 0.46$, $x_1 = 1.00$, and $x_2 = 1.51$;} \\\\
	&\text{$P_3$ using $x_0 = 0.08$, $x_1 = 0.46$, $x_2 = 1.00$, and $x_3 = 1.51$;} \\\\
	&\text{$P_4$} \text{ using all five points}.
\end{align}

------------------------------------------------------------------

## B. 
The information in the above table corresponds to the function
\begin{equation} 
f(x) = \sin(\pi x)e^{\cos(x)}. 
\end{equation}

What is the absolute error of each of your approximations $P_i(0.75)$? Which Lagrange polynomial was the most accurate? Is this the result you expected?

------------------------------------------------------------------


```python

```

9111b6a787b67440bf9f48ffd9a64cf7a21794ab
ipynb
Jupyter Notebook
Homework 5 Problems.ipynb
newby-jay/MATH381-Fall2021-JupyterNotebooks
9181fb6e154081de26fb267e0794a67f60ae11a0
Apache-2.0
Homework 5 Problems.ipynb
newby-jay/MATH381-Fall2021-JupyterNotebooks
9181fb6e154081de26fb267e0794a67f60ae11a0
Apache-2.0
Homework 5 Problems.ipynb
newby-jay/MATH381-Fall2021-JupyterNotebooks
9181fb6e154081de26fb267e0794a67f60ae11a0
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES