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 '.^'.

26 views (last 30 days)
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;

Accepted Answer

James Tursa
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;

More Answers (1)

KSSV
KSSV on 29 Sep 2020
Read about element by element operations.
Example:
x = rand(1,10) ;
x^2 % throws error
x.^2 % this is what you have to use
You should use .^ instead of ^.
  3 Comments
KSSV
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!