# exponential regression functions with error in input values

3 visualizaciones (últimos 30 días)
Benjamin Wilson el 22 de Nov. de 2023
Respondida: Star Strider el 22 de Nov. de 2023
Hi, I am trying to fit a regression model that should look like something of the form y = e^(a-bx) for some positive constants a and b. I have been perviously using the linear least squares method after taking the log of both sides and then fitting a linear model and this works. However, due to some erros in my input data this will not work for the occasion where y = 0 for obvious reasons. The issue is I need to fit an exponential line that follows the points for example [4,100], [25,50], [100,0]. This line should remain postive although the point [100,0] is where the failure arises. Any help would be greatly appreciated!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Fabio Freschi el 22 de Nov. de 2023
Editada: Fabio Freschi el 22 de Nov. de 2023
you can use lsqnonlin or lsqcurvefit
clear variables, close all
% data
x = [4 25 100];
y = [100 50 0];
% fitting function
yfit = @(p,x) exp(p(1)-p(2)*x);
% nonlin problem
f = @(p) yfit(p,x)-y;
% initial guess
p0 = [1 1];
% use lsqnonlin
pfit1 = lsqnonlin(f,p0);
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% use built-in lsqcurvefit
pfit2 = lsqcurvefit(yfit,p0,x,y);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% plot
figure,hold on;
plot(x,y,'o')
xfit = linspace(min(x),max(x),100);
plot(xfit,yfit(pfit1,xfit))
plot(xfit,yfit(pfit2,xfit))
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (1)

Star Strider el 22 de Nov. de 2023
There are other options, however everyone hass fminsearch, so using it —
x = [4; 25; 100];
y = [100; 50; 0];
% objfcn = @(b,x) b(1) .* exp(b(2) - b(3).*x);
objfcn = @(b,x) exp(b(1) - b(2).*x);
[B,nres] = fminsearch(@(b)norm(y - objfcn(b,x)), rand(2,1))
B = 2×1
4.7478 0.0345
nres = 3.9186
xv = linspace(min(x), max(x), 50);
figure
plot(x, y, 'p')
hold on
plot(xv, objfcn(B,xv), '-r')
hold off
grid
The fitnlm function is more robust and provides statistics on the fit. Use the predict function to return the fitted curve and confidence region on the fit.
.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by