第9章习题参考答案
1. 新建图形窗口,设置其标题为“对数函数的图像”,在该窗口中绘制对数函数
flnx在0x10的图像
参考答案:
>> figure,title('对数函数的图像'),plot(0:0.2:10, log(0:0.2:10))
2. 编写程序,实现功能为:创建图形窗口,并且设置其默认背景为黄色,默认线宽为
x2y2
4 个像素,在该窗口中绘制椭圆 221的图像,其中的a和b任选 ab
参考答案:
figure('Color','y');
set(gca,'DefaultLineLineWidth',4);
a = 4; b = 3;
x = linspace(-a,a,100);
y1 = sqrt((1-x.^2/(a^2))*b^2);
y2 = -sqrt((1-x.^2/(a^2))*b^2);
plot(x,y1);
hold on;
plot(x,y2);
3. 编写 MATLAB 程序,绘制下面的函数:
txtcosyt2sint2,其中2t2
该程序在绘制图形之后等待用户的鼠标输入,每单击其中一条曲线,就随机修改该曲线的颜色,包括红色、绿色、蓝色、黑色和黄色。
参考答案:
t = -2:0.1:2;
x = cos(t/pi);
y = 2*sin(t/(2*pi));
figure,plot(t,x);
hold on; plot(t,y);
%if waitforbuttonpress
while(1)
if waitforbuttonpress==0
get(gcf,'CurrentObject');
c = ceil((rand) * 5);
switch c
case 1
set(get(gcf,'CurrentObject'),'Color', 'r'); case 2
set(get(gcf,'CurrentObject'),'Color', 'g'); case 3
set(get(gcf,'CurrentObject'),'Color', 'b'); case 4
set(get(gcf,'CurrentObject'),'Color', 'k'); case 5
set(get(gcf,'CurrentObject'),'Color', 'y'); end
end
end