Question: = = In the main script file, given a set of data point x = [x_1,x_2...X_n] and y = [y_1,y_2,...,Y_n], we would like to evaluate

![data point x = [x_1,x_2...X_n] and y = [y_1,y_2,...,Y_n], we would like](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f331e74b283_05466f331e69c585.jpg)

= = In the main script file, given a set of data point x = [x_1,x_2...X_n] and y = [y_1,y_2,...,Y_n], we would like to evaluate Lagrange polynomial and Newton polynomial which fit the data. We then check that both polynomials give the same result and compare the speed of evaluations. Template of the script file clear n = 20; % number of data points m = 100; % number of evaluation points f = @(x) sin(pi*x)+exp(x.^2+2); % the function f x = 0:2/(n-1):2; % x location of data points = y = f(x); % f(x) values = z = 0:2/(m-1):2;% x location of evaluation points Pz_Nev = zeros(size(z)); % initialize Pz_Nev (which is the vector of using Neville_method to evaluate P(z)) Pz_New = zeros(size(z)); % initialize Pz_New (which is the vector of using Newton polynomial to evaluate P(z)) = %% evaluate P(z) by Neville_method (O(n^2*m) operations) tic = for j = 1:length(z) Pz_Nev(j) = end t_Nev=toc %% evaluate P(z) by Newton polynomial (O(n^2 + m*n) operations) = = tic [b] = Divided_difference(x,y); % You can download this function below for j = 1:length(z) Pz_New(j) = end t_New=toc norm(Pz_New-Pz_Nev) The first Matlab function is used to evaluate the Lagrange polynomial by Neville_method % ((x,y) are the vectors of data points (x_i,f(x_i)) and z is the location to Input: X, Y, Z, evaluate P) Output: Pz % Pz is the value of Pat z) Pseudocode: Step 1: Set n = length(x), initize Q =zeros(n,n); Q(:,1) = y; Step 2: for i = 2, 3, ..., n for j = 1,2, ..., 1-1 = Qi,j+1 = ((2 Xij)Qi,j (z x;)Qi-1,j)/(xi Xi_;) end end Step 3: Set Pz = Q(n,n); The second Matlab function is used to evaluate the Newton polynomial with given coefficients b i where P(x) = b[ + b2 (z x1) + b3 (z x1)(2 x2) + ... z + bn (z x1)(z x2)... (z Xn-1). Z 2 Input: b, x, z (b is the coefficients of Newton polynomial, x is location of the data points and z is the location to evaluate P) Output: Pz % Pz equal to P(z) where P is the Newton polynomial Pseudocode: Step 1: Set n = length(x), initize S = 1; Pz=b(1)*S; = Step 2: for i = 2, 3, ..., n > 1 = = S = S(z Xi1) %S = 11;=1 (z x;) ) P(x) = P(x)+b S end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
