Compute the measure of error of an interpolation

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.

6 Comments

I was told to use the norm(gn - f2) function.
I agree.
‘monomialF’ ?
But how? i have two function handles, and my interpolant array full of points.
monomialF, like i wrote it's just a function that takes a value and the coefficients and evaluate the value as x on the polynomial with the coefficients in a dot product. How do i use norm in this case? for every test point and add them up?
Probably something like:
resnorm = norm(F2(x,alpha) - something());
I assume ‘something’ is ‘f2exact’, however that is not obvious, at least to me.
I managed to do something, however i am not sure if it is a correct measurement of the error.
For a bit of general understanding of the code:
  • There is a for that goes from 3 to 20, this are the points that I am taking to interpolate. And show how the interpolant forms better with more points.
  • Then I have a function that evaluates the interoplant on 1000 points
What i did is in every run of the loop that goes from 3 to 20 I added the "error" (what i believe is)
for i=1:1000
error_norm(n-2) = error_norm(n-2) + norm(ff(i)-f2exact(xx(i)));
end
ff ( i ) is the array that contains the 1000 evaluated points of the interpolant of the n run from my loop from 3 to 20.
And f2exact ( xx ( i ) ) is the real sine evaluated on the range that my 1000 points are (in this case they are 1000 points from -1 to 2.)
Is this the correct approach? Or did i just added nonsense? Thank you for the help.
hi
I tried your code but I got error msg :
Unrecognized function or variable 'alphas'.
Error in Untitled3 (line 9)
alpha = alphas (0,1,n,f2exact); % coefficients
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.

Sign in to comment.

Answers (2)

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.

1 Comment

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.

Sign in to comment.

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

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 24 Oct 2020

Answered:

on 7 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!