Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write two MATLAB functions for implementing the Lagrange interpolation. The first Matlab function is used to calculate the Lagrange polynomial basis. Input: X, z, i
Write two MATLAB functions for implementing the Lagrange interpolation. The first Matlab function is used to calculate the Lagrange polynomial basis. Input: X, z, i % x the vector of locations for the interpolating points, z is the location where you want to calculate Li (z Output: L_i % L_i is the value of Li at z) Pseudocode: Step 1: Set n = length(x), initize L_i = Step 2: for j = 1,2,3,..., n if i ti L_i = L_i*(z-x_j)/(x_i-x_j) end end The second Matlab function is used to calculate the Lagrange interpolation at point x. Input: x, y, z ((x,y) are the vectors of data points (x_i,f(x_i)) and z is the location evaluate P) Output: Pz % Pz equal to P(z) where P is the Lagrange interpolating polynomial Pseudocode: Step 1: Set n = length(x), initize Pz by Pz=0 = Step 2: for j = 1,2, 3, ..., = n Pz = Pz+y_i*L_i(z); % you can use the first function to calculate L_i(z) end Template of the functions Make sure that you use the same order of input below: function [L_i] = Lagrange_polynomial_basis(x,z,i) %%%% type your code below %%%% % n = length(x); L_j = ... ; - % for j = 1:n % if ..... % L_j = L_i.*(z-x(j))/(x(i)-x(j)); % end % end function [Pz]= Lagrange_interpolation(x,y,z) %%%% type your code below %%%% % n = length(x); Pz = ... ; % for j = 1:n = % Pz = Pz + ... - % end Some useful MATLAB Commands: If-statement. Use the following code for "if x #z then ..." if x ~= Z end Let us consider the following test example for checking the code. For example, we will want to interpolate a function f with data point (x_i,y_i) and evaluate the interpolating polynomial at z_i Assuming the function is saved as "Lagrange_polynomial_basis.m" and "Lagrange_interpolation.m". We can use the following Matlab comment to check your code: f= @(x) x.^2-2; = X = = [0,0.5,1]; y = f(x); z = [0.25,0.75]; Pz = zeros(size(z)); = for i = 1:numel(z) = Pz(i) = Lagrange_interpolation(x,y,z(i)); end Pz The final Pz should be Pz = -1.9375 -1.4375
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started