How to plot a cubic spline from the coefficients?

I have a custom function myspline that returns the coefficients for a natural cubic spline as four vectors a,b,c and d which contain the appropriate part of the spline coefficients.
Now I'm supposed to plot the entire spline (in one plot) in a script, the assignment says the evaluation from coefficients to function should make use of Horner.
How would I go about doing this? I've tried to hard-code the multiple functions of the spline with the coefficients, then to plot those; I've also tried multiple different plot fuctions such as plot2sym and fplot that could make use of the coefficients directly.

 채택된 답변

Raunak Gupta
Raunak Gupta 2019년 11월 20일

0 개 추천

Hi,
In my understanding you have the coefficients of the cubic spline fitted for a graph. For converting the cubic spline coefficient into Horner nested polynomial representation you may try using horner. Below code may help plotting one of the cubic spline you may have. You may plot all spline with appropriate coefficients.
syms x
% Some random coefficient
a = rand(10,1);
b = rand(10,1);
c = rand(10,1);
d = rand(10,1);
p = a(1)*x^3 + b(1)*x^2 + c(1)*x + d(1);
horner_p = horner(p);
% Range for each spline to be plotted
range_to_plot = -10:0;
fplot(horner_p,[range_to_plot(1),range_to_plot(2)]);
hold on
for i = 2:size(a,1)
p = a(i)*x^3 + b(i)*x^2 + c(i)*x + d(i);
horner_p = horner(p);
fplot(horner_p,[range_to_plot(i),range_to_plot(i+1)]);
end
hold off

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Splines에 대해 자세히 알아보기

태그

질문:

2019년 11월 14일

답변:

2019년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by