ezplot('(cos(x))^(1/2)',[-pi/2 pi/2]); ylabel('y'); subplot(2,2,3); x=-2:0.5:2; y=-4:1:4;
ezsurfc('x^2/2^2+y^2/4^2')
(cos(x))1/210.5y0-1x2/22+y2/420x1105050y-5-5x05
基本编程题(每小题10分,共30分)
1. Write a program to compute the following expressions
;
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
and
in the range of , 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 in the range of and , 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);
写出程序的执行结果或写出给定要求的指令(总共35分) 1. 写出执行以下代码后C,D,E的值 (6分) A=[1,2,3;4:6;7:9]; C=[A;[10,11,12]], D=C(1:3,[2 3]) E=C(2,[1 2]) C =
1 2 3 4 5 6 7 8 9 10 11 12
D =
2 3 5 6 8 9 E =
4 5
2. 写出执行以下代码后,MATLAB命令窗口上显示的x矩阵的值 (5分) x=[0,1,0,2,0,3,0,4]; for k=1:8 if x(k)==0 x(k)=k; else
x(k)=2*k+1; end end disp(x);
1 5 3 9 5 13 7 17
3. 创建符号函数并求解,要求写出步骤和运行结果(7分) (1)创建符号函数f=ax2+bx+c (2)求f=0的解 syms a x b c; f=a*x^2+b*x+c; solve(f) ans =
1/2/a*(-b+(b^2-4*a*c)^(1/2)) 1/2/a*(-b-(b^2-4*a*c)^(1/2))
4. 求解以下线性方程组,要求写出程序代码和运行结果(5分) 2x1-3x2+x3+2x4=8
x1+3x2+ x4=6 x1-x2+x3+8x4=1 7x1+x2-2x3+2x4=5
解:程序代码:a=[2 -3 1 2;1 3 0 1;1 -1 1 8;7 1 -2 2]; b=[8 6 1 5]'; ra=rank(a); rb=rank([a b]); det(a); xx=a\\b
5.绘制函数曲线,要求写出程序代码(12分) (1)在区间[0:2π]均匀的取50个点,构成向量π
(2)在同一窗口绘制曲线y1=sin(2*t-0.3); y2=3cos(t+0.5);要求y1曲线为红色点划线,标记点为圆圈;y2为蓝色虚线,标记点为星号。 解:代码如下:t=linspace(0,2*pi,50); plot(t,sin(2*t-0.3),'r-.o'); hold on;
plot(t,3*cos(t+0.5),'b--*')
6. 打印出所有的水仙花数。所谓“水仙花数”,是指一个三位数,其各位数字立方之和等于该数本身。 解:程序如下: for k=100:999 a=fix(k/100);
b=rem(fix(k/100),10); c=rem(k,10);
if a.^3+b.^3+c.^3==k fprintf('%u,\\t\\t',k); end end
答案如下: 397, 713,
10. 由指令A=rand(3,5)生成二维数组A,试求该数组中所有大于0.5的元素的位置,分别求出它们的“全下标”和“单下标”。