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

3 次查看(过去 30 天)
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));

采纳的回答

Walter Roberson
Walter Roberson 2017-10-23
Z = (Y .* ((3*(X.^2) + Y.^2) ) ./ ((X.^2 + Y.^2).^2));
  2 个评论
Anshuman S
Anshuman S 2017-10-23
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 2017-10-24
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.

请先登录,再进行评论。

更多回答(1 个)

DASHRATH
DASHRATH 2022-9-6
[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)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by