Error using / Matrix dimensions must agree

3 views (last 30 days)
My script is this:
r=linspace(-180,180,360);
t=linspace(-180,180,360);
[R,T]=meshgrid(r,t);
phi_=6*r*cos(1+5^2/R.^2)-T;
contour(R,T,phi_)
but matlab throws the following error: Error using / Matrix dimensions must agree.
What am i doing wrong?

Accepted Answer

Voss
Voss on 28 Jun 2022
Use ./
r=linspace(-180,180,360);
t=linspace(-180,180,360);
[R,T]=meshgrid(r,t);
% phi_=6*r*cos(1+5^2/R.^2)-T;
phi_=6*r*cos(1+5^2./R.^2)-T;
contour(R,T,phi_)
  2 Comments
Voss
Voss on 28 Jun 2022
Edited: Voss on 28 Jun 2022
You're welcome!
As @Star Strider points out, you probably intend to use .* as well, but using / instead of ./ is what caused the error.

Sign in to comment.

More Answers (1)

Star Strider
Star Strider on 28 Jun 2022
All the operations in ‘phi’ need to be vectorised —
r=linspace(-180,180,360);
t=linspace(-180,180,360);
[R,T]=meshgrid(r,t);
phi_=6*r.*cos(1+5^2./R.^2)-T;
contour(R,T,phi_)
.

Community Treasure Hunt

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

Start Hunting!