Need to fit a curve to some data points

31 visualizaciones (últimos 30 días)
ISh
ISh el 26 de Feb. de 2024
Editada: Sam Chak el 26 de Feb. de 2024
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 comentarios
the cyclist
the cyclist el 26 de Feb. de 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?
ISh
ISh el 26 de Feb. de 2024
I would like a smooth curve that goes through or at least close to these points. I can't have it ever go below 0 for any positive x values. I cannot go into details of the purpose.

Iniciar sesión para comentar.

Respuesta aceptada

Sam Chak
Sam Chak el 26 de Feb. de 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 comentarios
Alexander
Alexander el 26 de Feb. de 2024
Where do you get 137.5 and -0.91629 from?
Sam Chak
Sam Chak el 26 de Feb. de 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)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
xFit2 = 1×2
137.5000 -0.9163
yAsy2 = xFit2(1).*exp(x*(xFit2(2)));
plot(x,yAsy2,dXdata,dYdata,'o'); grid;

Iniciar sesión para comentar.

Más respuestas (4)

Matt J
Matt J el 26 de Feb. de 2024
Editada: Matt J el 26 de Feb. de 2024
x = [1 2 100];
y = [55 22 0];
flist={@(p,x)1./(x-p(1)).^p(2)};
[p,A]=fminspleas(flist,[0,1], x,y, [-inf,0])
p = 1x2
0.0005 1.3219
A = 54.9679
f=@(x) A./(x-p(1)).^p(2); %The fitted function
xs=linspace(x(1),x(end));
plot(x,y,'or',xs,f(xs),'b--');

Alexander
Alexander el 26 de Feb. de 2024
Editada: Alexander el 26 de Feb. de 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;

Matt J
Matt J el 26 de Feb. de 2024
Editada: Matt J el 26 de Feb. de 2024
x = [1 2 100];
y = [55 22 0];
ylog=log(y+eps);
p=polyfit(x,ylog,2);
f=@(x) exp(polyval(p,x)); %The fitted function
fitError=abs(f(x)-y)
fitError = 1x3
1.0e-12 * 0.8669 0.0355 0.0002
xs=linspace(x(1),x(end));
plot(x,y,'or',xs,f(xs),'b--');

Sam Chak
Sam Chak el 26 de Feb. de 2024
Editada: Sam Chak el 26 de Feb. de 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)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
psol = 1×3
-0.00999999999999777 0.0265454545454659 -0.00854545454549746
resnorm =
9.40049033638573e-21
%% 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)
ytest = 1×3
55 22 0

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by