Need to fit a curve to some data points
31 views (last 30 days)
Show older comments
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 Comments
the cyclist
on 26 Feb 2024
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?
Accepted Answer
Sam Chak
on 26 Feb 2024
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 Comments
Sam Chak
on 26 Feb 2024
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;
More Answers (4)
Alexander
on 26 Feb 2024
Edited: Alexander
on 26 Feb 2024
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 Comments
Sam Chak
on 26 Feb 2024
Edited: Sam Chak
on 26 Feb 2024
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 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!