I want to plot a surface in spherical coordinates:

% Define tensor components
sigma11 = 20;
sigma22 = 10;
sigma33 = 10;
sigma23 = -40;
sigma13 = 0;
sigma12 = 0;
sigma32 = sigma23;
sigma31 = sigma13;
sigma21 = sigma12;

% Set ranges for theta and phi
phi = 0:0.01:360;
theta = 0:0.01:180;

% Define sigma_n
sigma_n = sigma11 * cosd(theta).^2 * sind(phi).^2 + 2 * sigma12 * sind(phi).^2 * cosd(theta) * sind(theta) + 2 * sigma13 * cosd(phi) * cosd(theta) * sind(phi) + sigma22 * sind(theta).^2 * sind(phi).^2 + 2 * sigma23 * cosd(phi) * sind(theta) * sind(phi) + sigma33 * cosd(phi).^2;

% Define spherical coordinates
x_n = abs(sigma_n).* cosd(theta) * sind(phi);
y_n = abs(sigma_n).* sind(theta) * sind(phi);
z_n = abs(sigma_n).* cosd(phi);

index_pos = (sigma_n >= 0);
index_neg = (sigma_n < 0);

% Plot positive values and negative values in different colors
surf(x_n(index_pos), y_n(index_pos), z_n(index_pos));
hold on;
surf(x_n(index_neg), y_n(index_neg), z_n(index_neg));

There might be other mistakes but I cannot even check because I immediately get the following error:

Error using  * 
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the
matrix individually, use TIMES (.*) for elementwise multiplication.

Error in extratask1 (line 14)
sigma_n = sigma11 * cosd(theta).^2 * sind(phi).^2 + 2 * sigma12 * sind(phi).^2 * cosd(theta) * sind(theta) + 2 * sigma13 * cosd(phi) * cosd(theta) * sind(phi) + sigma22 * sind(theta).^2 * sind(phi).^2 + 2 * sigma23 * cosd(phi) * sind(theta) * sind(phi) + sigma33 * cosd(phi).^2;

I cannot manage to fix it.

Please help!

I hereby attach the vector function that I am defining as sigma_n: The equation that i am trying to plot in spherical coordinates

I expect to get a surface plot similar to this: My plot should look something like this

1

There are 1 best solutions below

0
On

* does matrix multiplication, .* does element-wise multiplication. If you want to do a matrix multiplication, the inner dimensions of the two matrices have to match. Check your definition of sigma_n and decide where you want to do matrix and where element-wise multiplication