How to fit a plot to a set of data points in a log-log scale?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have an equation of the form:
y = [1 + (1 - q) * x * m]^1/(1 - q)
I have 1000 values of y and x . I need to determine the values of 'q' and 'm' which best fits the equation in a log-log scale and also plot the same.
How to do it?
0 comentarios
Respuestas (1)
Star Strider
el 2 de Abr. de 2023
Perhaps something like this —
yfcn = @(x,m,q) (1 + (1 - q) .* x .* m).^1./(1 - q); % Objective Function
x = sort(rand(1,50)); % Create Random Data
y = rand(1, 50); % Create Random Data
B0 = rand(2,1); % Initial Parameter Estimates
[B,fv] = fminsearch(@(b) norm(y - yfcn(x, b(1),b(2))), B0); % Estimate Parameters
xv = linspace(min(x), max(x), 150);
yv = yfcn(xv,B(1),B(2));
figure
loglog(x, y, '.', 'DisplayName','Data')
hold on
plot(xv, yv, '-r', 'DisplayName','Regression')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Location','best')
text(min(xlim)+0.01*diff(xlim), min(ylim)+0.01*diff(ylim), sprintf('$y = (1 + (1-%.2f) \\cdot x \\cdot %.2f)^\\frac{1}{1-%.2f}$',B(2),B(1),B(2)), 'Interpreter','latex', 'FontSize',12)
.
3 comentarios
Star Strider
el 2 de Abr. de 2023
The fit is not perfect, however the regression works for me —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1343429/data_points.xlsx')
yfcn = @(x,m,q) (1 + (1 - q) .* x .* m).^1./(1 - q); % Objective Function
x = T1.x;
y = T1.y;
B0 = rand(2,1); % Initial Parameter Estimates
[B,fv] = fminsearch(@(b) norm(y - yfcn(x, b(1),b(2))), B0) % Estimate Parameters
xv = linspace(min(x), max(x), 150);
yv = yfcn(xv,B(1),B(2));
figure
loglog(x, y, '.', 'DisplayName','Data')
hold on
plot(xv, yv, '-r', 'DisplayName','Regression')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Location','best')
text(min(xlim)+0.01*diff(xlim), min(ylim)+0.01*diff(ylim), sprintf('$y = (1 + (1-%.2f) \\cdot x \\cdot %.2f)^\\frac{1}{1-%.2f}$',B(2),B(1),B(2)), 'Interpreter','latex', 'FontSize',12)
B0 = rand(2,1)*10; % Initial Parameter Estimates
yfcn2 = @(b,x) yfcn(x,b(1),b(2));
[B,fv] = lsqcurvefit(yfcn2, B0, x, y, zeros(1,2)) % Estimate Parameters
xv = linspace(min(x), max(x), 150);
yv = yfcn2(B,xv);
figure
loglog(x, y, '.', 'DisplayName','Data')
hold on
plot(xv, yv, '-r', 'DisplayName','Regression')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Location','best')
text(min(xlim)+0.01*diff(xlim), min(ylim)+0.01*diff(ylim), sprintf('$y = (1 + (1-%.2f) \\cdot x \\cdot %.2f)^\\frac{1}{1-%.2f}$',B(2),B(1),B(2)), 'Interpreter','latex', 'FontSize',12)
The parameters are negative, so the display ed equation looks slightly strange, however there is no way in fminsearch to constrain them. If you have the Optimization Toolbox, use the lsqcurvefit function instead, and set the ‘lb’ srgument to zeros(1,2) to constrain them to be positive. I do not know if the regression will converge under those constraints, however.. (The negative values are ignored in the loglog plot because the logarithms of negative values are complex, and the logarithm of zero is negative infinity.)
Using lsqcurvefit with constrained parameters does not converge in any meaningful sense.
.
Alex Sha
el 4 de Abr. de 2023
Maybe the original fitting function: y = [1 + (1 - q) * x * m]^1/(1 - q)
should be: y = [1 + (1 - q) * x * m]^(1/(1 - q))
if so, the results will become a little better.
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!