Need to fit a curve to some data points
조회 수: 30 (최근 30일)
이전 댓글 표시
I am new to matlab, and I have a problem.
I have the data points:
x = [1 2 100]
y = [55 22 0]
I need to generate a curve that goes through these values. I thought some version of polyfit would work, but I also can't have the y values go below 0. I am looking for something like the upper half of the function 1/x. What should I use
댓글 수: 2
the cyclist
2024년 2월 26일
Here is a plot of your data:
x = [1 2 100];
y = [55 22 0];
scatter(x,y,64)
Are you saying you want a smooth curve that passes through these points and never goes below zero? Doesn't go below zero for any x, or just x in [1,100]?
Out of curiosity, what's the purpose?
채택된 답변
Sam Chak
2024년 2월 26일
Here's another candidate. An exponential decay function model should align with the description you provided.
Try learning how to fit it into the data.
%% Data
x = [ 1 2 100];
y = [55 22 0];
%% Exponential decay function
xx = linspace(x(1), x(end));
f = 137.5*exp(-0.9163*xx);
%% Plot results
plot(x, y, 'o', 'markersize', 10, 'linewidth', 2), hold on
plot(xx, f, 'linewidth', 1.5), grid on
xlabel x, ylabel y
댓글 수: 2
Sam Chak
2024년 2월 26일
From the fitting app. But I didn't use lsqcurvefit().
dXdata = [1 2 100];
dYdata = [55 22 0];
x = linspace(0,100,1000);
% y = a*exp(-b*x);
fun2 = @(w,xdata)(w(1)*exp(dXdata*(w(2))));
x02 = [0,0];
xFit2 = lsqcurvefit(fun2,x02,dXdata,dYdata)
yAsy2 = xFit2(1).*exp(x*(xFit2(2)));
plot(x,yAsy2,dXdata,dYdata,'o'); grid;
추가 답변 (4개)
Alexander
2024년 2월 26일
편집: Alexander
2024년 2월 26일
Seems to be an exponetial behavior. Use lsqcurvefit to approximate a curve according your needs. My code:
dXdata = [1 2 100]
dYdata = [55 22 0]
x = linspace(0,100,1000);
% y = a*exp(-b*x);
fun2 = @(w,xdata)(w(1)*exp(dXdata*(w(2))));
x02 = [0,0];
xFit2 = lsqcurvefit(fun2,x02,dXdata,dYdata);
yAsy2 = xFit2(1).*exp(x*(xFit2(2)));
plot(x,yAsy2,dXdata,dYdata,'o'); grid;
댓글 수: 0
Sam Chak
2024년 2월 26일
편집: Sam Chak
2024년 2월 26일
Hi @ISh
This Rational function model (Rat11) precisely fits the three data points.
format long g
%% Data
xdat = [ 1 2 100];
ydat = [55 22 0];
%% A Rational function model (Rat11) with coefficients {p1, p2, p3} is proposed
yfit = @(p, xdat) (p(1)*xdat + 1)./(p(2)*xdat + p(3));
%% Initial guess of coefficients {p1, p2, p3}
p0 = [1, 2, 3];
%% Call lsqcurvefit to fit the model
[psol, resnorm] = lsqcurvefit(yfit, p0, xdat, ydat)
%% Plot fitting result
xq = linspace(xdat(1), xdat(end), 1000);
plot(xdat, ydat, 'o', 'markersize', 10, 'linewidth', 2), hold on
plot(xq, yfit(psol, xq), 'linewidth', 1.5), grid on
legend('Data points', 'Fitted curve', 'location', 'best', 'fontsize', 12)
xlabel('x'), ylabel('y')
title({'$y(t) = \frac{-0.01 x + 1}{\frac{73}{2750} x - \frac{47}{5500}}$'}, 'interpreter', 'latex', 'fontsize', 16)
%% Test
p = [-0.01, 73/2750, -47/5500]; % <-- True values of the coefficients
ytest = yfit(p, xdat)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!