# Compute the measure of error of an interpolation

78 views (last 30 days)
Dussan Radonich on 24 Oct 2020
Answered: Amer Abdulfattah on 7 Feb 2022
Hello guys,
This is my first question here, sorry for anything being done wrong.
I am trying to compute the measure of error between my interpolant and the actual function. I was told to use the norm(gn - f2) function.
for n=3:20
x = linspace(0,1,n); % vector of points to evaluate the function at
alpha = alphas (0,1,n,f2exact); % coefficients
F2 = @(x,alpha) monomialF(x,alpha);
xx = linspace(-1,2,1000); % points to graph
ff = evaluate_test_points(-1,2,1000,F2,alpha); % results
if n<12
nexttile
plot_fun(xx,f2exact,ff);
else
if (flag==0) % when we reach half of the N points
figure (3) % new figure to show the rest of the graphs
flag = 1;
tiledlayout(3,3);
end
nexttile
plot_fun(xx,f2exact,ff);
end
title (n); % title of our graphs
end
I want to produce a figure of norm(gn - f2) against n, but the function norm doesn't take function handles.
f2exact is my exact function:
f2exact = @(x) sin(pi*x);
The points for my interpolant are in ff, and F2 is where every point is evaluated with the coefficients found alpha. The function of F2, monomialF just takes one point and evaluates it in the polinomial with the coefficients of alpha.
The function evaluate_test_points evaluate the points on F2 and save them in ff.
Hopefully this is enough for people to help me.
Do I have to evaluate norm(ff(x)-f2exact(x)) for every test point, add them up, and then save it to another array to plot against n? Or is there a way to do use the function directly with my function handles and my points?
Thanks for any help here.
Dussan Radonich on 26 Oct 2020
There is a bunch of code that I didn't post because it did't apply to the question i was having. I just wanted to know how/where to use the norm() function to calculate the error from the interpolant and the actual function.

Tarunbir Gambhir on 27 Oct 2020
As per my understanding, your are trying to analyze the total error in the interpolated/estimated valule for different function degrees 'n' ranging from 3 to 20.
The norm function is generally used to get the p-norm distance between two coordinates (eg: predicted coordinate and actual coordinate) in an n dimensional cartesian plane. For your case, to get the deviation between actual and estimated function values I would strongly suggest you use something like Root Mean Squared error. Go through the formulas used for "norm" and "RMS" to understand the difference.
(In case you still want to try it with norm function.)
For getting the Euclidean norm between actual function values ('f2exact(xx)') and the interpolated values ('ff'), the following can be done without the need for another nested for loop:
error_norm(n-2) = norm(ff-f2exact(xx));
Please note that this is different from what you implemented using a nested for loop. What you implemented is simply adding up "norm(x)", where 'x' is a single value representing the difference between an actual and an interpolated value. However, understand that "norm(x)" will just return x, when 'x' is just a scalar value and not a vector.
The function "norm(X)" returns the Euclidean norm of vector X. This norm is also called the 2-norm, vector magnitude, or Euclidean length. Please go through the Mathworks Documentation on norm for more information.
Dussan Radonich on 27 Oct 2020
Thank you, I will try it as soon as possible. More than wanting to use norm, it was given as a hint on an assignment. I just wasn't sure how to implement it on my code.

Amer Abdulfattah on 7 Feb 2022
for n=3:20
x = linspace(0,1,n); % vector of points to evaluate the function at
alpha = alphas (0,1,n,f2exact); % coefficients
F2 = @(x,alpha) monomialF(x,alpha);
xx = linspace(-1,2,1000); % points to graph
ff = evaluate_test_points(-1,2,1000,F2,alpha); % results
if n<12
nexttile
plot_fun(xx,f2exact,ff);
else
if (flag==0) % when we reach half of the N points
figure (3) % new figure to show the rest of the graphs
flag = 1;
tiledlayout(3,3);
end
nexttile
plot_fun(xx,f2exact,ff);
end
title (n); % title of our graphs
end