フィルターのクリア

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

3 ビュー (過去 30 日間)
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

採用された回答

Matt Tearle
Matt Tearle 2011 年 9 月 29 日
% 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 件のコメント
Adam Quintero
Adam Quintero 2011 年 9 月 29 日
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 2011 年 9 月 30 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by