Home > other >  Where did i do wrong when i tried to approximatee this data using polynomial?
Where did i do wrong when i tried to approximatee this data using polynomial?

Time:09-06

I am starting to learn numerical analysis using MATLAB in my course. so far we have covered polynomial interpolation (spline, polyfit, constraint spline, etc.) I was doing this practice question and I can not get the correct answer. I have uploaded the code I used and the question, where did I do wrong? thanks in advance!

close all; clear all; clc;
format long e

x = linspace(0,1,8);
xplot = linspace(0,1);
f = @(x) atan(x.*(x 1));
y_val = f(xplot);
c = polyfit(x,f(x),7);
p = polyval(c,0.7);
err = abs(f(0.7)-p)/(f(0.7))

The question I encountered is seen in the picture

enter image description here

CodePudding user response:

After some playing around, it seems to be a matter of computing the absolute error instead of the relative absolute error.

The code below yields the desired answer. And yes, it is pretty unclear from the question which error is intended.

% Definitions
format long e
x = linspace(0,1,8)';
xplot= linspace(0,1);
f = @(x) atan(x.*(x 1));
y_val = f(xplot);

% Degree of polynomial
n = 7;

% Points to evaluate function
point1 = 0.5;
point2 = 0.7;

% Fit
c= polyfit(x,f(x),n);

% Evaluate
approxPoint1 = polyval(c, point1);
approxPoint2 = polyval(c, point2);

% Absolute errors
errPoint1 = abs( f(point1) - approxPoint1)
errPoint2 = abs( f(point2) - approxPoint2)

CodePudding user response:

What you did wrong was :

  1. mixing absolute and relative values when calculating errors to feed resulting variable err.

  2. incorrectly placing abs() parentheses when calculating err: Your abs() only fixes the numerator, but then the denominator. To obtain |f(0.7)| you also need another abs(f(0.7))

for point x=0.7 instead of

err = abs(f(0.7)-p)/(f(0.7))

should be

err = abs(f(.7)-p));
  1. you only calculate err for assessment point 0.5 . In order to choose among the possible candidates of what seems to be a MATLAB Associate test multichoice answer, one needs err on 0.5 and err on 0.7 and then match the pair in the correct order, among all offered possible answers.

  2. Although it's common practice to approach polynomial approximation of N points with an N-1 degree polynomial, it's often possible to approximate below satisfactory error with lower degree polynomials than N-1.

Lower degree polynomials means less calculations, less time spent approximating points. If one obtains a fair enough approximation with for instance a degree 4 polynomial, why waste time calculating an approximation with a higher degree all the way up to N-1?

Since the question does not tell what degree should have the approximating polynomial, you have to find it sweeping all polynomial degrees from 1 to up to a reasonable order.

  1. The last error I found is that you have used linspace without specifying amount of points, MATLAB then takes by default 100 points hoping it's going to be ok.

Well, in your question 100 points is way too low an amount of points as I am going to show after supplying the following lines, as mentioned in previous point, sweeping all possible approximating polynomials, NOT on default 100 points you tacitly chose.

np=8;                                  % numel(x) amount supplied points
N=1e3;                              % amount points x grid we build to measure f(x)
x= linspace(0,1,np);

xplot = linspace(0,1,N);
f = @(x) atan(x.*(x 1));       % function to approximate
y_val = f(xplot);                   % f(x)

xm=[.5 .7];                         % points where to asses error

% finding poly coeffs

err1=zeros(2,np 2);          % 1st row are errors on x=.5, 2nd row errors on x=.7

for k=1:1:np 2
    
c = polyfit(x,f(x),k);   

p_01 = polyval(c,xm(1));
err1(1,k) = abs(f(xm(1)-p_01));
% err(1,k) = abs((f(0.5)-p_05)/(f(0.5)))

p_02 = polyval(c,xm(2));
err1(2,k) = abs(f(xm(2)-p_02));
% err(2,k) = abs((f(0.7)-p_07)/(f(0.7)))

plot(x,polyval(c,x),'LineWidth',1.5); %'Color','b');

end

err1

.

enter image description here

The only pair of errors matching in the correct order are indeed those of polynomial order 7, but the total smallest error corresponds to the approximating polynomial of order 6.

What happens when taking linspace without defining a large enough amount of points? let's have a look:

np=8;                                  % numel(x) amount supplied points
% N=1e3;                              % amount points x grid we build to measure f(x)
x= linspace(0,1,np);

xplot = linspace(0,1); %,N);
f = @(x) atan(x.*(x 1));       % function to approximate
y_val = f(xplot);                   % f(x)

xm=[.5 .7];                         % points where to asses error

% finding poly coeffs

err1=zeros(2,np 2);          % 1st row are errors on x=.5, 2nd row errors on x=.7

figure(1);
ax1=gca
hp1=plot(xplot,y_val)
grid on;
hp1.LineWidth=3;
hp1.Color='r';
hold on

for k=1:1:np 2
    
c = polyfit(x,f(x),k);   

p_01 = polyval(c,xm(1));
err1(1,k) = abs(f(xm(1)-p_01));
% err(1,k) = abs((f(0.5)-p_05)/(f(0.5)))

p_02 = polyval(c,xm(2));
err1(2,k) = abs(f(xm(2)-p_02));
% err(2,k) = abs((f(0.7)-p_07)/(f(0.7)))

plot(x,polyval(c,x),'LineWidth',1.5); %'Color','b');

end

err1

enter image description here

With only 100 points all errors come up way too large, not a single error anywhere near 1e-5 or 1e-6.

This is why one couldn't tell which pair to go for, because all obtained values where at least 5 orders of magnitude away from landing zone.

I was about to include a plot with legend, but the visualization of this particular approach is in this case and in my opinion at best misleading, as in both plots for 100 and 1000 points, at 1st glance, both look as if the errors should be similar regardless of the amount of grid points used.

But as shown above 1e2 points cannot approximate the function, it's like pitch dark, looking for something and pointing torch 180 from where we should be aiming at, not a chance to spot it.

Yet 1e3 grid points produce a pair of errors matching one of the possible answers, this is option D.

I hope it helps, thanks for reading my answer.

  • Related