Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Matt Tearle
Matt Tearle el 29 de Sept. de 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 comentarios
Adam Quintero
Adam Quintero el 29 de Sept. de 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 el 30 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Descriptive Statistics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by