필터 지우기
필터 지우기

How to fit a plot to a set of data points in a log-log scale?

조회 수: 6 (최근 30일)
Kashif Naukhez
Kashif Naukhez 2023년 4월 2일
댓글: Alex Sha 2023년 4월 4일
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?

답변 (1개)

Star Strider
Star Strider 2023년 4월 2일
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
Star Strider
Star Strider 2023년 4월 2일
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')
T1 = 1023×2 table
x y _________ _______ 0.0082825 0.67546 0.023756 0.22581 0.0086785 0.65298 0.0029937 0.94721 0.005617 0.78886 0.017193 0.35288 0.018741 0.31476 0.0071413 0.72825 0.020214 0.28837 0.02242 0.2522 0.0056988 0.78201 0.004754 0.84164 0.007176 0.7263 0.032983 0.12512 0.0045875 0.85239 0.013214 0.47507
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
B = 2×1
-16.6911 -0.2806
fv = 4.8219
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)
Warning: Negative data ignored
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
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.
B = 2×1
1.0e+06 * 0.0000 1.3517
fv = 293.7237
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
Alex Sha 2023년 4월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by