Hermite interpolation code in MATLAB This is my MATLAB code for divided differences and Hermite interpolation, but it doesn't work properly. Could you take a look at it? Thank you.
I'm sorry for the layout, but it's the best I could do.
function [F,X] = diffdivher(x,fx,df)

    X=[];

    for i=1:length(x)
        X=[X,x(i),x(i)];
    end

    for i=1:length(f);
        F=[F,f(i),f(i)];
    end

    n=length(x)-1;
    N=2*n+1;

    for i=N:-2:3
        F(i)=(F(i)-F(i-1))/(X(i)-X(i-1));
    end

    for k=2:N
        for i=N+1:-2:k+1
            F(i)=(F(i)-F(i-1))/(X(i)-X(i-k));
        end
    end
end

function [Pval] = herhor(x,f,df,xval)
    X=[];

    for i=1:length(x)
        X=[X,x(i),x(i)];
    end

    dd=diffdivher(x,f,df);

    for i=1:length(xval)
        Pval(i)=dd(end);
        for j=length(dd)-1:-1:1
            Pval(i)=dd(j)+(xval(i)-X(j))*(Pval(i));
        end
    end
end

 A: I'll assume your formulae are correct, and just give some MATLAB observations. For your diffdivher function:


*

*Are you storing each function in its own *.m file?

*You need a F=[]; before using F in your second for-loop.

*Second for-loop: that semi-colon after length(f) really shouldn't be there.

*Your diffdivher function receives a fx and a df, but in your second for-loop you are using a f that came right of nowhere.

*First two for-loops: if you are iterating over the entries of x and f, then it is easier to just assume x and f are row vectors, then access each entry as for y=x,  for z=f. Also, not the most practical way of sortedly duplicating the entries of a vector; simply do X=[x;x](:)';.

*Your third for-loop is really not that necessary, you can just do u=N:-2:3;, then F(u)=(F(u)-F(u-1))./(X(u)-X(u-1));. Notice the dot before the slash. Similarly for the inner loop of your fourth for-loop.


Observations for your herhor function:


*

*You need to declare the length of Pval before accessing dd(end) to each entry.

*Your first for-loop has the same issues as the fourth item listed above.

