how do i deduce the function using linear regression for a set of x and y values?

조회 수: 1 (최근 30일)
clc
clear all
load x2.txt
load y2.txt
x=[x2]
y=log([y2])
format long
b2=x\y
yCalc1 = b2*x;
scatter(x,y)
hold on
plot(x,yCalc1)
xlabel('X_2')
ylabel('Y_2')
title('Linear Regression Relation Between X2 & Y2')
x = 25×1
70 170 10 20 100 180 200 110 130 31
y = 25×1
6.7153 8.8063 2.6804 3.7078 7.4083 8.8500 9.1882 7.6424 8.1274 4.9574
b2 =
0.050110078623913
This is what I am getting when i tried to use linear regression. Is there any way i can find the function this plot is tracing?

채택된 답변

Turlough Hughes
Turlough Hughes 2022년 1월 2일
Consider using a power fit.
Your data:
x = readmatrix('https://uk.mathworks.com/matlabcentral/answers/uploaded_files/850570/x2.txt');
y = readmatrix('https://uk.mathworks.com/matlabcentral/answers/uploaded_files/850575/y2.txt');
Power law fit:
powerFit = fit(x, y, fittype('power1'))
powerFit =
General model Power1: powerFit(x) = a*x^b Coefficients (with 95% confidence bounds): a = 0.01469 (0.001567, 0.02782) b = 2.525 (2.359, 2.691)
plot(powerFit, x, y);
set(gca,'YScale','log')
  댓글 수: 1
Turlough Hughes
Turlough Hughes 2022년 1월 2일
You actually have the parameters in your question but the way you fitted the data fixes the intercept to 0 - so the slope is equal to b2 and the intercept is 0. As a point of information, you can fit the slope and intercept using matrix left division with the following modification:
x = readmatrix('https://uk.mathworks.com/matlabcentral/answers/uploaded_files/850570/x2.txt');
y = readmatrix('https://uk.mathworks.com/matlabcentral/answers/uploaded_files/850575/y2.txt');
log_y = log(y);
b = [ones(size(x)) x]\log_y; % pad the left side with ones
yCalc1 = b(2)*x + b(1);
scatter(x,log_y)
hold on
plot(x,yCalc1)
sprintf('Slope: %1.2f\nIntercept: %1.2f',b(2),b(1))
ans =
'Slope: 0.02 Intercept: 4.45'

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

추가 답변 (1개)

KSSV
KSSV 2022년 1월 2일
clc
clear all
load x2.txt
load y2.txt
x=x2 ;
y = log(y2) ;
% Use polyfit
p = polyfit(x,y,1) ;
yCalc1 = polyval(p,x) ;
scatter(x,y)
hold on
plot(x,yCalc1)
title(sprintf('y = %f*x+%f',p))
xlabel('X_2')
ylabel('Y_2')

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by