The "plot" and "polyfit".

4 views (last 30 days)
Zhengsong Liao
Zhengsong Liao on 25 May 2015
Commented: Zhengsong Liao on 27 May 2015
I want to plot a graph of a function fitted by two list of t and fp. A dependent variable fp is gotten by a independent variable t with a fitting function using "ployfit". I don't know how to plot a graph of the function ,even if I got "fp"s from "t" given arbitrarily.The code is shown below.
function y=fp(t)
t_1=[0 0.15 0.49 2.1126];
t_2=[2.27 3.53 8.78 25.45 42.80 43.68 44.08];
p_1=9.80665*[331.2 614.3 505.4 607.8];
p_2=9.80665*[48.65 43.97 42.01 41 40.8 40.79 2.22];
y=polyfit(t_2,p_2,2)*[t.*t;t;1]*(t>=2.1126)+polyfit(t_1,p_1,2)*[t.*t;t;1]*(t>=0&&t<2.1126);
end
Actually,the "plot" seems workable, but I need help to plot;
  1 Comment
John D'Errico
John D'Errico on 25 May 2015
Please learn to format your code so it will be readable. I've done that for you this time.
You do realize that this is not actually a spline that you are building? It is not even a continuous function across the breakpoint.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 25 May 2015
It looks like you are trying to build a VERY simple spline, but not doing a very credible job of it. (Sorry.) This is a piecewise quadratic function, but it is one that will not even be continuous across the breakpoint.
t_1=[0 0.15 0.49 2.1126];
t_2=[2.27 3.53 8.78 25.45 42.80 43.68 44.08];
p_1=9.80665*[331.2 614.3 505.4 607.8];
p_2=9.80665*[48.65 43.97 42.01 41 40.8 40.79 2.22];
poly1 = polyfit(t_1,p_1,2);
poly2 = polyfit(t_2,p_2,2);
plot(t_1,p_1,'bo',t_2,p_2,'ro')
hold on
t = linspace(0,2.1126,100);
plot(t,polyval(poly1,t),'b-')
t = linspace(2.1126,44.08,100);
plot(t,polyval(poly2,t),'r-')
plot([2.1126 2.1126],[0 607.8],'g-')
grid on
In fact, the entire modeling effort is (again, sorry) rather insane. That first piece with only 4 wildly scattered points does not even justify a quadratic polynomial for that fit.
I don't know what you are trying to do here, nor why you are trying to force a quadratic polynomial through that mess. But I'm not sure that you know why either.
  1 Comment
Zhengsong Liao
Zhengsong Liao on 27 May 2015
Thank you for your answer to the question.
The whole work I have tried is to build a spline for these discerete data so that an ODE can be solved with the spline. Actually, these show the force of jet engine from launch, and I want to solve an ODE to get the trajectory with a continuous force function. However ,it is no easy to build a fit function. Quadratic polynomial might be not good so I want to find a BETTER one which at least should be continuous at break point.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!