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!