How to resolve Matrix dimensions error?

Hi, May I know how to resolve this error? Thank You!
Error part Matrix dimensions must agree.
Error in MathCW (line 15) v = [ones(21,1) I2 I2.^2].\PN0;

 Respuesta aceptada

Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
I made the code more readable to me, and adjusted the dimensions which are in the annotations.
% Increment of current
I=0:0.5:(0.5*20); % 1x21
I2=transpose(I); % Power measurements 21x1
Po=I.*I*100; % 1x21
for experiment = 1:10 % Noise with sd 0
noise0=randn(1,21); % 1x21
PN0=Po+(0.*noise0); % 1x21
scatter(I,Po); hold on
scatter(I,PN0,'filled');hold on;
hold off
x = polyfit(I,PN0,2); % 1x3
A = [ones(21,1) I2 I2.^2]; % 21x3
PN0tr = PN0'; % 21x1
xx = A .\ PN0tr; % 21x3 .\ 21x1
% I has a 0 in it, so you are dividing by 0 - not good
% A .\ PN0tr is the matrix with elements: PN0tr(i,j) / A(i,j)
y = polyval(x,I); % 1x21
errDiff = Po-y; % ( 1x21 - 1x21 ) .^ 2
E =(errDiff).^2;
Em=mean(E);
disp( ['Em = ' num2str(Em) ])
end
Now the output is:
Em = 6.0104e-25

13 comentarios

Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
This question was about errors that were resolved by getting the dimensions straightened out, and also by getting the polyfit/polyval to be used correctly, which also improved the dimensions so that they made sense.
For this assignment, you are now ready to better understand least squares. I recommend that you ask another question to deal with the algorithm and coding style if you have specific questions or concerns.
For starters, your inner for-loop does does not look right. You should include a comment to explain your intent in the next question. Secondly, you compute variables that are not used. Something is missing. Thirdly, your errors are way off. You may have to parse the remaining questions into smaller pieces and explain your design intent.
gp
gp el 10 de Dic. de 2020
Thanks for your explanation Sir. But i am not quite understand with your third points that you mentioned about the errors and the remaining questions have to parse off.
gp
gp el 10 de Dic. de 2020
is the code consider correct and acceptable?
Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
Editada: Paul Hoffrichter el 10 de Dic. de 2020
>> Thirdly, your errors are way off.
My mistake. Your latest posted version has shows very little error.
>> acceptable?
If you are doing 10 experiments, add a comment to explain how the experiments vary. Right now, I am not seeing any variations. Please explain.
This statement always results in PN0 == Po for every experiment. Try adding some noise.
PN0=Po+(0.*noise0);
gp
gp el 11 de Dic. de 2020
This is the latest version after considered your advices. Is it acceptable ? Thanks
% Increment of current
I=0:0.5:(0.5*20);
I2=transpose(I);
% Power measurements
Po=I.*I*100;
% Repeating 10 times for each noise level
for experiment = 1:10
% Noise values with standard deviation from 0 to 50W
for sd = 0:10:50
gaussnoise=randn(1,21);
PN=Po+(sd.*gaussnoise);
end
scatter(I,Po);
hold on
scatter(I,PN,'filled');
hold off
% Linear Regression and Square Fitting
xl = polyfit(I,PN,0);
vl = [ones(21,1) I2 I2.^2].\PN';
yl = polyval(xl,I);
% Second Order
x = polyfit(I,PN,2);
v = [ones(21,1) I2 I2.^2].\PN';
y = polyval(x,I);
% Fourth Order
xh = polyfit(I,PN,4);
vh = [ones(21,1) I2 I2.^2].\PN';
yh = polyval(xh,I);
% Total Values From least Square
Y = yl + y + yh;
% Estimation Error
errDiff=Po-Y;
E =(errDiff).^2;
end
% Average Error
Em=mean(E);
disp(['Em=' num2str(Em)])
Paul Hoffrichter
Paul Hoffrichter el 11 de Dic. de 2020
Editada: Paul Hoffrichter el 11 de Dic. de 2020
No. As an aside, it might be more interesting to have a 1st order polyfit rather than (or in addition to) a 0 order polyfit (which is just a horizontal line).
Problems that you need to address in a separate questions now that the errors with dimensions are fixed:
  1. The program displays "Em=56224760.4609" which is very high error.
  2. The inner for-loop is incorrect. Why have a loop? The intent is not clear and the loop is ineffective.
  3. The program computes variables that are not used.
  4. In outer loop, E is thrown away at start of the loop.
  5. vl, v, vh are identical.
