隐函数的做图。心形图的笛卡尔坐标系形式为(x.^2+(9/4)*y.^2+z.^2-1).^3-x.^2.*z.^3-(9/80)*y.^2.*z.^3。fplot可以进行函数的做图,ezplot可以进行隐函数和多元函数的做图,fplot的一个程序代码如下:fplot('sin(x)',[0
pi])。ezplot的一个程序代码如下:ezplot('u^2+v^2-6',[-3 3 -3 3]),做出来的图像如图1所示。
图1 ezplot函数图像
接着再说一下三维函数的做图。三维图像制作一般是在确定向量X和Y的基础上,使用meshgrid命令生成新的矩阵,在输入函数Z=f(X,Y),然后使用mesh生成三维网格,使用surf生成三维曲面。一段典型的程序代码如下所示:
figure(1);
x=-1:0.01:1;
y=-2:0.01:2;
[x,y]=meshgrid(x,y);
z=0.3*x.^3+0.5*y.^4;
subplot(2,2,1);
mesh(x,y,z); %三维网格
subplot(2,2,2);
surf(x,y,z); %三维曲面
subplot(2,2,3);
meshc(x,y,z); %网格线下添加等值线
subplot(2,2,4);
surfc(x,y,z); %曲面下添加等值线
figure(2);
meshz(x,y,z); %网格线下添加零平面
做图所得的结果如图2和图3所示。
图2 mesh、surf、meshc、surfc
图3 meshz
ezplot貌似仅可以对二维变量进行处理,我们这里有三个变量,因此需要考虑其他的方法,经过网上发帖咨询,得知isosurface等值曲面函数可以解决这样的问题,具体可见help
isosurface。此外也可以用极坐标绘图函数polar进行曲线的绘制,心形图的极坐标形式为r=a(1-sinθ)。在三维图像做出来之后,可以使用高级处理命令对三维图像进行美化等处理。最后的程序如下所示:
% function: 心形图的matlab实现
% author:
dingqian
% email: dingqian12345@126.com
clf;
clc;
clear;
close all;
figure(1);
%用极坐标的形式实现
%r=a(1-sinθ)
x=-pi:pi/1000:pi;
y=2*(1-sin(x));
polar(x,y,'r-');
title('\fontsize{16}To SmallWhite');
xlabel('\fontsize{16}\copyrightDingQian');
figure(2);
%用隐函数的形式实现
%(x^2 + (9/4)y^2 + z^2 - 1)^3 - x^2z^3 - (9/80)y^2z^3 = 0
-3<=x,y,z<=3
[x,y,z]=meshgrid(linspace(-3,3)); %做出网格meshgrid p=(x.^2+(9/4)*y.^2+z.^2-1).^3-x.^2.*z.^3-(9/80)*y.^2.*z.^3; %实现结果的表达
isosurface(x,y,z,p,0); %使用函数做图mesh surf
axis equal;
axis off;
view(0,45); %视角的控制
colormap([1 0
0]); %绘图颜色红色
brighten(0.5); %增亮
camlight
right; %光源位置
lighting
phong; %光照模式
title('\fontsize{16}To SmallWhite');
xlabel('\fontsize{16}\copyrightDingQian');
text(-0.37,0.75,1,'\fontsize{16}\copyrightDingQian');
figure(3);
isosurface(x,y,z,p,0);
view(0,45);
colormap([1 0.2 0.2]);
axis off;
axis equal;
title('\fontsize{16}To SmallWhite');
text(-0.37,0.75,1.20,'\fontsize{16}\copyrightDingQian');
figure(4);
isosurface(x,y,z,p,0);
view(-10,24);
colormap([1 0.2 0.2]);
axis off;
axis equal;
title('\fontsize{16}To SmallWhite');
text(-0.37,0.75,1.20,'\fontsize{16}\copyrightDingQian');
figure(5);
isosurface(x,y,z,p,0);
view(-10,24);
colormap([1 0 0]);
axis off;
axis equal;
title('\fontsize{16}To SmallWhite');
text(-0.37,0.75,1.20,'\fontsize{16}\copyrightDingQian');
figure(6);
isosurface(x,y,z,p,0);
view(1,11);
colormap([1 0 0]);
axis off;
axis equal;
title('\fontsize{16}To SmallWhite');
在上述程序中,对三维图像进行处理的命令有:观察视角view([az,el])、调整绘图颜色colormap([a,b,c])、增亮或变暗颜色印像表brighten(a)、光源设置camlight、光照模式lighting、颜色渲染属性shading等。最后所得到的图像如图4到图9所示。
图4 极坐标图
图5 笛卡尔坐标系下心形图1
图6 笛卡尔坐标系下心形图2
图7 笛卡尔坐标系下心形图3
图8 笛卡尔坐标系下心形图4