matlab绘制空间图形(比较全的总结)

空间图形绘制

对于下面的图形,已经给出了代码与详细注释,并没有给出具体画出的图形,建议初学者自己敲一下代码实现正确的图形显示

[plain]  view plain  copy
  1. %绘制空间曲线,利用plot3  
[plain]  view plain  copy
  1. clf; %清除所有图形  
  2. axis([0 20 * pi 0 20 * pi 0 20 * pi] )   
  3. %表示x轴坐标范围:0-20*pi y:0-20*pi z :0-20*pi  
  4. x = 0 : pi / 50 : 10 * pi;  
  5. y1 = sin(x);  
  6. y2 = cos(x);  
  7. y3 = x;  
  8. subplot(1, 2, 1), plot3(y1, y2, y3, 'r.');  
  9. title('螺旋曲线');  
  10. xlabel('x'), xlabel('y');  
  11. grid on;  
  12.   
  13. x = 0.5 : 0.01 : 1.5;  
  14. y1 = cos(x);  
  15. y2 = sin(x);  
  16. y3 = 1 ./ x;  
  17. subplot(1, 2, 2), plot3(y1, y2, y3, 'b-');  
  18. title('曲线');  
  19. xlabel('x'), xlabel('y');  
  20. grid on;  
[plain]  view plain  copy
  1. %空间曲面的绘制  
[plain]  view plain  copy
  1. x = -3 : 0.1 : 3;  
  2. [X1, Y1] = meshgrid(x); %生成格点矩阵  
  3. Z1 = sin(X1 + sin(Y1));  
  4. subplot(1, 2, 1); mesh(X1, Y1, Z1); %mesh绘制空间曲面  
  5. title('sin(x + sin(y))');  
  6.   
  7. x = -10 : 0.3 : 10;  
  8. [X2, Y2] = meshgrid(x); %生成格点矩阵  
  9. Z2 = X2 .^2 - 2 * Y2 .^2 + eps; %eps是一个很小的数字,避免出现此处为0的情况  
  10. subplot(1, 2, 2); mesh(X2, Y2, Z2);  
  11. title('马鞍面');  

mesh,meshc meshz不同,自己对比画出图形的不同
[plain]  view plain  copy
  1. t = -8 : 0.3 : 8;  
  2. [x, y] = meshgrid(t);  
  3. r = sqrt(x.^2 + y.^2) + eps;   
  4. z = sin(r) ./ r;  
  5. subplot(1, 3, 1); meshc(x ,y, z);  
  6. title('meshc') , axis( [ -8 8 -8 8 -0.5 0.8] );  
  7. subplot(1,3,2) ,meshz(x,y,z), title('meshz' ) ,axis( [ -8 8 -8 8 -0.5 0.8] );  
  8. subplot(1,3,3) ,mesh(x,y,z), title('mesh') , axis( [ -8 8 -8 8 -0.5 0.8] ) ;  

surf:画表面图

[plain]  view plain  copy
  1. x = -10 : 0.3 : 10;  
  2. [X2, Y2] = meshgrid(x); %生成格点矩阵  
  3. Z2 = X2 .^2 - 2 * Y2 .^2 + eps; %eps  
  4. subplot(1, 2, 1); mesh(X2, Y2, Z2);  
  5. title('网格图');  
  6. subplot(1,  2, 2); surf(X2, Y2, Z2);  
  7. title('表面图');  

[plain]  view plain  copy
  1. %空间平面的绘制  
[plain]  view plain  copy
  1. t = -10 : 0.5 : 10;  
  2. [x1, y1] = meshgrid(t);  
  3. z1 = 5 * ones(length(t)); %ones(n)是生成阶数为n的矩阵  
  4. subplot(1, 2, 1); mesh(x1, y1, z1); title('z1 = 5');  
  5.   
  6. t = -1 : 0.1 : 1  
  7. [x2, y2] = meshgrid(t);  
  8. z2 = 5 - 2 * x2 + 3 * y2;  
  9. subplot(1, 2, 2); mesh(x2, y2, z2); title('2 * x2 + 3 * y2 + z2 = 5');  

[plain]  view plain  copy
  1. %求两平面的交线  
[plain]  view plain  copy
  1. x = -10 : 0.1 : 10;  
  2. [x1, y1] = meshgrid(x); %生成格点矩阵  
  3. z1 = x1 .^2 - 2 * y1 .^2 + eps;  
  4. subplot(1, 3, 1); mesh(x1, y1, z1);  
  5. title('马鞍面');  
  6. a = input('a = (-50 < a < 50)'); %动态输入,在控制台输入即可  
  7. z2 = a * ones(length(x));   
  8. subplot(1, 3, 2); mesh(x1, y1, z2); title('平面');  
  9. r0 = abs(z2 - z1) <= 1; % r0只可能为1:表示两图形间距小于1侧认为重合 或者r0为0:不重合  
  10. z3 = r0 .* z2; y3 = r0 .* y1; x3 = r0 .* x1;  
  11. subplot(1, 3, 3);   
  12. plot3(x3(r0 ~= 0), y3(r0 ~= 0), z3(r0 ~= 0), 'x');  
  13. title('交线');  

[plain]  view plain  copy
  1. %球面  
[plain]  view plain  copy
  1. v = [ -2 2 -2 2 -2 2 ] ;  
  2. subplot (1 ,2 ,1 ), sphere(30), title('半径为1的球面');  
  3. axis(v); %控制坐标系  
  4. %半径为3的球面  
  5. [x, y, z] = sphere(30);  
  6. subplot (1, 2 ,2 ) , surf( 2*x , 2*y ,2*z)  
  7. title('半径为2的球面'), axis(v); %控制坐标系,上面已经讲过  

