Error using ^ (line 51) Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.

52 views (last 30 days)
Heya :) on 29 Sep 2020
Edited: Heya :) on 15 Oct 2020
Please help me to fix the problem mentioned as Error-1 and Error-2. I will be very thankful.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Error -1
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To
perform elementwise matrix powers, use '.^'.
Error in PE_HT (line 125)
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1)^2+c6*hx^2+c7*x_rec(i-1)^2*hx^2;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Error -2 [When I replaced ^ with .^ then i get the error given below]
Unable to perform assignment because the left and right sides have a different number of elements.
Error in PE_HT (line 125)
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1).^2+c6*hx.^2+c7*x_rec(i-1).^2*hx.^2;

James Tursa on 29 Sep 2020
Edited: James Tursa on 29 Sep 2020
This is often caused by using a matrix or vector in an equation when you thought you were using a scalar. E.g., take these lines:
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1)^2+c6*hx^2+c7*x_rec(i-1)^2*hx^2;
hx(i)=d1+d2*x_rec(i-1)+d3*hx+d4*x_rec(i-1)*hx+d5*x_rec(i-1)^2+d6*hx^2+d7*x_rec(i-1)^2*hx^2;
It is very suspicious to me that you have hx(i) on the left hand side, indicating that hx is a vector, and yet you have hx^2 on the right hand side, indicating that hx is a scalar. These don't seem to match. Why wouldn't you be using hx(i-1) and hx(i-1)^2 on the right hand side? E.g.,
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx(i-1)+c4*x_rec(i-1)*hx(i-1)+c5*x_rec(i-1)^2+c6*hx(i-1)^2+c7*x_rec(i-1)^2*hx(i-1)^2;
hx(i)=d1+d2*x_rec(i-1)+d3*hx(i-1)+d4*x_rec(i-1)*hx(i-1)+d5*x_rec(i-1)^2+d6*hx(i-1)^2+d7*x_rec(i-1)^2*hx(i-1)^2;
Heya :) on 1 Oct 2020
You spot my mistake. Thanks a lot.

KSSV on 29 Sep 2020
Example:
x = rand(1,10) ;
x^2 % throws error
x.^2 % this is what you have to use
You should use .^ instead of ^.
3 CommentsShow 1 older commentHide 1 older comment
KSSV on 29 Sep 2020
Again the error is clear.....you have initlaized a matrix to lower dimension and trying to save more dimensions.
Example:
A = zeros(3) ;
A(1,:) = rand(1,4) % error
Heya :) on 1 Oct 2020
Thank you.