How to plot a cubic spline from the coefficients?

17 views (last 30 days)
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.

Accepted Answer

Raunak Gupta
Raunak Gupta on 20 Nov 2019
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

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!