gp
gp el 11 de Dic. de 2020
The loop is for the noise increment. Should i put it as another loop outside? How to correct the high error and compute the different models of regression?
Paul Hoffrichter
Paul Hoffrichter el 11 de Dic. de 2020
These are good questions that belong in a separate question. It goes deep into what you are trying to accomplish.
Here is a tip: In general, when you have a loop, you expect something to come out of it. In your outer loop, you compute E, and that comes out of the loop, but its value is not dependent upon the number of experiments that you have. In the inner loop, you have PN coming out of the loop, but its value is not dependent upon the number of sd values that you have because you throw away the value of PN in each loop iteration.
So, I suggest that you close this question since the question of the compiler errors and/or crashes has been solved, and let's get a few questions dealing with the pieces of your script addressed in one or more other questions related to analysis of your script.
gp
gp el 11 de Dic. de 2020
Noted. Thanks Sir
gp
gp el 11 de Dic. de 2020
I have submitted another question. Hope you can take a look
Paul Hoffrichter
Paul Hoffrichter el 11 de Dic. de 2020
I and other experts were starting to help you in your other question. Can you tell me why it was deleted, or should I contact Mathworks directly?
gp
gp el 12 de Dic. de 2020
I have no idea why it was deleted. I just noticed that issues. I will now post a new question and hope you can help me on this. Thank you

Iniciar sesión para comentar.

Más respuestas (3)

AVB
AVB el 10 de Dic. de 2020
Next time please make sure you copy the code block in your question using the 'Insert a line of code' option.
There were two issues.
  1. The matrix [ones(21,1) I2 I2.^2] is of 21x3 size hence PNO should have compatible size which means it should be of size 21x1
  2. The first argument in the polyval function should be polynomial coefficients (in descending powers) of an nth-degree polynomial. See polyval
Below is your updated code:
% Increment of current
I=0:0.5:(0.5*20);
I2=transpose(I);
% Power measurements
Po=I.*I*100;
for experiment = 1:10
% Noise with sd 0
noise0=randn(1,21);
PN0=Po+(0.*noise0);
scatter(I,Po);
hold on
scatter(I,PN0,'filled');
hold off
x = polyfit(I,PN0,2);
v = [ones(21,1) I2 I2.^2].\PN0';
y = polyval(x,I);
% Error
E =(Po-y).^2;
Em=mean(E);
end

4 comentarios

gp
gp el 10 de Dic. de 2020
Noted, ya but after resolve it, the E part is showing the same error also
gp
gp el 10 de Dic. de 2020
So, just transpose the PNO also like what Mr.Paul mean ruined?
did you change the polyval function arguments? should be ......
y = polyval(x,I);
If you want to keep your y as is as below,
y = polyval(I,x);
then do transpose on y while computing E
E =(Po-y').^2;
gp
gp el 10 de Dic. de 2020
Yup, I did changed and transpose it also

Iniciar sesión para comentar.

Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
E =(Po-y).^2; % ( 1x21 - 1x3 ) .^ 2
is this what you want:
E =(Po-y').^2; % ( 1x21 -3x1 ) .^ 2

3 comentarios

gp
gp el 10 de Dic. de 2020
Thanks for the reply. But why do I need to transpose it to get the answer?
gp
gp el 10 de Dic. de 2020
Is it used to get the same dimension size?
Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
Editada: Paul Hoffrichter el 10 de Dic. de 2020
Yes. However, I have already fixed a core problem, so now the dimensions are the same. But the above dimensions are interesting. I am pretty sure this would not have worked 10 years ago. Here is a simple example to illustrate some newer matrix/vector operations.
>> t % 1x4
t =
10 20 30 40
>> u % 7x1
u =
0
2
4
6
8
9
10
>> t - u
ans =
10 20 30 40
8 18 28 38
6 16 26 36
4 14 24 34
2 12 22 32
1 11 21 31
0 10 20 30
If u was also a row vector, then its dimensions would have to equal t's dimensions. For example:
s = % 4x1
5 5 5 5
>> t - s
ans =
5 15 25 35

Iniciar sesión para comentar.

Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
y = polyval(I,x); % Error
Did you mean
y = polyval(x,I);

Etiquetas

Preguntada:

gp
el 10 de Dic. de 2020

Comentada:

gp
el 12 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by