# Cut off a Curve created by "curve fit"

8 views (last 30 days)
JingChong Ning on 24 Jan 2023
Edited: Matt J on 24 Jan 2023
I have 3 data point that I need to curve fit a 5th order polynomial to. I used the "fit()" function, but the homework requirement asks for the fitted curve to end at the same x-value as the first and last datapoint. I tried to use "polyval(p, x(ind))" with ind being x>=initial x value & x<= final x value. But it doesn't work. Could you tell me how to get through this?
clear all
close
clc
a0 = [1.05 1.25 linspace(1.5,6,10)];
sa0 = size(a0);
RE = 6371000;
minPr = 6628000;
emax = zeros(sa0(1,1),sa0(1,2));
for i = 1:sa0(1,2)
emax(i)=1-minPr/(a0(i)*RE);
end
emid = emax/2;
B1 = [3.93879 -7.25435 4.68581 -1.14627 -0.64419 0.93603];
B = flip(B1);
C1 = [136.6919 -140.3474 49.2636 -7.5121 -1.2188 0.93603];
C = flip(C1);
tempmax = zeros(1,12);
tempmid = zeros(1,12);
vmax = zeros(1,12);
vmid = zeros(1,12);
for i = 1:12
for j = 6:-1:1
tempmax(i) = B(1,j)*power(emax(i),j-1)+tempmax(i);
tempmid(i) = C(1,j)*power(emid(i),j-1)+tempmid(i);
end
vmax(i) = 5000*tempmax(i);
vmid(i) = 5000*tempmid(i);
end
rGEO = 42241;% in km
mu = 3.986e5;% in km3/s2
vGEO = (sqrt(mu/rGEO))*1000;
vc = zeros(1,12);
dvc = zeros(1,12);
for i = 1:12
vc(i) = (sqrt(mu/(a0(i)*(RE/1000))))*1000;
dvc(i) = vc(i) - vGEO;
end
%C = 'k','b','r','g','y' % Cell array of colros.
figure
for i = 1:12
x1 = [emax(i); emid(i); 0];
y1 = [vmax(i); vmid(i); dvc(i)];
[f,d]=fit(x1, y1, 'poly2');
p = fit(x1, y1, 'poly2');
ind = x1>=0 & x1<=emax(i);
fid = polyval(p,x1(ind));
plot(x1,y1,'ko')
plot(p,'b')
legend('hide')
hold on
end
Matt J on 24 Jan 2023
Edited: Matt J on 24 Jan 2023
I have 3 data point that I need to curve fit a 5th order polynomial to.
Three points is not enough to fit a 5th order polynomial. It has 6 unknown coefficients.

Matt J on 24 Jan 2023
Edited: Matt J on 24 Jan 2023
This might be what you want:
for i = 1:12
x1 = [emax(i); emid(i); 0];
y1 = [vmax(i); vmid(i); dvc(i)];
p = polyfit(x1, y1, 2);
xfit=linspace(min(x1),max(x1),1000);
plot(x1,y1,'ko',xfit,polyval(p,xfit),'b')
legend('hide')
hold on
end ### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!