Search code examples
matlabmatlab-figure

How do I add any point to a piecewise fplot?


I have been tasked with finding the natural cubic spline function S to interpolate a set of data I have been given. I have been given 11 data points. Essentially what this means is that I must place a cubic function between these points so that I have 10 cubic functions that flow together into something that looks like 1 function.

I have already completed this. I am new to using MatLab, so I'm not sure how I am supposed to put points on this plot.

Due to the nature of there being 10 functions I must glue together, I found a function called piecewise that works well for this. Here is how I define the function that is piecewise:

syms S(t)
S(t) = piecewise(x(1)<t<x(2), (longExpression1), x(2)<t<x(3), (longExpression2), ... x(10)<t<x(11), (longExpression10));

I then plot the function with fplot() and define the domain I want to see the plot for:

fplot(S(t), [0,100]);

I need to plot 13 points on this graph. The first 11 points that I need to plot are the points that lie at the ends of each of these segments. The last 2 points that I need plot are not endpoints, but instead are points somewhere in the middle of two different piecewise segments.

The 11 data points have the following x and y values:

x = [0 10 20 30 40 50 60 70 80 90 100];
y = [75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212 226.505 249.633 281.422];

My plot looks like this: enter image description here


Solution

  • You may try the following:

    fplot(S(t), [0,100]);
    hold on         % This will prevent the new plot from erasing the result of fplot
    plot(x,y,'or'); % 'ok' is a format identifier, the points will be plot as red(r) circles(o)
    plot([x1,X2],[S(x1),S(x2)],'or'); %Plot the two extra points