Filter löschen
Filter löschen

Graphing a linear regression line from given points, very simple example, having trouble with matrices dimensions?

3 Ansichten (letzte 30 Tage)
I am getting the error Inner matrix dimensions must agree.
Error in ==> spectro at 15
Y = [ones(size(T)) exp(-T) T.*exp(-T)]*a;
here is the code: %regression
% Enter t=dataC and y=dataABS as columnwise vectors
dataABS=[1.003 1.038 1.079 0.466 0.402 0.469 0.156 0.237 0.188];
dataT=[9.94 9.17 8.34 34.23 39.62 33.93 69.86 58.00 64.85];
dataC=[10 11 10 6 7 6 2 3 2];
% Form the design matrix
X = [ones(size(dataC)) exp(-dataC) dataC.*exp(-dataC)];
% Calculate model coefficients
a = X\dataC;
T = (0:0.5:10)';
Y = [ones(size(T)) exp(-T) T.*exp(-T)]*a;
plot(T,Y,'-',dataC,dataABS,'o'), grid on
any suggestions appreciated.
thanks

Akzeptierte Antwort

Matt Tearle
Matt Tearle am 29 Sep. 2011
% Enter t=dataC and y=dataABS as columnwise vectors
And then you enter them as rows :)
Stick a transpose ( ' ) on those three lines and it works.
ETA: Well, "works" is a subjective term... I suspect the line a = X\dataC; is supposed to be a = X\dataABS;
ETA(2): In response to question below, here's the code using function handles. At the end of this, ymodel is a function of t that you can evaluate ad naseum.
% Enter t=dataC and y=dataABS as columnwise vectors
dataABS=[1.003 1.038 1.079 0.466 0.402 0.469 0.156 0.237 0.188]';
dataT=[9.94 9.17 8.34 34.23 39.62 33.93 69.86 58.00 64.85]';
dataC=[10 11 10 6 7 6 2 3 2]';
% Form the design matrix
dmat = @(t) [ones(size(t)) exp(-t) t.*exp(-t)];
% Calculate model coefficients
a = dmat(dataC)\dataABS;
T = (0:0.5:10)';
ymodel = @(t) dmat(t)*a;
plot(T,ymodel(T),'-',dataC,dataABS,'o'), grid on
  2 Kommentare
Adam Quintero
Adam Quintero am 29 Sep. 2011
Excellent! It makes sense now. I am now wondering how to extract the equation of my line of best fit. Is there a function? I'm checking around the help but not having mcuh luck.
Matt Tearle
Matt Tearle am 30 Sep. 2011
What do you mean by "extract the equation"? As a string? Not really -- you might as well just do it by hand. As a function you can evaluate? Yes: see above for edit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by