Plot for Cubic Splines

1 vue (au cours des 30 derniers jours)
Bhavik
Bhavik le 20 Oct 2012
I have created a code which works out the coefficients of each spline but I am having trouble plotting a graph using the coefficients. Matlab has problems recognising arrays like a(k) when using it to plot graphs. I don't know how to overcome this problem and have been trying for a while now.
I used the Burden and Faires, Numerical Analysis book to create the Matlab code.
Heres the code:
x=[1,2,3];
y=[2,3,5];
n = length(x);
for i=1:n-1
h(i) = x(i+1) - x(i);
end
for i=2:n-1
al(i)=((3/h(i))*(y(i+1)-y(i)))-((3/h(i-1))*(y(i)-y(i-1)));
end
l(1)=1;
m(1)=0;
z(1)=0;
for i=2:n-1
l(i)=(2*(x(i+1)-x(i-1)))-(h(i-1)*m(i-1));
m(i)=h(i)/l(i);
z(i)=(al(i)-h(i-1)*z(i-1))/l(i);
end
l(n)=1;
z(n)=0;
c(n)=0;
for j=n-1:-1:1
c(j)=z(j)-(m(j)*c(j+1));
b(j)=(y(j+1)-y(j))/h(j)-(h(j)*(c(j+1)+2*c(j)))/3;
d(j)=(c(j+1)-c(j))/(3*h(j));
end
for i=1:n-1
a(i)=y(i);
end
p=x;
*for k=1:n-1
fplot('a(k)+(b(k)*(x-p(k)))+(c(k)*(x-p(k)).^2)+(d(k)*(x-p(k)).^3)',[p(k),p(k+1)])
hold on
end*
I'm having problems with the last for loop.

Réponse acceptée

Matt J
Matt J le 20 Oct 2012
for k=1:n-1
fun=@(x) a(k)+(b(k)*(x-p(k)))+(c(k)*(x-p(k)).^2)+(d(k)*(x-p(k)).^3) ;
fplot(fun,[p(k),p(k+1)])
hold on
end
  2 commentaires
Bhavik
Bhavik le 21 Oct 2012
The hold on function isn't working properly. I'm only getting a plot of the last function. I don't get the plots of the function between x = 1 and x = 2
Matt J
Matt J le 22 Oct 2012
No, the code is working fine. Add this as the very last line, if you want to see the entire plot:
axis auto

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Splines dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by