Need to fit a curve to some data points
31 次查看(过去 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!