课后习题解答
如果出现错误,欢迎指正。
1. 绘制下列曲线
% (1)
x = -20 : 0.1 : 20;
y = 100./(1+x.^2);
plot(x, y); xlabel('x'); ylabel('y'); grid on; title('1-1');
% (2)
x = -5 : 0.1 : 5;
y = 1/(2*pi)*exp(-1/2*x.^2);
plot(x, y); xlabel('x'); ylabel('y'); grid on; title('1-2');
% (3)
x = -1 : 0.01 : 1;
y1 = sqrt(1-x.^2);
y2 = -sqrt(1-x.^2);
plot(x, y1, 'b', x, y2, 'b'); xlabel('x'); ylabel('y'); grid on; title('1-3');
% (4)
t = -20 : 0.1 : 20;
x = t.^2;
y = 5*t.^3;
plot(x, y); xlabel('x'); ylabel('y'); grid on; title('1-4');
2. 绘制下列极坐标图。
% (1)
theta = 0 : 0.01 : 2*pi;
rho = 5*cos(theta)+4;
polar(theta, rho); title('2-1');clear;
% (2)
theta = 0.05 : 0.01 : 2*pi;
rho = 12./sqrt(theta);
polar(theta, rho); title('2-2');
% (3)
theta = 0 : 0.01 : pi/2-0.3;
rho = 5./cos(theta)-7;
polar(theta, rho); title('2-3');
% (4)
theta = 0 : 0.01 : 10*pi;
rho = pi/3*theta.^2;
polar(theta, rho); title('2-4');
3. 绘制下列三维图形。
% (1)
t = 0 : 0.01 : 2*pi;
x = cos(t);
y = sin(t);
z = t;
plot3(x, y, z); xlabel('x'); ylabel('y'); zlabel('z'); text(0, 0, 0, 'O'); grid on; title('3-1');
% (2)
u = 0 : 0.1 : 2*pi;
v = 0 : 0.1 : 2*pi;
[u, v] = meshgrid(u, v);
x = (1+cos(u)).*cos(v);
y = (1+cos(u)).*sin(v);
z = sin(u);
mesh(x, y, z); xlabel('x'); ylabel('y'); zlabel('z'); text(0, 0, 0, 'O'); grid on; title('3-2');
% (3)
x = -10 : 0.5 : 10;
y = -10 : 0.5 : 10;
[x, y] = meshgrid(x, y);
z = 5*ones(size(x));
mesh(x, y, z); xlabel('x'); ylabel('y'); zlabel('z'); text(0, 0, 0,'O'); grid on; title('3-3');
% (4)
[x, y, z] = sphere(100);
x = x * 10;
y = y * 10;
z = z * 10;
mesh(x, y, z); xlabel('x'); ylabel('y'); zlabel('z'); text(0, 0, 0, 'O'); grid on; title('3-2');
4. 在统一图形窗口采用子图形式分别绘制正方形、圆、三角形和六边形。
ord = [4, 2^10, 3, 6]; % 正方形(四边),圆(n边),三角形(三边),六边形(六边)
for i = 1 : 4
subplot(2, 2, i);
theta = linspace(pi/ord(i), 2*pi+pi/ord(i), ord(i)+1);
plot(cos(theta), sin(theta));
xlim([-1.5, 1.5]); ylim([-1.5, 1.5]); axis equal;
grid on;
if i == 1
title('4-正方形');
elseif i == 2
title('4-圆');
elseif i == 3
title('4-等边三角形');
else
title('4-正六边形');
end
end
5.分别用 plot 和 fplot 函数绘制下列分段函数的曲线。
% plot
x = -5 : 0.02 : 5;
f = [];
for i = x
if i > 0
f = [f, i^2+(1+i)^0.25+5];
elseif i == 0
f = [f, 0];
else
f = [f, i^3+sqrt(1-i)-5];
end
end
subplot(2, 1, 1);
plot(x, f); xlabel('x'); ylabel('y'); grid on; title('5-plot');
subplot(2, 1, 2);
fplot('fplot_5', [-5, 5, -140, 40]); xlabel('x'); ylabel('y'); grid on; title('5-fplot');
6. 在同一坐标轴中绘制下列两条曲线并标注两曲线交叉点。
t = 0 : pi/100 : pi;
x1 = sin(3*t).*cos(t);
y1 = sin(3*t).*sin(t);
x2 = cos(t);
y2 = 2*x2-0.5;
plot(x1, y1, x2, y2); xlabel('x'); ylabel('y'); grid on; title('6');
hold on;
[t, x, y] = solve('x=sin(3*t)*cos(t)', 'y=sin(3*t)*sin(t)', 'y=2*x-0.5')
plot(eval(x), eval(y), 'r*');
hold off;
7. 某工厂2005年度各季度产值(单位:万元)分别为:450.6, 395.9, 410.2, 450.9,绘制折线图和饼图,并说明图形的实际意义。
subplot(1, 1, 1); clear; clc;
x = 1 : 4;
y = [450.6, 395.9, 410.2, 450.9];
subplot(1, 2, 1);
plot(x, y);
title('6-折线图•四个季度产值变化'); xlabel('第i季度'); ylabel('产值/万元'); grid on; axis([0, 5, 360, 480]);
subplot(1, 2, 2);
pie(y);
title('6-饼图•每季度占总产值的百分比');
8. 根据 x^2/y^2 +y^2/(25-a^2 )=1 绘制平面曲线,并分析参数 a 对其形状的影响。
subplot(1, 1, 1); clear; clc;
subplot(2, 3, 1);
ezplot('x^2/y^2+y^2/(25-0^2)-1'); grid on;
subplot(2, 3, 2);
ezplot('x^2/y^2+y^2/(25-1^2)-1'); grid on;
subplot(2, 3, 3);
ezplot('x^2/y^2+y^2/(25-2^2)-1'); grid on;
subplot(2, 3, 4);
ezplot('x^2/y^2+y^2/(25-3^2)-1'); grid on;
subplot(2, 3, 5);
ezplot('x^2/y^2+y^2/(25-4^2)-1'); grid on;
subplot(2, 3, 6);
ezplot('x^2/y^2+y^2/(25-4.8^2)-1'); grid on;
9. 利用图形对象绘制下列曲线,要求先利用默认属性绘制曲线,然后通过图形句柄操作来改变曲线的颜色、线型和线宽,并利用文字对象给曲线添加文字标注。
% (1)
x = -20 : 0.1 : 20;
y = (1+x.^2)./(1+x.^4);
g = plot(x, y, 'k'); xlabel('x'); ylabel('y'); grid on; title('9-1');
set(g, 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
legend('y = (1+x^2)/(1+x^4)');
% (2)
x = -2*pi : pi/50 : 2*pi;
y = 3*x + sin(x) - exp(x);
g = plot(x, y, 'k'); xlabel('x'); ylabel('y'); grid on; title('9-2');
set(g, 'Color', 'b', 'LineStyle', '-', 'LineWidth', 1);
legend('y = 3x + sinx - e^x');
% (3)
theta = linspace(0, pi, 100);
rho = sin(2*theta);
g = polar(theta, rho, 'k'); title('9-3');
set(g, 'Color', 'b', 'LineStyle', '-', 'LineWidth', 1);
legend('{\rho} = sin2{\theta}');
% (4)
x = [linspace(-1, 0-0.001, 100), linspace(0.001, 1, 100)];
y = log((1+sqrt(1-x.^2))./x) + sqrt(1-x.^2);
g = plot(x, y, 'k'); xlabel('x'); ylabel('y'); grid on; title('9-4');
set(g, 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
legend('y = ln(1+sqrt(1-x^2))/x + sqrt(1-x^2)');
10. 利用图像对象绘制下列三维图形,要求与第9题相同。
% (1)
t = [-20 : 0.01 : -1-eps, -1+eps : 0.01 : 20];
x = 3*t./(1+t);
y = 3*t.^2./(1+t.^3);
g = plot(x, y, 'k'); xlabel('x'); ylabel('y'); grid on; title('10-1');
set(g, 'Color', 'r', 'LineStyle', '-', 'LineWidth', 2);
legend('x = 3t/(1+t)) : y = 3t^2/(1+t^3)');
% (2)
g = ezplot('y/(1+x^2+y^2)'); xlabel('x'); ylabel('y'); grid on; title('10-2');
set(g, 'Color', 'b', 'LineStyle', '-', 'LineWidth', 1);
legend('f(x,y) = y/(1+x^2+y^2)');
% (3)
x = -sqrt(pi) : pi/50 : sqrt(pi);
y = -sqrt(pi) : pi/50 : sqrt(pi);
[x, y] = meshgrid(x, y);
z = x.^2 + y.^2 - 5*sin(x.*y);
g = mesh(x, y, z, 'FaceColor', 'w', 'EdgeColor', 'flat');
xlabel('x'); ylabel('y'); zlabel('z'); grid on; title('10-3');
set(g, 'FaceColor', 'flat', 'LineStyle', '-');
% (4)
x = -10 : 0.1 : 10;
[x, y] = meshgrid(x);
z = x.*y.*exp(-x.^2-y.^2);
g = mesh(x, y, z);
xlabel('x'); ylabel('y'); zlabel('z'); grid on; title('10-4');
set(g, 'FaceColor', 'b', 'LineStyle', '-.', 'EdgeColor', 'flat');
11. 绘制一个长方形,将长方形3等份,每等份分别着不同的颜色。
vert = [0, 0; 1, 0; 2, 0; 3, 0; 3, 1; 2, 1; 1, 1; 0, 1];
fac = [1, 8, 7, 2; 2, 7, 6, 3; 3, 6, 5, 4];
mc = jet(3);
patch('Vertices', vert, 'Faces', fac, 'FaceVertexCData', mc, 'FaceColor', 'flat');
12. 生成一个长方形,每小面着不同颜色,并进行光照和材质处理。
vert = [0, 0, 0; 1, 0, 0; 1, 1, 0; 0, 1, 0; 0, 0, 1; 1, 0, 1; 1, 1, 1; 0, 1, 1];
fac = [1, 5, 6, 2; 2, 6, 7, 3; 3, 7, 8, 4; 4, 8, 5, 1; 1, 4, 3, 2; 5, 8, 7, 6];
mc = jet(6);
patch('Vertices', vert, 'Faces', fac, 'FaceVertexCData', mc, 'FaceColor', 'Flat');
% 顶点集,小面上顶点,
axis([-0.5, 2.5, -0.5, 2.5, -0.5, 2.5]); grid on; axis square;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');
title('神奇的方块');
light('Color', 'b', 'Style', 'local', 'Position', [1, 1, 1]);
lighting flat; % 均匀入射光
material shiny; % 镜面反射光
hold on;
plot3(2, 2, 2, 'p'); text(2, 2, 2, 'light');
hold off