[英]How to plot a sphere function?
I tried plotting a sphere to look like this . 我试图绘制一个球体看起来像这样 。
This earlier question contains the code where i tried plotting. 这个较早的问题包含我尝试绘图的代码。
That previous question was answered, but now I am having trouble plotting the sphere function as shown in the plot given by the link above. 回答了前面的问题,但是现在我在绘制球面函数时遇到了麻烦,如上面链接给出的图所示。
Edit This is the sphere function i am using: 编辑这是我正在使用的球形函数:
function ph = sphereFN(x)
ph = sum(x.*x, 2);
end
Edit Outcome should look something like this: 编辑结果应如下所示:
NB: I have changed the function from sphere to sphereFN to avoid the conflict with the matlab sphere. 注意:为了避免与Matlab球体发生冲突,我将功能从sphere更改为sphereFN。
Your sphereFN
may not be doing what you expect it to do. 您的sphereFN
可能没有按照您的预期做。 It looks like you are trying to implement a sum of different powers
test based on this function: 看来您正在尝试基于此函数实施sum of different powers
测试的sum of different powers
:
However, your implementation as: 但是,您的实现为:
ph = sum(x.*x, 2);
does not resemble that function. 与该功能不相似。 How about using this as a starting point: 如何以此为起点:
x = linspace(-1,1,25);
for I=1:size(x,2)
for J=1:size(x,2)
s(I,J) = abs(x(I))^2 + abs(x(J))^3;
end
end
[xx,yy] = meshgrid(linspace(-1,1,25));
surfc(xx,yy,s)
As your are in essence describing a 2D problem, this crude implementation of the function will suffice. 正如您本质上描述的是2D问题一样,该函数的这种粗略实现就足够了。 You can put the nested for
-loops into a function called sum_of_different_powers_2D
(for example) and call it, passing the vector x
. 您可以将嵌套的for
循环放入名为sum_of_different_powers_2D
的函数中,然后调用它,并传递向量x
。
Edit 编辑
You may get a shape that looks more like your intended surface by replace the command inside the nested for
-loops with: 通过将嵌套在for
-loops中的命令替换for
:,可以得到看起来更像您想要的表面的形状:
s(I,J) = abs(x(I))^2 + abs(x(J))^2;
This will resemble this function: 这将类似于以下功能:
Edit 2 编辑2
It is important to understand that n
in the equation quoted above is the dimension of your problem. 重要的是要理解,上面引用的方程式中的n
是问题的维度。 Which, as you showed, is 2
. 如您所示,该值为2
。
Edit 3 编辑3
I recommend you use this function: 我建议您使用此功能:
function ph = sphereFN(x)
for I=1:size(x,2)
for J=1:size(x,2)
ph(I,J) = abs(x(I))^2 + abs(x(J))^2;
end
end
end
Matlab has a built in sphere function and the documentation shows how to plot it. Matlab具有内置的球形函数,文档显示了如何绘制它。
http://www.mathworks.com/help/matlab/ref/sphere.html http://www.mathworks.com/help/matlab/ref/sphere.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.