P 第一次实验答案
1. 设
要求以0.01秒为间隔,求出y 的151个点,并求出其导数的值和曲线。 clc clear
x=0:0.01:1.5;
y=sqrt(3)/2*exp(-4*x).*sin(4*sqrt(3)*x+pi/3) y1=diff(y) subplot(2,1,1) plot(x,y)
subplot(2,1,2) plot(x(1:150),y1)
2绘制极坐标系下曲线(a,b,n 自定数据)
clc clear a=10; b=pi/2; n=5;
theta=0:pi/100:2*pi; rho=a*cos(b+n*theta); polar(theta,rho)
3. 列出求下列空间曲面交线的程序
clc clear
x=[-5:0.5:5];
[X,Y]=meshgrid(x); z1=X.^2-2*Y.^2;
z2=X.*2-Y.*3; xlabel('x') ylabel('y') zlabel('z') surf(X,Y,z1) hold on
surf(X,Y,z2)
k=find(abs(z1-z2)<0.5); x1=X(k) y1=Y(k)
z3=x1.^2-2*y1.^2 hold on
plot3(x1,y1,z3,'*')
4、设 ???
??
?++=)1(sin 35.0cos 2x x x y 把x=0~2π间分为101点,画出以x 为横坐标,y
为纵坐标的曲线,要求有图形标注。
clc clear
x=-2*pi:0.1: 2*pi;
y=cos(x).*(0.5+sin(x)*3./(1+x.^2)); plot(x,y,'b*-'); title('绘图'); xlabel('x 坐标'); ylabel('y 坐标'); legend('原函数')
gtext('y=cos(x)(0.5+3*sin(x)/(1+x^2))')
5、求下列联立方程的解 8102569
583
2475412743-=+-+-=-+-=++-=--+w z y x w z x w z y x w z y x
clc clear
a=[3,4,-7,-12;5,-7,4,2;1,0,8,-5;-6,5,-2,10]; b=[4,-3,9,-8]; c=b/a; x=c(1,1) y=c(1,2) z=c(1,3) w=c(1,4)
6. 假设一曲线数据点为
x = 0:2:4*pi;
y = sin(x).*exp(-x/5);
试将x 的间距调成 0.1,采用不同插值方法进行插值,并通过子图的形式将不同插值结果和原始数据点绘制在同一图形窗口。 clc clear
x=0:2:4*pi;
y=sin(x).*exp(-x./5); x1=0:0.1:4*pi;
y01=interp1(x,y,x1,'spline'); y02=interp1(x,y,x1,'cubic'); y03=interp1(x,y,x1,'nearest'); y04=interp1(x,y,x1); subplot(3,2,1); plot(x,y,'o-');
title('?-oˉêy');
??
?
??+?=-334sin 234πt e y t ()θρn b a +=cos 2
212y x z -=y x z 322-=
subplot(3,2,2);
plot(x1,y01,'gx-');
title('spline');
subplot(3,2,3);
plot(x1,y02,'m+-');
title('cubic');
subplot(3,2,4);
plot(x1,y03,'r*-');
title('nearest');
subplot(3,2,5);
plot(x1,y04,'kd-');
title('line');
第二次试验答案
1、编制m文件,等待键盘输入,输入密码
20120520,密码正确,显示输入密码正确,程序结束;否则提示,重新输入。
clear
a=input('输入密码:')
while a~=20120520
disp('密码错误,请重新输入:')
clear
a=input('输入密码:')
end
if a==20120520
disp('密码输入正确!')
end
2、编写一个函数实现以下功能:
a. 若没有实参输入变量,则用蓝色实线画出
半径为1的圆;
b.若有两个实参输入变量N,R,则用蓝色实
线画出外接半径为R的正N多边形;
d. 若有两个实参输出变量,则分别输出正多
边形的面积S和周长L。
g. 如果输入变量多于三个或输出变量多于两
个则给出错误提示信息。
(提示:外接半径为R的正N多边形参数
式:t=0:2*pi/N:2*pi;x=R*sin(t);y=R*cos(t)当N≥100时,可看作是半径为R的圆)
function [S,L]=shiyan22(N,R,str)
switch nargin
case 0
N=100;R=1;str='-b';
case 1
R=1;str='-b';
case 2
str='-b';
case 3
;
otherwise
error('输入量太多。');
end;
t=0:2*pi/N:2*pi;
x=R*sin(t);y=R*cos(t);
if nargout==0
plot(x,y,str);
elseif nargout>2
error('输入量太多。');
else
S=N*R*R*sin(2*pi/N)/2;
L=2*N*R*sin(pi/N);
plot(x,y,str)
end
axis equal square
box on
3、编写一个学生成绩管理的程序,完成下列功能:
(1) 构造一个学生结构,包括学生的姓名,课
程名称(限M门),考试成绩和平均值等域;
(2) 从键盘上,连续输入N个学生的情况,分
别将这些学生的情况填入到相应的域,若域中
没有值即为空或0,并分别计算其平均值写入
到其平均值域。
(3) 根据平均值排序(由高到低),并输出学
生的姓名和平均值。
clc
clear
n=input('please input students number:');
for x=1:n
number(x)=struct('name','','Curriculum1','','Curric ulum2','','Curriculum3','','Average','');
end
for i=1:n
number(i).name=input('name:','s');
number(i).Curriculum1=input('please input the scores\nCurriculum1:');
number(i).Curriculum2=input('Curriculum2:');
number(i).Curriculum3=input('Curriculum3:');
number(i).Average=(number(i).Curriculum1+number(i). Curriculum2+number(i).Curriculum3)/3;
disp('the average is:')
disp(num2str(number(i).Average))
end
NameCell=cell(1,n);
Array=[1,n];
for i=1:n
NameCell(1,i)={number(i).name};
Array(i)=number(i).Average;
end
for j=1:(n-1)
iptr = j;
for i=(j+1):n
if Array(i)>Array(iptr) %比较相邻前后大小? iptr=i;
end
end
if j~=iptr %若后面比前面大,互换
Name=NameCell(1,i);
NameCell(1,i)=NameCell(1,j);
NameCell(1,j)=Name;
average=Array(i);
Array(i)=Array(j);
Array(j)=average;
j=1;
iptr=j;
end
end
disp('成绩排序如下:')
for i=1:n disp(strcat('名次: ',num2str(i),' 名字
',NameCell(1,i),' 平均成绩: ',num2str(Array(i)))) end
4、使用句柄图像对象绘制曲线:
y=2e-0.5x cos(πx),同时对曲线进行标注和修饰。
x=-12:0.02:12
y=2*exp((-0.5)*x).*cos(pi*x)
h_f=figure('Position',[200 300 300
300],'menubar','none')
h_a1=axes('position',[0.1,0.1,.8,.8])
h_t=title(h_a1,'函数=2*exp((-0.5)*x).*cos(pi*x)')
h_1=line(x,y)
set(gca,'xtick',[(-6)*pi (-4)*pi (-2)*pi 0 (2)*pi (4)*pi (6)*pi])
set(gca,'xticklabel',{'(-6)*pi',' (-4)*pi',
'(-2)*pi', '0', '(2)*pi', '(4)*pi','(6)pi'})
set(gca,'xgrid','on','ygrid','on')
set(h_1,'linewidth',2)
set(get(h_t,'parent'),'color','y')
h_anm1=annotation(gcf,'rectangle',[0.1 0.5 .8
0.4],'FaceAlpha',.7,'FaceColor','red')
第三次试验答案
1、做一个带按钮的界面,当按动“播放”按
钮时调入声音文件并播放,显示声音波形,并建立一个用于关闭界面的按钮对象。(提示,找一个.wav文件,简单起见可以在windows 目录下找一个文件,将其放在当前工作目录下或搜索路径上。具体用法请参照:
[y,f,b]=wavread(*.wav'); % 读入声音文件
sound(y,f,b) % 由声卡播放声音
plot(y) % 画出波形
2、创建一个用于绘图参数选择的菜单对象,
其中包含三个选项LineStyle、Marker和
Color,每个选项下面又包含若干的子项分别
可以进行选择图线的类型、标记点的类型和颜
色 (每个子项不少于3个),当按下“绘图”
按钮时,根据选项绘制正弦曲线(缺省时为蓝
色无标记实线)。(注意使用全球变量)
function varargout = shiyan32(varargin)
% SHIYAN32 MATLAB code for shiyan32.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton',
gui_Singleton, ...
'gui_OpeningFcn',
@shiyan32_OpeningFcn, ...
'gui_OutputFcn',
@shiyan32_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1}); end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before shiyan32 is made visible. function shiyan32_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to shiyan32 (see VARARGIN)
% Choose default command line output for shiyan32 handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes shiyan32 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
global c
c='b'
global l
l='-'
global m
m=' '
% --- Outputs from this function are returned to the command line.
function varargout = shiyan32_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%
--------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global l
%
---------------------------------------------------
-----------------
function Untitled_8_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global m
%
--------------------------------------------------------------------
function color_Callback(hObject, eventdata, handles) % hObject handle to color (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
%
--------------------------------------------------------------------
function Untitled_14_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
c='r'
%
--------------------------------------------------------------------
function Untitled_15_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_15 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
c='b'
%
--------------------------------------------------------------------
function Untitled_16_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
c='g'
%
--------------------------------------------------------------------
function Untitled_17_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_17 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
c='y'
%
--------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global m
m='o'
%
--------------------------------------------------------------------
function Untitled_11_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global m
m='*'
%
--------------------------------------------------------------------
function Untitled_13_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_13 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global m
m='d'
%
--------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global l
l='-'
%
--------------------------------------------------------------------
function Untitled_5_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global l
l='--'
%
--------------------------------------------------------------------
function Untitled_7_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global l
l='-.'
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
global s
global l
global m
global c
x=0:0.1:2*pi;
y=sin(x);
s=strcat(l,m,c);
plot(x,y,s)
2. 设10
10
)
(
?
=
j i
a
A和
10
10
)
(
?
=
j i
b
B是两个10行10列的矩阵(数组),试说明命令A*B, A/B, A\B, A.*(B.^A), A./B, A.\B和sin(A).*exp(B)的涵义。A,B的矩阵乘法,矩阵A乘以B的逆,矩阵B乘以A的逆,B的每个元素的对应A次幂乘以A对应的每个元素
A的每个元素除以B对应的每个元素,B的每个元素除以A对应的每个元素,
A的每个元素的正弦与B对应的每个元素的指数求积,3. 设A为1417
?数组,B为一个行数大于3的数组,请给出
(1)删除A的第4、8、12三列的命令;(2)删除B的倒数第3行的命令;
(3)求符号极限
tan()
lim
x
mx
nx
→
的命令集;(4)求
2
3
arctan ln(1)
2
x
x
y e
x
-
+
=-+
-
的3阶导数的命令集;(5)计算定积分
12
1
(sin)
x x x dx
-
+
?的命令,并给出计算结果(!)A(:,[4 8 12])=[]
(2)B(end-2)=[]
(3)syms m n x
limit(tan(m*x)/(n*x))
(4)syms x y
y=atan((x+3)/(x-2))-log(1+exp((-2)*x)
diff(y,3)
(5)syms x
int((abs(x)+sin(x))*x^2,-1,1)
三.基本编程题(每小题10分,共
30分)
1. Write a program to compute the following
expressions 10
()()
,,1,2,,10
ik
i jk j ij x
x x x r i j --=
=∑L ;
Here, we suppose the variable x has existed in the workspace.
for i=1:10 for j=1:10
xbar(i)=xbar(i)+x(i,j); end
xbar(i)=xbar(i)/10; end
for i=1:10 for j=1:10
t1=0;t2=0;t3=0; for k=1:3
t1=t1+(x(i,k)-xbar(i))*(x(j,k)-xbar(j)); t2=t2+(x(i,k)-xbar(i))^2; t3=t3+(x(j,k)-xbar(j))^2; end
r2(i,j)=t1/sqrt(t2*t3); end end r2
2. (1) Using plot() function to plot the curves of
2[sin(2)cos3]e x y x x -=+ and sin x
z x
=
in the range of []2,
2x ππ∈-, let their color are
red and green respectively, and add the grid to the figure. (2) Using fplot() function and ezplot() function to plot above-mentioned (上述的) curves respectively.
(1)x=-2*pi:pi/100:2*pi;
y=(sin(2*x)+cos(3*x)).*exp(-2*x); z=sin(x)/x;
plot(x,y,’r ’,x,z,’g ’)
(2)fplot('[(sin(2*x)+cos(3*x)).*exp(-2*x), sin(x)/x]',[-2*pi 2*pi])
3. Plot the 3D mesh figure and 3D surface figure of the function
2
2
2/2(1)(,)9(1)e x
y f x y x --+=-
in the range of 44x -≤≤ and 44y -≤≤, respectively. x=-4:1/100:4; y=-4:1/100:4;
z=9(1-x)^2*exp(-x ’^2/2-(y ’+1)^2) mesh(x,y,z); surf(x,y,z);
四.综合编程题(每小题11分,
1. Write a function program to compute the following function
1.8
|2|!,
()f1(x)34sin(2),i x i x x =?=?+?
整数其他
and call this function program to compute the value
of y=f1(-4)+f1(3)-f1(14.37) in main program or command window, there, ||!i shows the factorial (阶乘) of ||i . 提示:对x 取整的函数有fix(x), floor(x), round(x) 和ceil(x) .
fuction y=f1(x) %f1.m if x-fix(x)= =0 y=prod(1:abs(2*x)); else
y=4*sin(2*x)+x^1.8; y=f1(-4)+f1(3)-f1(14.37)
2.Write a program to compute the sample mean
1
1xbar ==n
i i x x n =∑,
and the sample skewness (偏度)
31
1ske ()n
i i x x n ==-∑ .
for the following three case: (1) n and ],,,[21n x x x x Λ= exist in Workspace of Matlab. It is no any limit;
(2) n and ],,,[21n x x x x
Λ= are unknown variables.
Please input x by keyboard one by one, and give a mark of end the data input. Examinees can use loop
statement and length() function and input() function, but cannot use mean() function, std() function, and var() function directly (可以使用循环语句和length()函数、input()函数等进行编程,但不能直接使用mean()、std()和var()等函数).
(1)s=0;
for i=1:n
s=s+x(i);
end
xbar=s/n
m=0;
for i=1:n
m=m+(x(i)-xbar)^3;
end
ske=m/n
(2)x(1)=input('请输入x ');
i=1;s=0;
while(x(i)~='a')
s=s+x(i);
i=i+1;
x(i)=input('请输入x ,输’a’结束');
end
xbar=s/(i-1)
m=0;
for j=1:i-1
m=m+(x(i)-xbar)^3;
end
ske=m/(i-1)
一、填空题
1、MATLAB常用操作界面包括命令窗口、工作空间窗口、命令历史窗口、当前目录窗口、内存数组编辑器、M文件编辑/调试器、帮助导航/浏览器、图形窗口等。
2、MATLAB中Inf或inf表示无穷大、NaN或nan表示不是一个数、nargout表示函数输出变量数目。
3、工作空间浏览器主要用于内存变量的查阅、保持和编辑。
4、MATLAB实现将全下标转换为单下标的指令为sub2ind 、据单下标换算出全下标的指令为ind2sub 。
5、MATLAB中clf用于清除图形窗口、clc用于清除指令窗口中显示内容、clear用于清除MATLAB工作空间中保存的变量。
二、简答题(每题5分,共20分)
1、简述MATLAB历史指令窗的主要作用。
答:历史指令窗记录着用户在MATLAB指令窗中所输入过
的所有指令。历史记录包括:每次开启MATLAB的时间,
每次开启MATLAB后在指令窗中运行过的所有指令。应用
功能有单行或多行指令的复制和运行、生成M文件等。
2、简述MATLAB函数的基本结构。
答:典型M函数文件的结构:函数申明行、H1行、在线
帮助文本区、编写和修改记录、函数体。
3、简述绘制二维图形的一般步骤。
绘制二维图形的一般步骤为:曲线数据准备、选定图形窗
及子图位置、调用二维曲线绘图指令、设置轴的范围、坐
标分格线、图形注释、图形的精细操作。
三、阅读程序并回答问题(每题4分,共28分)
1、写出下列指令运行结果。
A=zeros(2,4);
A(:)=1:8;
s=[2 3 5];
A(s)
Sa=[10 20 30]'
A(s)=Sa
ans =
2 3 5
Sa =
10
20
30
A =
1 20 30 7
10 4 6 8
2、写出下列指令运行结果。
A=reshape(1:16,2,8)
reshape(A,4,4)
s=[1 3 6 8 9 11 14 16];
A(s)=0
A =
1 3 5 7 9 11 13 15
2 4 6 8 10 12 14 16
ans =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
A =
0 0 5 7 0 0 13 15
2 4 0 0 10 12 0 0
3、写出下列指令运行结果。
A=[1,2;3,4];
B=[-1,-2;2,1];
S=3;
A.*B
ans =
-1 -4
6 4
A*B
ans =
3 0
5 -2
S.*A
ans =
3 6
9 12
S*B
ans =
-3 -6
6 3
4、下面的函数主要完成什么功能?
function f=factor(n)
if n<=1
f=1;
else
f=factor(n-1)*n;
end
利用函数的递归调用求n!
5、写出下列指令运行结果。
c h=‘ABc123d4e56Fg9’;
subch=ch(1:5)
revch=ch(end:-1:1)
k=find(ch>=‘a’&ch<=‘z’);
ch(k)=ch(k)-(‘a’-‘A’);
char(ch)
subch =
ABc12
revch =
9gF65e4d321cBA
ans =
ABC123D4E56FG9
ans =
4
6、写出下列指令运行结果。
A(1,1)={'this is cell'};
A{1,2}={[1 2 3;4 5 6]};
A{2,1}=[1+2*i];
A{2,2}=A{1,2}{1}+(A{1,2}{1}(1,1)+A{1,2}{1}(2, 2));
celldisp(A)
A{1,1} =
this is cell
A{2,1} =
1.0000 +
2.0000i
A{1,2}{1} =
1 2 3
4 5 6
A{2,2} =
7 8 9
10 11 12
7、下面的程序完成功能是什么?
t=0:pi/50:4*pi;
y0=exp(-t/3);
y=exp(-t/3).*sin(3*t);
plot(t,y,'-r',t,y0,':b',t,-y0,':b')
xlabel(‘\bf\it t’);
ylabel(‘\bf\it y’);
grid on;
绘制图形如下:
四、编程题(32分)
1、用命令来创建GUI并绘制方程y=ax2 +bx+c图形,需要显示绘图结果的坐标系窗口,还能输入a、b、c的值和x的取值范围,同时用命令设置所以对象的属性。
2、在同一图上分别用红色实线和绿色虚线绘制
y1=sin(x)和y2=cos(x)在区间[0,4*pi]的曲线,并
用星号*标出两条曲线的交点以及建立图例。(5分)x=0:0.01:4*pi;
y1=sin(x);
y2=cos(x);
z=find(abs(y1-y2)<0.007);
plot(x,y1,'r.-','Linewidth',2,'Markersiz e',1);
axis([-1.5,15,-1.5,1.5]);
hold on
plot
(x,y2,'g.:','Linewidth',2,'Markersize',1);
n=size(z);
plot(x(z),y1(z),'k*','Markersize',15);
3、某商场对顾客所购买的商品实行打折销售,
标准如下(商品价格用price来表示):
price<200 没有折扣
200≤price<500 3%折扣
500≤price<1000 5%折扣
1000≤price<2500 8%折扣
2500≤price<5000 10%折扣
5000≤price 14%折扣
输入所售商品的价格,求其实际销售价格。(5分)
price=input('please input your price:'); a=0;
if price<200 a=1;
elseif price<500 a=2;
elseif price<1000 a=3;
elseif price<2500 a=4;
elseif price<5000 a=5; else a=6; end
switch a case 0
disp('0 inputs given'); case 1
b=price; case 2
b=0.97*price; case 3
b=0.95*price; case 4
b=0.92*price; case 5
b=0.90*price; case 6
b=0.86*price; otherwise
disp('unexpected input!'); end
fprintf('the actaul price is %f',b); 4、二阶微分方程x"+0.2x'+0.4x=0.2u (t), 其中u(t)是单位阶跃函数,试建立系统模型并仿真。
1. 请编写一个M-函数,用来实现级数和
1
253132-+++++=n x x x x S n
Λ
并利用该M-函数计算n = 学号末2位数+5,x = n/60时
S 的值。 1.
function s=zh(x,n)
if nargin>2,error('wrong');end
if nargout>1,error('wrong');end ss=1;
for t=1:n,ss=ss+(x^t)./(2*t-1); s=ss;end
>> zh(35/60,35)
ans =
1.7677
2. 已知系统的框图如下图所示,请推导出从输入信号
r(t)到输出信号y(t)的总系统模型。 2.
syms G1 G2 G3 G4 G5 G6 G7 H1 H2 H3 H4 c1=feedback(G4,H4); c2=feedback(G3*G2,H2); c3=feedback(G5*c1,H3);
G=feedback((G6+G7)*c3*c2*G1,H1)
3. 已知单位负反馈控制系统的被控对象及控制器的传递函数分别为
)
20/1)(5.0/1()
5.2/1(16)(s s s s s G +++=
, )23)(5.1()2)(1(20)(++++=s s s s s Gc 试判断系统的稳定性,并用时域响应检验得出的结论。 >> s=tf('s')
Transfer function: s
>> Gs=16*(1+s/2.5)/[s*(1+s/0.5)*(1+s/20)]
Transfer function: 6.4 s + 16
---------------------- 0.1 s^3 + 2.05 s^2 + s
>> Gc=20*(s+1)*(s+2)/[(s+1.5)*(s+23)]
Transfer function: 20 s^2 + 60 s + 40 ------------------- s^2 + 24.5 s + 34.5 >> GG=feedback(Gs*Gc,1)
Transfer function:
128 s^3 + 704 s^2 + 1216 s + 640 --------------------------------------------------------
0.1 s^5 + 4.5 s^4 + 182.7 s^3 + 799.2 s^2 + 1251 s + 640
>> eig(GG)
ans =
-20.1421 +34.9785i -20.1421 -34.9785i -1.8264 + 0.5997i -1.8264 - 0.5997i -1.0630
根都有负实部 则系统稳定
>> step(GG,1)
4. 考虑简单的线性微分方程
)2sin(2413453''')3()4(t e e y y y y y t t --+=++++,
且方程的初值为y(0)=1,y ’(0)=y ”(0)=1/2,
y (3)(0)=0.6,,请用Simulink 搭建起系统的仿真模型,并绘制出仿真结果曲线。
2.6 求??
?
?
??+-+-+-+-++=i 44i 93i 49i 67i 23i 57i 41i 72i 53i 84x 的共轭转置。
>> x=[4+8i 3+5i 2-7i 1+4i 7-5i;3+2i 7-6i 9+4i 3-9i 4+4i];
>> x’ ans =
4.0000 - 8.0000i 3.0000 - 2.0000i
3.0000 - 5.0000i 7.0000 + 6.0000i 2.0000 + 7.0000i 9.0000 -
4.0000i 1.0000 - 4.0000i 3.0000 + 9.0000i 7.0000 +
5.0000i 4.0000 - 4.0000i
2.7 计算????
??=572396a 与??
?
???=864142b 的数组乘积。 >> a=[6 9 3;2 7 5];
>> b=[2 4 1;4 6 8]; >> a.*b ans =
12 36 3 8 42 40
2.9 对于B AX =,如果????
?
?????=753467294A ,??
????????=282637B ,求解X 。
>> A=[4 9 2;7 6 4;3 5 7]; >> B=[37 26 28]’; >> X=A\B X =
-0.5118 4.0427 1.3318
2.10 已知:??
??
?
?????=987654321a ,分别计算a 的数组平方和矩阵平方,并观察其结果。
>> a=[1 2 3;4 5 6;7 8 9]; >> a.^2 ans =
1 4 9 16 25 36 49 64 81 >> a^
2 ans =
30 36 42 66 81 96 102 126 150
2.11 ????
??-=463521a ,??
?
???-=263478b ,观察a 与b 之间的六种关系运算的结果。
>> a=[1 2 3;4 5 6]; >> b=[8 –7 4;3 6 2]; >> a>b
ans =
0 1 0
1 0 1
>> a>=b
ans =
0 1 0
1 0 1
>> a
ans =
1 0 1
0 1 0
>> a<=b
ans =
1 0 1
0 1 0
>> a==b
ans =
0 0 0
0 0 0
>> a~=b
ans =
1 1 1
1 1 1
2.13 在sin(x)运算中,x是角度还是弧度?
在sin(x)运算中,x是弧度,MATLAB规定所有的三角函数运算都是按弧度进行运算。
2.14 角度[]60
45
30
=
x,求x的正弦、余弦、正切和余切。
>> x=[30 45 60];
>> x1=x/180*pi;
>> sin(x1)
ans =
0.5000 0.7071 0.8660
>> cos(x1)
ans =
0.8660 0.7071 0.5000
>> tan(x1)
ans =
0.5774 1.0000 1.7321
>> cot(x1)
ans =
1.7321 1.0000 0.5774
2.15 用四舍五入的方法将数组[2.4568 6.3982
3.9375 8.5042]取整。
>> b=[2.4568 6.3982 3.9375 8.5042];
>> round(b)
ans =
2 6 4 9
2.16 矩阵
?
?
?
?
?
?
?
?
?
?
=
7
2
8
3
6
5
2
1
9
a,分别对a进行特征值分解、
奇异值分解、LU分解、QR分解及Chollesky分解。
>> [v,d]=eig(a,b)
v =
-0.4330 -0.2543 -0.1744
-0.5657 0.9660 -0.6091
-0.7018 0.0472 0.7736
d =
13.5482 0 0
0 4.8303 0
0 0 3.6216
>> a=[9 1 2;5 6 3;8 2 7];
>> [u,s,v]=svd(a)
u =
-0.5601 0.5320 -0.6350
-0.4762 -0.8340 -0.2788
-0.6779 0.1462 0.7204
s =
15.5234 0 0
0 4.5648 0
0 0 3.3446
v =
-0.8275 0.3917 -0.4023
-0.3075 -0.9156 -0.2592
-0.4699 -0.0907 0.8781
>> [l,u]=lu(a)
l =
1.0000 0 0
0.5556 1.0000 0
0.8889 0.2041 1.0000
u =
9.0000 1.0000 2.0000
0 5.4444 1.8889
0 0 4.8367
>> [q,r]=qr(a)
q =
-0.6903 0.3969 -0.6050
-0.3835 -0.9097 -0.1592
-0.6136 0.1221 0.7801
r =
-13.0384 -4.2183 -6.8260
0 -4.8172 -1.0807
0 0 3.7733
>> c=chol(a)
c =
3.0000 0.3333 0.6667
0 2.4267 1.1447 0 0 2.2903
2.17 将矩阵????
??=5724a 、??????=3817b 和??
?
???=2695c 组合成两个新矩阵:
(1)组合成一个4?3的矩阵,第一列为按列顺序排列的a 矩阵元素,第二列为按列顺序排列的b 矩阵元素,第三列为按列顺序排列的c 矩阵元素,即 ??
???
????
???237
912685
574 (2)按照a 、b 、c 的列顺序组合成一个行矢量,即 []296531877254
>> a=[4 2;5 7]; >> b=[7 1;8 3]; >> c=[5 9;6 2]; % (1)
>> d=[a(:) b(:) c(:)] d =
4 7
5 5 8
6 2 1 9
7 3 2 % (2)
>> e=[a(:);b(:);c(:)]' e =
4 5 2 7 7 8 1 3 5 6 9 2
或利用(1)中产生的d >> e=reshape(d,1,12) ans =
4 5 2 7 7 8 1 3 5 6 9 2
第3章 数值计算基础
3.1 将(x -6)(x -3)(x -8)展开为系数多项式的形式。 >> a=[6 3 8]; >> pa=poly(a);
>> ppa=poly2sym(pa) ppa =
x^3-17*x^2+90*x-144
3.2 求解多项式x 3-7x 2
+2x +40的根。 >> r=[1 -7 2 40]; >> p=roots(r); -0.2151 0.4459 0.7949
0.2707
3.3 求解在x =8时多项式(x -1)(x -2) (x -3)(x -4)的值。
>> p=poly([1 2 3 4]); >> polyvalm(p,8) ans = 840
3.4 计算多项式乘法(x 2+2x +2)(x 2
+5x +4)。 >> c=conv([1 2 2],[1 5 4]) c =
1 7 16 18 8
3.5 计算多项式除法(3x 3+13x 2
+6x +8)/(x +4)。 >> d=deconv([3 13 6 8],[1 4]) d =
3 1 2
3.6 对下式进行部分分式展开:
2
7243645232345234+++++++++x x x x x x x x x
>> a=[1 3 4 2 7 2]; >> b=[3 2 5 4 6];
>> [r,s,k]=residue(b,a) r =
1.1274 + 1.1513i 1.1274 - 1.1513i -0.0232 - 0.0722i -0.0232 + 0.0722i 0.7916 s =
-1.7680 + 1.2673i -1.7680 - 1.2673i 0.4176 + 1.1130i 0.4176 - 1.1130i -0.2991 k = []
3.7 计算多项式9514124234++--x x x x 的微分和积分。
>> p=[4 –12 –14 5]; >> pder=polyder(p); >> pders=poly2sym(pder) >> pint=polyint(p); >> pints=poly2sym(pint) pders =
12*x^2-24*x-14 pints =
x^4-4*x^3-7*x^2+5*x
3.8 解方程组????
?
?????=??????????66136221143092x 。
>> a=[2 9 0;3 4 11;2 2 6];
>> b=[13 6 6]';
>> x=a\b
x =
7.4000
-0.2000
-1.4000
3.9 求欠定方程组???
???=??????5865394742x 的最小范数解。 >> a=[2 4 7 4;9 3 5 6]; >> b=[8 5]'; >> x=pinv(a)*b x = -0.2151 0.4459
0.7949
0.2707
3.10 有一组测量数据如下表所示,数据具有y =x 2
的
>> x=[1 1.5 2 2.5 3 3.5 4 4.5 5]' >> y=[-1.4 2.7 3 5.9 8.4 12.2 16.6 18.8 26.2]' >> e=[ones(size(x)) x.^2] >> c=e\y >> x1=[1:0.1:5]'; >> y1=[ones(size(x1)),x1.^2]*c; >> plot(x,y,'ro',x1,y1,'k')
3.11 矩阵?????
?????-=943457624a ,计算a 的行列式和逆矩阵。 >> a=[4 2 -6;7 5 4 ;3 4 9]; >> ad=det(a) >> ai=inv(a) ad = -64 ai = -0.4531 0.6562 -0.5937 0.7969 -0.8437 0.9062 -0.2031 0.1562 -0.0937 3.12 y =sin(x ),x 从0到2π,?x =0.02π,求y 的最大值、最小值、均值和标准差。
>> x=0:0.02*pi:2*pi;
>> y=sin(x);
>> ymax=max(y)
>> ymin=min(y)
>> ymean=mean(y)
>> ystd=std(y)
ymax =
1
ymin = -1 ymean =
2.2995e-017 ystd = 0.7071
3.13 []54321=x ,[]108642=y ,计算x 的协方差、y 的协方差、x 与y 的互协方差。
>> x=[1 2 3 4 5];
>> y=[2 4 6 8 10];
>> cx=cov(x) >> cy=cov(y)
>> cxy=cov(x,y)
cx = 2.5000
cy = 10 cxy = 2.5000 5.0000
5.0000 10.0000 3.14 参照例3-20的方法,计算表达式
()
2
2e 1053y x y x z ---=的梯度并绘图。 >> v = -2:0.2:2;
>> [x,y] = meshgrid(v);
>> z=10*(x.^3-y.^5).*exp(-x.^2-y.^2); >> [px,py] = gradient(z,.2,.2);
>> contour(x,y,z)
>> hold on
>> quiver(x,y,px,py) >> hold off 3.15 有一正弦衰减数据y=sin(x).*exp(-x/10),其中x=0:pi/5:4*pi ,用三次样条法进行插值。 >> x0=0:pi/5:4*pi;
>> y0=sin(x0).*exp(-x0/10); >> x=0:pi/20:4*pi; >> y=spline(x0,y0,x);
>> plot(x0,y0,'or',x,y,'b')
第4章 符号数学基础
4.1 创建符号变量有几种方法?
MATLAB 提供了两种创建符号变量和表达式的函数:sym 和syms 。
sym 用于创建一个符号变量或表达式,用法如x=sym(‘x’) 及 f=sym(‘x+y+z’),syms 用于创建多个符号变量,用法如syms x y z 。
f=sym(‘x+y+z’) 相当于
syms x y z f= x+y+z
4.2 下面三种表示方法有什么不同的含义? (1)f=3*x^2+5*x+2 (2)f='3*x^2+5*x+2' (3)x=sym('x') f=3*x^2+5*x+2 (1)f=3*x^2+5*x+2 表示在给定x 时,将3*x^2+5*x+2的数值运算结果赋值给变量f ,如果没有给定x 则指示错误信息。
(2)f='3*x^2+5*x+2'
表示将字符串'3*x^2+5*x+2'赋值给字符变量f ,没有任何计算含义,因此也不对字符串中的内容做任何分析。
(3)x=sym('x') f=3*x^2+5*x+2 表示x 是一个符号变量,因此算式f=3*x^2+5*x+2就具有了符号函数的意义,f 也自然成为符号变量了。
4.3 用符号函数法求解方程a t 2
+b*t +c=0。 >> r=solve('a*t^2+b*t+c=0','t') r =
[ 1/2/a*(-b+(b^2-4*a*c)^(1/2))] [ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
4.4 用符号计算验证三角等式:
sin(?1)cos(?2)-cos(?1)sin(?2) =sin(?1-?2) >> syms phi1 phi2; >>
y=simple(sin(phi1)*cos(phi2)-cos(phi1)*sin(phi2))
y =
sin(phi1-phi2)
4.5 求矩阵??
?
?
??=22211211a a a a A 的行列式值、逆和特征根。 >> syms a11 a12 a21 a22;
>> A=[a11,a12;a21,a22]
>> AD=det(A) % 行列式 >> AI=inv(A) % 逆 >> AE=eig(A) % 特征值 A =
[ a11, a12] [ a21, a22] AD =
a11*a22-a12*a21 AI = [ -a22/(-a11*a22+a12*a21), a12/(-a11*a22+a12*a21)]
[ a21/(-a11*a22+a12*a21), -a11/(-a11*a22+a12*a21)]
AE =
[ 1/2*a11+1/2*a22+1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)]
[ 1/2*a11+1/2*a22-1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)]
4.6 因式分解:6555234-++-x x x x >> syms x;
>> f=x^4-5*x^3+5*x^2+5*x-6; >> factor(f) ans =
(x-1)*(x-2)*(x-3)*(x+1)
4.7 ?
???????=)sin()log(12
x x e
x x
a f ax ,用符号微分求df/dx 。
>> syms a x; >> f=[a, x^2, 1/x; exp(a*x), log(x), sin(x)]; >> df=diff(f) df =
[ 0, 2*x, -1/x^2] [ a*exp(a*x), 1/x, cos(x)]
4.8 求代数方程组?????=+=++0
2y x c by ax 关于x,y 的解。
>>
S=solve('a*x^2+b*y+c=0','b*x+c=0','x','y');
>> disp('S.x=') , disp(S.x) >> disp('S.y=') , disp(S.y) S.x= -c/b S.y=
-c*(a*c+b^2)/b^3
4.9 符号函数绘图法绘制函数x=sin(3t)cos(t),y=sin(3t)sin(t)的图形,t 的变化范围为[0,2π]。
>> syms t
>>
ezplot(sin(3*t)*cos(t),sin(3*t)*sin(t),[0,pi])
4.10 绘制极坐标下sin(3*t)*cos(t)的图形。 >> syms t
>> ezpolar(sin(3*t)*cos(t)
第5章 基本图形处理功能
5.1 绘制曲线13++=x x y ,x 的取值范围为[-5,5]。 >> x=-5:0.2:5; >> y=x.^3+x+1; >> plot(x,y)
5.2 有一组测量数据满足-at e =y ,t 的变化范围为0~10,用不同的线型和标记点画出a=0.1、a=0.2和a=0.5三种情况下的曲线。
>> t=0:0.5:10; >> y1=exp(-0.1*t); >> y2=exp(-0.2*t); >> y3=exp(-0.5*t);
>> plot(t,y1,'-ob',t,y2,':*r',t,y3,'-.^g')
5.3 在5.1题结果图中添加标题-at e =y ,并用箭头线标识出各曲线a 的取值。
>> title('\ity\rm=e^{-\itat}') >>
title('\ity\rm=e^{-\itat}','FontSize',12)
>>
text(t(6),y1(6),'\leftarrow\ita\rm=0.1','FontSize ',11)
>>
text(t(6),y2(6),'\leftarrow\ita\rm=0.2','FontSize ',11)
>>
text(t(6),y3(6),'\leftarrow\ita\rm=0.5','FontSize ',11)
5.4 在5.1题结果图中添加标题-at e =y 和图例框。
>>
title('\ity\rm=e^{-\itat}','FontSize',12)
>> legend('a=0.1','a=0.2','a=0.5')
5.5表中列出了4个观测点的6次测量数据,将数据绘制成为分组形式和堆叠形式的条形图。
>> y=[3 6 9 6;6 7 7 4;7 3 2 3;4 2 5 2;2 4 8 7;8 7 4 4];
>> bar(y)
>> bar(y ,’stack’)
5.6 x= [66 49 71 56 38],绘制饼图,并将第五个切块分离出来。
>> x=[66 49 71 56 38]; >> L=[0 0 0 0 1]; >> pie(x,L)
5.7 2
2
y x xe z --=,当x 和y 的取值范围均为-2到2时,用建立子窗口的方法在同一个图形窗口中绘制出三维线图、网线图、表面图和带渲染效果的表面图。
>> [x,y]=meshgrid([-2:.2:2]); >> z=x.*exp(-x.^2-y.^2); >> mesh(x,y,z)
>> subplot(2,2,1), plot3(x,y,z) >> title('plot3 (x,y,z)')
>> subplot(2,2,2), mesh(x,y,z) >> title('mesh (x,y,z)')
>> subplot(2,2,3), surf(x,y,z) >> title('surf (x,y,z)')
>> subplot(2,2,4), surf(x,y,z), shading interp
>> title('surf (x,y,z), shading interp')
5.8 绘制peaks 函数的表面图,用colormap 函数改变预置的色图,观察色彩的分布情况。 >> surf(peaks(30));
>> colormap(hot)
>> colormap(cool)
>> colormap(lines)
5.9 用sphere 函数产生球表面坐标,绘制不通明网线图、透明网线图、表面图和带剪孔的表面图。
>> [x,y,z]=sphere(30); >> mesh(x,y,z)
>> mesh(x,y,z),hidden off
>> z(18:30,1:5)=NaN*ones(13,5);
>> surf(x,y,z)
5.10 将5.9题中的带剪孔的球形表面图的坐标改变为正方形,以使球面看起来是圆的而不是椭圆的,然后关闭坐标轴的显示。
axis square
axis off
6.3 如何设置和获取指定句柄对象的属性值?一图形窗口对象的句柄为h,先查询该窗口对象可以设置的各种属性,再将窗口的灰色背景设置为白色背景。
(1)利用set(句柄,‘属性名称’,属性值)语句可以设置指定对象的属性,get(句柄,‘属性名称’)语句可以获得指定对象的属性。
(2)>> set(h)
Alphamap
BackingStore: [ {on} | off ]
CloseRequestFcn: string -or- function handle -or- cell array
Color
Colormap
CurrentAxes
CurrentCharacter
CurrentObject
……
从列出的属性内容可以看到,设置背景颜色的属性名为Color,因此
>> set(h,’color’,’w’)
即可将图形窗口的背景色改为白色。
6.4 已知三维图形视角的缺省值是方位角为-3
7.5°,仰角为30°,将观察点顺时针旋转20 角的命令是什么?
>> view(-57.5,30)
6.5画一双峰曲面(peaks)图,加灯光light,改变光源的位置观察图形的变化。
>> surf(peaks)
>> shading interp
>> lighting phong
>> light('Position',[-3 -2 1]);
>> light('Position',[-1 0 1]);
6.6 在双峰曲面上改变不同的光照模式,观察效果。
>> surf(peaks)
>> shading interp
>> light('Position',[-3 -2 1]);
>> lighting flat
>> lighting gouraud
>> lighting phong
>> lighting none
6.7 用subplot语句在一个图形窗口上开多个大小不等的子窗口进行绘图并添加注释,见图。
subplot('position',[0.1,0.15,0.3,0.65])
hist(randn(1,1000),20);
xlabel('直方图')
subplot('position',[0.45,0.52,0.25,0.28])
[xp,yp,zp]=peaks;
contour(xp,yp,zp,15,'k')
hold on
pcolor(xp,yp,zp)
shading interp
hold off
axis off
text(-1.2,-4,'伪彩色图')
subplot('position',[0.72,0.5,0.25,0.3])
sphere(25);
axis
equal,axis([-0.75,0.75,-0.75,0.75,-0.75,0.75]) light('Position',[1 3 2]);
light('Position',[-3 -1 3]);
material shiny
axis off
text(-0.8,-0.7,-1,'三维图')
subplot('position',[0.45,0.15,0.5,0.25])
t=0:pi/15:pi;
y=sin(4*t).*sin(t)/2;
plot(t,y,'-bs','LineWidth',2,... %设置线型
'MarkerEdgeColor','k',... %设置标记点边缘颜色
'MarkerFaceColor','y',... %设置标记点填充颜色
'MarkerSize',5)
axis([0,3.14,-0.5,0.5])
xlabel('带标记点的线图')
subplot('position',[0.1,0.9,0.8,0.1])
text(0.25,0.2,'多窗口绘图示例',...
'fontsize',25,'fontname','隶书','color','b') axis off
6.8 用[c,hc]=contour(peaks(30))语句绘制双峰曲面的等高线图,通过控制图形句柄的方法将第四条等高线加粗为2磅,将第六条等高线表示为虚线,在第十条等高线上加星号标记
[c,hc]=contour(peaks(30));
6.9 做一个花瓶,如图示。(提示:做一个旋转体表面,调入一幅图像对该表面进行彩绘,即用图像的色图索引作为表面体的色图索引)
>> t=(0:20)/20;
>> r=sin(2*pi*t)+2;
>> [x,y,z]=cylinder(r,40); % 产生旋转体表面的三维数据
>> cx=imread('flowers.tif'); % 读取名为flowers.tif的图象文件
>> [c,map]=rgb2ind(cx,256); % 真彩色图转换为索引图(若读入的是索引图,不需转换)
>> c1=double(c)+1; % 把unit8编址图象数据变换为双精度格式
>>
surface(x,y,z,'Cdata',flipud(c1),'FaceColor','tex turemap',...
'EdgeColor','none','CDataMapping','direct','A mbient',...
0.6,'diffuse',0.8,'speculars',0.9) %通过属性设置,进行彩绘。
>> colormap(map) % 使用图象的色图>> view(-50,10)
>> axis off
第7章 GUI程序设计
7.1 GUI开发环境中提供了哪些方便的工具?各有什么用途?
在GUI开发环境中提供了下列五个方便的工具:
(1)布局编辑器(Layout Editor)—在图形窗口中创建及布置图形对象
(2)几何排列工具(Alignment Tool)—调整各对象之间的相互几何关系和位置
(3)属性编辑器(Property Inspector)—查询并设置对象的属性值
(4)对象浏览器(Object Browser)—获得当前MATLAB窗口中图形对象句柄的分级排列
(5)菜单编辑器(Menu Editor)—建立和编辑主菜单和图形对象的鼠标右键菜单
7.2 做一个带按钮的界面,当按动按钮时,在计算机声卡中播放一段音乐。(提示,找一个.wav文件,简单起见可以在windows目录下找一个文件,将其放在当前工作目录下或搜索路径上,当按动“开始”按钮时调入该文件并播放,发声功能由sound函数完成,具体用法请查阅帮助信息)
提示:
(1)先建立一个静态文本对象作为界面的标题“简单声音播放器”
(2)建立一个按钮对象用于启动播放器,callback 函数中的内容为
[y,f,b]=wavread('loff'); % 读入声音文件loff.wav
sound(y,f,b) % 由声卡播放声音
(3)再建立一个用于关闭界面的按钮对象,callback 函数中的内容为
close(gcbf)
7.3 做一个滑条(滚动条)界面,图形窗口标题设置为GUI Demo: Slider,并关闭图形窗口的菜单条。功能:通过移动中间的滑块选择不同的取值并显示在数字框中,如果在数字框中输入指定范围内的数字,滑块将移动到相应的位置,见下图。
提示:
(1)在figure的属性浏览器中设置Name为GUI Demo: Slider
(2)先建立一个滑条对象,在属性浏览器中设置Max 为50,Min为-50;
(3)在滑条的两端各放置一个静态文本用于显示最大值和最小值;
(4)滑条对象的callback函数中的内容为:
val=get(handles.slider1,'value');
set(handles.edit1,'string',num2str(val));
(5)在滑条上方放置一个文本框,用于显示滑块的位置所指示的数值,也可以在文本框中直接输入数值,callback函数中的内容为:
str=get(handles.edit1,'string');
set(handles.slider1,'value',str2num(str));
7.4 用单选框做一个如图所示的界面,通过选择不同的单选框来决定使用不同的色彩图。
(1)建立坐标轴对象,用于显示图形;
(2)建立建立五个单选框,用于选择不同的色图;
(3)callback函数的内容为:
function varargout = radiobutton1_Callback(h, eventdata, handles, varargin)
set(handles.radiobutton1,'value',1)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',0)
set(handles.radiobutton5,'value',0)
colormap(jet)
%
--------------------------------------------------------------------
function varargout = radiobutton2_Callback(h, eventdata, handles, varargin)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton2,'value',1)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',0)
set(handles.radiobutton5,'value',0)
colormap(hsv)
%
--------------------------------------------------------------------
function varargout = radiobutton3_Callback(h, eventdata, handles, varargin)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton3,'value',1)
set(handles.radiobutton4,'value',0)
set(handles.radiobutton5,'value',0)
colormap(hot)
%
--------------------------------------------------------------------
function varargout = radiobutton4_Callback(h, eventdata, handles, varargin)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',1)
set(handles.radiobutton5,'value',0)
colormap(pink)
%
--------------------------------------------------------------------
function varargout = radiobutton5_Callback(h, eventdata, handles, varargin)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',0)
set(handles.radiobutton5,'value',1)
colormap(copper)
7.5 制作一个曲面光照效果的演示界面,如图所示,三个弹出式菜单分别用于选择曲面形式、色彩图、光照模式和反射模式,三个滚动条用于确定光源的位置,一个按钮用于退出演示。
提示:
(1)建立一个静态文本,用于显示界面的标题:光照效果演示;
(2)建立坐标轴对象,用于显示图形;
(3)建立四个下拉菜单,分别用于选择绘图表面的形状、色图、光照模式和反射模式,每个下拉菜单的上方都有一个静态文本用于说明菜单的作用;
(4)在一个frame上建立三个滑条用于确定光源的位置,并在frame上方加一说明;
(5)建立一个按钮用于退出演示;
(6)callback函数的内容为:
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)
delete(handles.figure1)
%
--------------------------------------------------------------------
function varargout = popupmenu1_Callback(h, eventdata, handles, varargin)
val=get(h,'value');
switch val
case 1
surf(peaks);
case 2
sphere(30);
case 3
membrane
case 4
[x,y]=meshgrid(-4:.1:4);
r=sqrt(x.^2+y.^2)+eps;
z=sinc(r);
surf(x,y,z)
case 5
[x,y]=meshgrid([-1.5:.3:1.5],[-1:0.2:1]);
z=sqrt(4-x.^2/9-y.^2/4);
surf(x,y,z);
case 6
t=0:pi/12:3*pi;
r=abs(exp(-t/4).*sin(t));
[x,y,z]=cylinder(r,30);
surf(x,y,z);
end
shading interp
light('Position',[-3 -2 1]);
axis off
%
--------------------------------------------------------------------
function varargout = radiobutton1_Callback(h, eventdata, handles, varargin)
set(h,'value',1)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',0)
lighting flat
% --------------------------------------------------------------------
function varargout = radiobutton2_Callback(h, eventdata, handles, varargin)
set(h,'value',1)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton4,'value',0)
lighting gouraud
%
--------------------------------------------------------------------
function varargout = radiobutton3_Callback(h, eventdata, handles, varargin)
set(h,'value',1)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton2,'value',0)
set(handles.radiobutton4,'value',0)
lighting phong
%
--------------------------------------------------------------------
function varargout = radiobutton4_Callback(h, eventdata, handles, varargin)
set(h,'value',1)
set(handles.radiobutton1,'value',0)
set(handles.radiobutton3,'value',0)
set(handles.radiobutton3,'value',0)
lighting none
%
--------------------------------------------------------------------
function varargout = popupmenu2_Callback(h, eventdata, handles, varargin)
val=get(h,'value');
switch val
case 1
colormap(jet)
case 2
colormap(hot)
case 3
colormap(cool)
case 4
colormap(copper)
case 5