I am relatively new to Matlab and is working on a problem but keep getting this error. Please help! I attached the matlab file, but is also posting a screenshot.
MATLAB: *Matrix dimensions must agree error*
error using *MATLABmatrix dimensions must agree
Related Solutions
If you want to solve a system of equations symbolically or using symbolic variables, use the solve function instead of fsolve. The fsolve function requires that the output of the function you specify as the first input be double precision numbers, not symbolic expressions.
If you must use fsolve with symbolic variables inside the function, you must convert those symbolic expressions into numbers using double before returning the numbers (not the symbolic expressions) from the function.
As a very simple example, put all this code into a function example373677.m and run it:
function y = example373677y = fsolve(@mysubfun, 3); function z = mysubfun(q)x = sym('x');r = x.^3-2*x-5;z = double(subs(r, q));
Of course, with solve this is easier.
syms xy = solve(x.^3-2*x-5)
You may need to use vpa to approximate y numerically.
vpa(y)
One of the solutions returned in y should match the result you received from running the function.
Hi
The problem is not in solving
x = 0:0.8:pi;A = sin(x).^(1/3)B = cos(x).^(1/3)
the operator
.^
solves for either real or complex.
The problem lays in the fact that surf doesn't take in complex values.
To solve the long expression of H, since you are dealing with complex numbers, you have to plot 2 surfs, not one: Real and Imaginary, or Modulus and Phase
clctheta5=[0:0.1:pi];theta4=[0:0.1:pi];[X,Y]=meshgrid(theta4,theta5);H=(((((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27+(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2-cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^2-((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(X).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2*cos(Y).^4.*sin(X).^2)./3+(2.*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3).^3).^(1/2)-((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6-(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27-(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2+cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^(1/3)+cos(X).^2./3-cos(Y).^2./3+sin(X).^2./3+((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2.*cos(Y).^4.*sin(X).^2)./3+(2*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3)./(((((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(X).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27+(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2-cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^2-((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2.*cos(Y).^4.*sin(X).^2)./3+(2.*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3).^3).^(1/2)-((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X)^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6-(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27-(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2+cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^(1/3)-(cos(X).^2.*sin(Y).^2)./3-(sin(X).^2.*sin(Y).^2)./3;figure(1);surf(X,Y,abs(H))figure(2);surf(X,Y,angle(H))
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
Best Answer