Natural Cubic Spline Interpolation
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Was trying to make a function to do cubic spline interpolation, but kept failing the test file. Anyone can check it out, I appreciate it.
function y=naturalCubicSplinetest1(X,Y)
N=length(X)-1;
P=length(x);
D=diff(Y)./h;
dD3=3*diff(D);
a=Y(1:N);
H=diag(2*(h(2:N)+h(1:N-1)));
for k=1:N-2
H(k,k+1)=h(k+1);
H(k+1,k)=h(k+1);
end
c=zeros(1,N+1);
c(2:N)=H\dD3';
b=D-h.*(c(2:N+1)+2*c(1:N))/3;
d=(c(2:N+1)-c(1:N))./(3*h);
end
0 Kommentare
Antworten (2)
Steven Lord
am 13 Aug. 2022
Set a breakpoint on the first line of your code. Run your program on an example that you (or your textbook) have worked out, step by step. Identify on which line the results from your program and the manual process diverge, then determine why. Repeat until your code produces the same results as the manually generated solution.
Then repeat this process with other problems (some from your textbook, some from the test suite, some you've made up) until you feel confident you've eliminated all the bugs.
0 Kommentare
Bruno Luong
am 13 Aug. 2022
You made at least 2 mistakes:
- h is not computes
- Typo "x" instead of "X"
The rest of the algorithm I didn't look at carefully but manything seems odd like the for-loop on H.
h = diff(X);
N=length(X)-1;
P=length(X);
D=diff(Y)./h;
dD3=3*diff(D);
a=Y(1:N);
H=diag(2*(h(2:N)+h(1:N-1)));
for k=1:N-2
H(k,k+1)=h(k+1);
H(k+1,k)=h(k+1);
end
c=zeros(1,N+1);
c(2:N)=H\dD3';
b=D-h.*(c(2:N+1)+2*c(1:N))/3;
d=(c(2:N+1)-c(1:N))./(3*h);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!