Search code examples
matlabmaplecode-translation

what is the Maple translation for this Matlab code?


uu = polyval(polyfit(x,u,N),xx); % interpolate grid data

The code above is written in Matlab. How to write this statement in Maple?


Solution

  • You haven't shown use your example data for x, u, or xx. So I'll make some up.

    The following generates P3 which can be applied elementwise to Vector xx. It interpolates the degree N-1 polynomial at each point of xx.

    restart;
    Digits:=15:
    N:=20:
    (a,b):=0,2*Pi:
    x:=Vector([seq(evalf(a)..evalf(b), evalf((b-a)/(N-1)))],
              datatype=float[8]):
    u:=map(sin, x):
    xx:=Vector([seq(evalf(a)..evalf(b), 0.5)],
               datatype=float[8]):
    
    P3:=Interpolation:-SplineInterpolation(x,u,degree=N-1,
          endpoints=notaknot):
    
    P3~(xx);
    

    The call P3~(xx) acts element-by-element. But the interpolating function returned by that Interpolate command also knows how to act automatically (elementwise) of the whole of Vector xx, so the following should be more efficient,

    P3(xx);
    

    That example is 1-D data. There are other ways to get a similar result, generating the polynomial interpolant explicitly. But they are less efficient when evaluating across the xx data.

    restart;
    Digits:=15:
    N:=20:  (a,b):=0,2*Pi:
    x:=Vector([seq(evalf(a)..evalf(b), evalf((b-a)/(N-1)))],
              datatype=float[8]):
    u:=map(sin, x):
    xx:=Vector([seq(evalf(a)..evalf(b), 0.5)],
               datatype=float[8]):
    P1:=unapply(CurveFitting:-PolynomialInterpolation(x,u,'t'),'t'):
    P2:=unapply(CurveFitting:-Spline(x,u,'t',degree=N-1,endpoints=notaknot),'t'):
    P3:=Interpolation:-SplineInterpolation(x,u,degree=N-1,endpoints=notaknot):
    sin~(xx);
    P1~(xx);
    P2~(xx);
    P3~(xx);
    plots:-display(
     plot(sin, a..b, color=gray),
     plots:-pointplot(<xx|P1~(xx)>, color=blue, symbol=circle, symbolsize=25),
     plots:-pointplot(<xx|P2~(xx)>, color=red, symbol=circle, symbolsize=16),
     plots:-pointplot(<xx|P3~(xx)>, color=black, symbol=solidcircle, symbolsize=5)
    );
    

    If you have data x, u, and xx of greater dimension (eg, 2-D) then please provide an explicit example in more detail.

    [edit] Here is data that may conform more to your example provided (later) in comments. The results of the four methods (to produce a degree 16 polynomial) agree reasonable well with each other. They also seem to agree quite well with the uu computed by Matlab.

    What should matter to you is the accuracy with which the interpolating polynomial provides when evaluated across the desired range (ie, xx). What should not matter to you is whether the particular coefficients from one approach match closely with the coefficients from another approach.

    The approach using Interpolation:-SplineInterpolation is the most efficient of these four.

    restart;
    Digits := 15:
    x := <1.0000e+00,9.8079e-01,9.2388e-01,8.3147e-01,7.0711e-01,5.5557e-01,
      3.8268e-01,1.9509e-01,6.1232e-17,-1.9509e-01,-3.8268e-01,-5.5557e-01,
      -7.0711e-01,-8.3147e-01,-9.2388e-01,-9.8079e-01,-1.0000e+00>:
    u := <0,-0.2197,-0.7659,-1.3860,-1.8554,-2.0776,-2.0706,-1.9031,
      -1.6443,-1.3454,-1.0405,-0.7524,-0.4970,-0.2863,-0.1294,-0.0327,0>:
    xx := <seq(-1..1,0.01)>:
    exact := map(t->evalhf((exp(4*t)-sinh(4)*t-cosh(4))/16), xx):
    
    P3:=Interpolation:-SplineInterpolation(x,u,degree=16,endpoints=notaknot):
    uu3 := P3(xx):
    plots:-display(
      plot([ <xx|(uu3)> ], style=point, color=black),
      plot(t->(exp(4*t)-sinh(4)*t-cosh(4))/16, -1..1, color=red)
    );
    plot([ <xx|(uu3-exact)> ]):
    
    P4 := unapply(Statistics:-PolynomialFit(16, x, u, 't'), 't'):
    uu4 := map[evalhf](P4,xx):
    plot([ <xx|(uu4-exact)> ]);
    plots:-display(
      plot([ <xx|(uu4)> ], style=point, color=black),
      plot(t->(exp(4*t)-sinh(4)*t-cosh(4))/16, -1..1, color=red)
    );
    
    P1:=unapply(CurveFitting:-PolynomialInterpolation(x,u,'t'),'t'):
    uu1 := map[evalhf](P1,xx):
    plot([ <xx|(uu1-exact)> ]);
    plots:-display(
      plot([ <xx|(uu1)> ], style=point, color=black),
      plot(t->(exp(4*t)-sinh(4)*t-cosh(4))/16, -1..1, color=red)
    );
    
    P2:=unapply(CurveFitting:-Spline(x,u,'t',degree=16,endpoints=notaknot),'t'):
    uu2 := map[evalhf](P2,xx):
    plot([ <xx|(uu2-exact)> ]);
    plots:-display(
      plot([ <xx|(uu2)> ], style=point, color=black),
      plot(t->(exp(4*t)-sinh(4)*t-cosh(4))/16, -1..1, color=red)
    );