一、矩陣操作練習
1、(1)
>> a=[1,2,6,4;2,5,1,3;7,1,1,-1;1,0,-2,-6];
>> a(3,4)=[6]
(2)
>> a(2,:)=[3,-2,1,9]
(3)
>> a(:,4)=[3,-2,1,9]
(4)
>> a=[1,2,6,4;2,5,1,3;7,1,1,-1;1,0,-2,-6];
>> b=a(1,:)*(-2)+a(3,:);
>> a(3,:)=b
2、(1)
>> rand(3,4)
ans =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
(2)
>> eye(4,3)
ans =
1 0 0
0 1 0
0 0 1
0 0 0
(3)
>> eye(2,2)
ans =
1 0
0 1
(4)
>> zeros(2,3)
ans =
0 0 0
0 0 0
(5)
>> ones(5,3)
ans =
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
3、
>> c1=ones(4,4);
>> c2=eye(4,6);
>> c3=zeros(4,4);
>> c4=rand(4,6);
>> c=[c1,c2;c3,c4]
c =
1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0 0 0
1.0000 1.0000 1.0000 1.0000 0 1.0000 0 0 0 0
1.0000 1.0000 1.0000 1.0000 0 0 1.0000 0 0 0
1.0000 1.0000 1.0000 1.0000 0 0 0 1.0000 0 0
0 0 0 0 0.9572 0.4218 0.6557 0.6787 0.6555 0.2769
0 0 0 0 0.4854 0.9157 0.0357 0.7577 0.1712 0.0462
0 0 0 0 0.8003 0.7922 0.8491 0.7431 0.7060 0.0971
0 0 0 0 0.1419 0.9595 0.9340 0.3922 0.0318 0.8235
4、
>> a=[2,-1,3;3,1,-6;4,-2,15];
>> det(a)
ans =
45
>> inv(a)
ans =
0.0667 0.2000 0.0667
-1.5333 0.4000 0.4667
-0.2222 0 0.1111
二、程序設計練習
1、
function sum=exam1(n)
sum=0;
for i=1:n
sum=sum+i;
end
>> sum=exam1(10)
sum =
55
三、圖形操作練習
1、
>> x=0:pi/50:2*pi;
>> y1=sin(x);
>> y2=cos(x);
>> plot(x,y,'k:',x,y2,'b-')
2、
>> subplot(2,2,1);
>> x=-2:0.01:2;
>> x=0:pi/50:2*pi;
>> y=sin(x);
>> plot(x,y,'k');
>> subplot(2,2,2);
>> x=0:pi/50:2*pi;
>> y=cos(x);
>> plot(x,y,'k');
3、(1)
>> ezplot('16*e^(-x^2)',[-3,3])
(2)
>> ezplot('y^2=x^2*(4-x^2)',[-2,2])
(3)
>> ezplot('sin(3*x)',[0,2*pi])
(4)
>> t=0:pi/30:10*pi;
>> x=sin(t);
>> y=cos(t);
>> z=t;
>> plot3(x,y,z);
4、(1)
>> x=-10:1:10;
>> y=-10:1:10;
>> [x,y]=meshgrid(x,y);
>> v=sqrt(x.^2+y.^2)+eps;
>> z=sin(v)./v;
>> mesh(x,y,z)
(2)
x=-2:1:2;
y=-2:1:2;
[x,y]=meshgrid(x,y);
h=2*x+eps;
d=exp(x.^2+y.^2);
z=h./d;
subplot(2,2,1);
mesh(x,y,z);