How to plot 3D surface using surf [y*(3x^2+y^2)] / (x^2+y^2)^2 ?

1 view (last 30 days)
I have used the following command .
[X,Y] = meshgrid(1:0.5:10,1:20);
Z =(Y*((3*(X.^2) + Y.^2) )/((X.^2 + Y.^2).^2));
surf(X,Y,Z)
This is the error which I am getting
Error using *
Inner matrix dimensions must agree.
Error in Mse302 (line 2)
Z =(Y*((3*(X.^2) + Y.^2) )/((X.^2 + Y.^2).^2));

Accepted Answer

Walter Roberson
Walter Roberson on 23 Oct 2017
Z = (Y .* ((3*(X.^2) + Y.^2) ) ./ ((X.^2 + Y.^2).^2));
  2 Comments
Anshuman S
Anshuman S on 23 Oct 2017
Thanks. Can you please explain how did the matrix dimensions not match previously ? The numerator is a 3x1 * ( 3x3 ) / ( 3x3 ) { if we take x,y to be (3x1).
Walter Roberson
Walter Roberson on 24 Oct 2017
The problem is that * is the algebraic matrix multiplication operation. In A*B, size (MxN)*(PxQ) must have N=P and produces a MxQ output -- size size(A,2) == size(B,1) .
Your X and Y are 20 x 19.
The ((3*(X.^2) + Y.^2) ) part of your expression is adding two 20 x 19 matrices, so the result is 20 x 19.
The Y * part before that means you have a 20 x 19 * a 20 x 19. size(20x19,2) ~= size(20x19, 1) so the use of * was invalid. If you had happened to use meshgrid to create 20 x 20 instead of 20 x 19 then the * would have been permitted, 20x20 * 20x20 giving 20x20 -- but it would probably have been mathematical nonsense for the purposes of your formula.
With the * converted to .* then the (Y .* ((3*(X.^2) + Y.^2) ) part is going to give 20 x 19. The ((X.^2 + Y.^2).^2)) part would also be 20 x 19. You would then have 20x19 / 20x19 .
The / operator in MATLAB is the Matrix Right Divide operator, with B/A being similar to B*inv(A). The operator has several uses including linear least squares and solving simultaneous linear equations. The operator requires that the number of columns be the same between the two sides -- which was the case for you. However, it is rather unlikely that this is the operation you intended to calculate.

Sign in to comment.

More Answers (1)

DASHRATH
DASHRATH on 6 Sep 2022
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = (Y .* ((3*(X.^2) + Y.^2) ) ./ ((X.^2 + Y.^2).^2));
surf(X,Y,Z)

Tags

Community Treasure Hunt

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

Start Hunting!