MATLAB _ 由隐式函数(直接)画曲线曲面

本文介绍了使用MATLAB绘制隐式函数的多种方法,包括通过参数形式转换、isosurface函数和ezimplot3函数。示例包括绘制球面、圆柱面和复杂曲面,并提供了相应的代码示例,便于读者理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 将隐式函数化作参数形式,如球面,x2 +y2 +z2 =R2

  • ezsurf

sphere.m

function sphere(R)
x=[num2str(R),'*sin(s)*cos(t)'];
y=[num2str(R),'*sin(s)*sin(t)'];
z=[num2str(R),'*cos(s)'];
ezsurf(x,y,z,[0,pi,0,2*pi]);
axis square;
shading interp;
colormap(spring);
view(10,40)
在命令行中执行命令sphere(r)就可以得到半径为r的球形了

  •  转化成参数方程后,用mesh或surf画

%     x,y,z are equally sized matrices with coordinates.
s=0:0.05:10;
t=-0.05:0.05:2*pi;%取-0.05 使得圆锥体没有缝隙
[s,t]=meshgrid(s,t);
x=(s+10).*cos(t);
y=(s+10).*sin(t);
z=s;
mesh(x,y,z);
saveobjmesh('temp.obj',x,y,z);

 

二. 利用isosurface, 可将隐式函数直接画出来

1. isosurface与patch连用,可将曲面显示出来

Example: 在同一个图内,画出球面,圆柱面

 

x=-10:0.1:10;y=x;z=x;
[x,y,z]=meshgrid(x,y,z);
f1=x.^2+y.^2+z.^2-4;
f2=x.^2+y.^2-1;
f3=x.^2+z.^2-1;
f4=z.^2+y.^2-1;
p1=patch(isosurface(x,y,z,f1,0));
set(p1, 'FaceColor', 'b', 'EdgeColor', 'none');
p2=patch(isosurface(x,y,z,f2,0));
set(p2, 'FaceColor', 'r', 'EdgeColor', 'none');
p3=patch(isosurface(x,y,z,f3,0));
set(p3, 'FaceColor', 'y', 'EdgeColor', 'none');
p4=patch(isosurface(x,y,z,f4,0));
set(p4, 'FaceColor', 'h', 'EdgeColor', 'none');
daspect([1 1 1])
view(3); axis tight
camlight;
lighting phong

 

2. 将isosurface输出.obj文件

首先,先写vertface2obj.m

function vertface2obj(v,f,name)
% VERTFACE2OBJ Save a set of vertice coordinates and faces as a Wavefront/Alias Obj file
% VERTFACE2OBJ(v,f,fname)
%     v is a Nx3 matrix of vertex coordinates.
%     f is a Mx3 matrix of vertex indices.
%     fname is the filename to save the obj file.

fid = fopen(name,'w');

for i=1:size(v,1)
fprintf(fid,'v %f %f %f/n',v(i,1),v(i,2),v(i,3));
end

fprintf(fid,'g foo/n');

for i=1:size(f,1);
fprintf(fid,'f %d %d %d/n',f(i,1),f(i,2),f(i,3));
end
fprint

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值