Generate a logarithmic regression of xlsdata and plot it

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!

 Accepted Answer

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 Comments

Correct of course. But honestly, the subtle error you corrected in your answer is symptomatic of the reason I have begun to strongly dislike tables in MATLAB.
Tables allow users to use MATLAB as if they are still using a spreadsheet.
Why learn MATLAB itself as a language, when you have a spreadsheet inside MATLAB right there? As it is things are bad enough that we have stuents who insist on doing everything from a spreadsheet and directly back into a spreadsheet. That is, I have this data in a spreadsheet, will someone please show me how to do computation X on the spreadsheet, and then return the results as a spreadsheet.
And, yes, I can sort of understand the reason why tables were introduced into MATLAB. It was an interesting idea, as it allows you to nicely collect your data for display, to view your data. As it is, I often group my data into a 2-d array, a nice way to compare two columns perhaps side by side. A table is even nicer for display purposes. The problem arises in my eyes when it becomes too easy, too tempting to do all of your computations there too.
Sorry, just a rant, that probably belongs in a discussion.
@Dyuman Joshi I attache the wrong data file. Please see update in post. Sorry for the blunder. I tried your code on it, but it didn't work, I got
Incorrect number or types of inputs or outputs for function log10.
Error in uppgift44 (line 5)
X=log10(P.Var1);
Idk how you got this data, but it contains many spaces at the end of the data values in the 1st column, thus the data is read as a cell array of char vectors -
P=readtable('data.xlsx')
P = 5×2 table
Var1 Var2 _______ ____ {'39 '} 167 {'40 '} 192 {'41 '} 240 {'42 '} 343 {'43 '} 497
And as they are significant whitespace character, so regular/inbuilt string functions won't work.
x = str2double(extractBefore(P.Var1, strlength(P.Var1)))
x = 5×1
39 40 41 42 43
X = log10(x);
Y=log10(P.Var2); % convert both variables to log's
b=polyfit(X,Y,1); % estimate coefficients
%% Also raising it to the power of 10 is not required.
yhat=polyval(b,[X(1) X(end)]); % evaluate at end points
figure
loglog(X,Y,'r')
hold on
plot([X(1) X(end)],yhat,'b') % add fitted line
@Dyuman Joshi Thanks! This is truly great! How do you get the equation of the line, and what is the difference between the blue and red lines?
Thanks
"How do you get the equation of the line"
Refer to the documentation of polyfit and see what is the form of output it returns.
"what is the difference between the blue and red lines?"
I don't understand what you are asking here.
@Dyuman Joshi Did you see the plot? It has a red line and a blue line. What is the red line and blue line?
Regarding polyfit there is not one example on how to generate a function from a set of points that are regressed. Any ideas where I can find this? Thanks
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 ..."
Thanks , this worked out well!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Asked:

on 22 Feb 2024

Commented:

on 26 Feb 2024

Community Treasure Hunt

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

Start Hunting!