Plotting a 3D graph, incorrect solution.

2 次查看(过去 30 天)
clc
clear
p00 = 553.6;
p10 = 23.56;
p01 = -24.72;
p20 = 212.1;
p11 = -301.9;
p02 = 186.9;
p30 = -2.76;
p21 = -1.261;
p12 = -7.603;
p03 = 11.23;
p40 = -54.55;
p31 = 30.5;
p22 = 10.44;
p13 = 36.48;
p04 = -48.45;
m=1;
n=1;
for x=-100:1:100
n=1;
for y=-100:1:100
Z(m,n)=p00+p10*x+p01*y+p20*x^2+p11*x*y+p02*y^2+p30*x^3+p21*x^2*y+p12*x*y^2+p03*x^3+p40*x^4+p31*y^3*y+p22*y^2*x^2+p13*x*y^3+p04*y^4
n=n+1;
end
m=m+1;
end
surf(x,y,Z)
title('3D PLOT'),xlabel('\bfX'),ylabel('\bfY'),zlabel('\bfZ')
This is the code that gets the Z array, which is to be plotted. Rewriting this in vectorised form, I have
clc
clear
x=-100:1:100;
y=-100:1:100;
p00 = 553.6;
p10 = 23.56;
p01 = -24.72;
p20 = 212.1;
p11 = -301.9;
p02 = 186.9;
p30 = -2.76;
p21 = -1.261;
p12 = -7.603;
p03 = 11.23;
p40 = -54.55;
p31 = 30.5;
p22 = 10.44;
p13 = 36.48;
p04 = -48.45;
[X Y]=meshgrid(x,y);
Z=p00+p10*X+p01*Y+p20*X^2+p11*X*Y+p02*Y^2+p30*X^3+p21*X^2*Y+p12*X*Y^2+p03*Y^3+p40*X^4+p31*X^3*Y+p22*X^2*Y^2+p13*X*Y^3+p04*Y^4
surf(X,Y,Z)
title('3D PLOT'),xlabel('\bfX'),ylabel('\bfY'),zlabel('\bfZ')
Why am I getting separate solutions for both, when the solution should be the one that I am getting from the second one. (solution here is the values in Z). Any help would be most appreciated.
Solved, I was not putting the dots before the product and power operators.

采纳的回答

Seyedali Mirjalili
The problem is that after exiting the for loops, x and y are both equal to 100. So, they are scalars and cannot be used in surf. The function surf requires 2D matrices.

更多回答(0 个)

类别

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