Cut off a Curve created by "curve fit"

조회 수: 7 (최근 30일)
JingChong Ning
JingChong Ning 2023년 1월 24일
편집: Matt J 2023년 1월 24일
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
  댓글 수: 1
Matt J
Matt J 2023년 1월 24일
편집: Matt J 2023년 1월 24일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2023년 1월 24일
편집: Matt J 2023년 1월 24일
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

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by