Plot for Cubic Splines

1 次查看(过去 30 天)
Bhavik
Bhavik 2012-10-20
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.

采纳的回答

Matt J
Matt J 2012-10-20
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 个评论
Bhavik
Bhavik 2012-10-21
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 2012-10-22
No, the code is working fine. Add this as the very last line, if you want to see the entire plot:
axis auto

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by