필터 지우기
필터 지우기

Generate a logarithmic regression of xlsdata and plot it

조회 수: 5 (최근 30일)
Sergio
Sergio 2024년 2월 22일
댓글: Sergio 2024년 2월 26일
I have the given data in the xls file. I tried to convert the Var1 and Var2 to log10 values, and then do a regression with the given command. But I get an error.
Incorrect number or types of inputs or outputs for function log10.
Error in uppgift44 (line 2)
X=log10('Var1'); Y=log10('Var2'); % convert both variables to log's
P=readtable('data')
X=log10('Var1'); Y=log10('Var2'); % convert both variables to log's
b=polyfit(X,Y,1); % estimate coefficients
yhat=10.^[polyval(b,[X(1) X(end)])]; % evaluate at end points
loglog(x,y)
hold on
loglog([x(1) x(end)],yhat) % add fitted line
plot(ans)
Where is the error here?
Thanks!

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 2월 22일
That is not how you access data inside Tables. Go through this for more information - Access Data in Tables
%% Provide the filename with extension
P=readtable('data1.xlsx');
%% Use dot indexing to access the data
X=log10(P.Var1);
Y=log10(P.Var2); % convert both variables to log's
b=polyfit(X,Y,1); % estimate coefficients
yhat=10.^[polyval(b,[X(1) X(end)])]; % evaluate at end points
%% Variable names are capital
figure
loglog(X,Y,'r')
hold on
plot([X(1) X(end)],yhat,'b') % add fitted line
  댓글 수: 8
Dyuman Joshi
Dyuman Joshi 2024년 2월 24일
The red line is the plot of the data you have, and the blue line is the corresponding linear fit to the data. You can infer that from the code.
Read the description of the polyfit function - https://in.mathworks.com/help/matlab/ref/polyfit.html#description
"p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n ..."
Sergio
Sergio 2024년 2월 26일
Thanks , this worked out well!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by