필터 지우기
필터 지우기

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 CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by