[plain]  view plain  copy
  1. %绘制柱面  
[plain]  view plain  copy
  1. r = - 1: .1: 1; subplot (1,2,1 ), cylinder(1, 50 ) , title('柱面')   
  2. subplot (1 ,2 ,2 ), cylinder( sqrt(abs(r)) , 50 ) , title ('旋转曲面');  


下面给出几个练习题,里面出现的语句基本已经全部在前面讲过,如果发现不懂的地方,请再去看看前面的内容

[plain]  view plain  copy
  1. %test_1 :观察x^u当u取不同的值时的图像的特点  
[plain]  view plain  copy
  1. %方法一  
  2. t = -10 : 1 : 10;  
  3. [x, y] = meshgrid(t);  
  4. for j = 1 : 1 : 6  
  5.     z = x .^ j;  
  6.     surf(x, y, z);  
  7.     axis([-10 10 -10 10 -10 10]);  
  8.     hold on;  
  9. end  
[plain]  view plain  copy
  1. %方法二  
  2. x = -10 : 0.1 : 10;  
  3. for j = 1 : 1 : 6  
  4.     y = x .^j;  
  5.     plot(x, y);  
  6.     axis([-10 10 -10 10]);  
  7.     %axis equal;  
  8.     xlabel('x');  
  9.     ylabel('y');  
  10.     hold on;  
  11. end  



[plain]  view plain  copy
  1. %test_2 :观察plot与fplot画图的区别  
[plain]  view plain  copy
  1. figure(1);1  
  2. x = -4 : 0.1 : 4;  
  3. y = x .^2 .* sin(x);  
  4. plot(x, y, 'b-');  
  5. figure(2);  
  6. fplot('x .^2 .* sin(x)', [-4, 4], 'r.');  



[plain]  view plain  copy
  1. %test_3 :用黄色,数据点为钻石画出3 * cos(x) * exp(sin(x))  
[plain]  view plain  copy
  1. x = 0 : 0.1 : 5;  
  2. y = 3 * cos(x) .* exp(sin(x));  
  3. plot(x, y, 'yd--');  



[plain]  view plain  copy
  1. %test_4:运用xlabel ylabel gtext axis legend title画出图形xsin(x), x*tan(1/x)*sin(x^3), x^3, tan(x) + sin(x)  
[plain]  view plain  copy
  1. x = -pi : pi / 50 : pi;  
  2. y = x .* sin(x);  
  3. plot(x, y, 'b-');  
  4.   
  5. hold on; %只用写一次即可  
  6. x = pi : pi / 50 : 4 * pi;  
  7. y = x .* tan(1 ./ x) .* sin(x .^ 3);  
  8. plot(x, y, 'r-');  
  9.   
  10. x = 1 : 0.1 : 8;  
  11. y = x .^ 3;  
  12. plot(x, y, 'k-');  
  13.   
  14. x = 1 : 0.1 : 8;  
  15. y = tan(x) + sin(x);  
  16. plot(x, y, 'g-');  
  17.   
  18. xlabel('x'), xlabel('y'), title('xsin(x) x*tan(1/x)*sin(x^3) x^3 tan(x) + sin(x)'),axis([-5 10 -4 15]);  
  19. gtext('xsin(x)'), gtext('x*tan(1/x)*sin(x^3)'), gtext('x^3'), gtext('tan(x) + sin(x)');  
  20. legend('xsin(x)', 'x*tan(1/x)*sin(x^3)', 'x^3', 'tan(x) + sin(x)');  


[plain]  view plain  copy
  1. %test_6:在同一个图形窗口画出半径为一的球面,z = 4的平面马鞍面z = 2*x^2 - y^2  
  2. subplot (2, 2, 1 ), sphere(30);  
  3. title('半径为1的球面');  
  4. axis([ -2 2 -2 2 -2 2 ]); %控制坐标系  
  5.   
  6. t = -2 : 0.1 : 2;  
  7. [x, y] = meshgrid(t);  
  8. z = 4 * ones(length(t));  
  9. subplot(2, 2, 2); mesh(x, y, z);  
  10. title('z = 4的平面');  
  11.   
  12. t = -2 : 0.1 : 2;  
  13. [x, y] = meshgrid(t);  
  14. z = 2 * x .^2 - y .^2;  
  15. subplot('position' , [0.2,0.05,0.6,0.45] ) , mesh(x, y, z), title('马鞍面'); grid on;  


[plain]  view plain  copy
  1. %test_7  
  2. %求两平面的交线:马鞍面3x^3 - 2y^2与z =4的平面  
  3. x = -10 : 0.1 : 10;  
  4. [x1, y1] = meshgrid(x); %生成格点矩阵  
  5. z1 = 3 * x1 .^2 - 2 * y1 .^2 + eps;   
  6. subplot(1, 3, 1);  
  7. mesh(x1, y1, z1);  
  8. title('马鞍面');  
  9. z2 = 4 * ones(length(x));  
  10. hold on;  
  11. subplot(1, 3, 2);  
  12. mesh(x1, y1, z2); title('平面');  
  13. r0 = abs(z2 - z1) <= 1; %r0  
  14. z3 = r0 .* z2; y3 = r0 .* y1; x3 = r0 .* x1;  
  15. subplot(1, 3, 3);  
  16. plot3(x3(r0 ~= 0), y3(r0 ~= 0), z3(r0 ~= 0), 'x');  
  17. title('交线');  

猜你喜欢

转载自blog.csdn.net/eric_e/article/details/80327922
今日推荐