Graphing a linear regression line from given points, very simple example, having trouble with matrices dimensions?
1 次查看(过去 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
0 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!