[英]Plotting a 3D surface in Matlab
我有以下功能:
我想在 MATLAB 中將它繪制在 -15 ≤ x ≤ 15 和 -15 ≤ y ≤ 15 處。
我試過的是這樣的:
[x,y] = meshgrid(-15:1:15, -15:1:15);
z = ((x^4 + y^4 - 4 * x^2 * y^2)/(x^2 + y^2));
plot(x,y,z)
當我運行它時,它說Warning: Matrix is singular to working precision.
Data must be a single matrix Y or a list of pairs X,Y.
而我的變量 z 只包含一個用 NaN 填充的 31x31 雙精度值。
問題是由 x^2+y^2 項除法產生的,在某些情況下它實際上為零,以及您對 Matlab 運算符的錯誤使用。 最后, plot
函數不適合繪制 3D 表面。 為簡單起見,我建議使用符號計算:
syms x y;
z = ((x^4 + y^4 - 4 * x^2 * y^2)/(x^2 + y^2));
fsurf(z,[-15,15,-15,15])
您也可以使用您的數字版本(更快),但要注意使用正確的運算符 - 而不是矩陣乘法*
,例如使用元素乘法.*
。 這也與^
和/
相關。
[x,y] = meshgrid(-15:1:15, -15:1:15);
z = ((x.^4 + y.^4 - 4 .* x.^2 .* y.^2)./(x.^2 + y.^2));
surf(x,y,z)
請注意,在這種情況下未定義原點 - 由於被零除的問題。 如果您願意,您可以使用不同的范圍來避免此問題。
[x,y] = meshgrid(-15:0.17:15, -15:0.17:15);
z = ((x.^4 + y.^4 - 4 .* x.^2 .* y.^2)./(x.^2 + y.^2));
surf(x,y,z,'EdgeAlpha',0) % The above range is dense - so we remove the edge coloring for clarity.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.