Professional Documents
Culture Documents
ans =
e. 2:4:16
ans =
10
14
b 2 4ac 1
x1 =1 , x2 =2
x 2 x 2 =0
b 2 4ac 7
x3 3x 1 0
>>Roots([1 0 -3 1])
ans =
-1.8794
1.5321
0.3473
3 x 4 3x 2 x 1 0
>>Roots([3 0 -3 1 -1])
ans =
-1.2229
1.0000
0.1114 + 0.5101i
0.1114 - 0.5101i
1.7 Cho a =-36, b =15. Tnh bng tay cc biu thc sau, ri th li bng Matlab
a. mod(a,b)=9 chia ly d // MOD(x,y) is x - n.*y where n = floor(x./y)
b. rem(a,b)=-6 chia ly d // REM(x,y) is x - n.*y where n = fix(x./y)
c. gcd(a,b)= 3 c s chung ln nht
d. lcm(a,b)= !! bi s chung nh nht
>> lcm(a,b)
??? Error using ==> lcm
Input arguments must contain positive integers. (a b phi cng l s dng )
2. Vector
2.1 To mt vector cha cc s nguyn t 31 n 75. >>A=31:75
To mt vector c 10 phn t c gi tr nguyn c ly ngu nhin trong khong [0, 100]
>>randint(1,10,[0 100]) // >>A=round(100*rand(1,10))
To mt vector c 10 phn t c gi tr nguyn c ly ngu nhin trong khong [-20 ,10]
>>randint(1,10,[-20 10])
>> sqrt(x)
ans = 2.2361 2.2361 2.0000 2.4495
d. Bnh phng tt c cc phn t.
>> x.*x
ans = 4 25 1 36
x =((-1).^((1:n)+1))./((2.*(1:n) - 1))
n=100
>> sum(x)
ans = 1529/1953
3. Ma trn
3.1 Cho x = [1 4 8], y = [2 1 5] v A = [3 1 6 ; 5 2 7]
gii thch; ri th li bng Matlab :
a. x + y = 3
5
13
b. x + A = ??? Error using ==> +
g. A 3
0
-2
-1
b. A(:,[1 4])
ans =
2
8
5
c. A([2 3], [3 1])
ans =
5
2
8
d. reshape(A, 2, 6)
ans =
e. A(:) =
2
3
8
7
1
1
9
5
2
7
6
5
f. flipud(A)
ans =
8
g. fliplr(A)
ans =
h. [A; A(end,:)]
ans =
2
i. A(1:3,:)
ans =
k. sum(A) ans =
l. sum(A) ans =
m. sum(A, 2)
13
25
9
15
16
16
18
ans =
25
15
16
n. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
ans =
2
25
15
16
13
16
18
56
4. V th
4.1 V th hm s x, x3, ex v e^(x2) vi 0 < x < 4
>> plot(x,x)
>> plot(x,x.^3)
>> plot(x,exp(x.^2))
=
=
=
0
0
0
1
1
0
0
0
1
1
1
0
1
1
0
0
0
1
0
0
0
1
1
1
1
1
0
0
0
0
1
1
1
1
1
1
1
1
1
1
0
0
0
0
1
1
1
1
1
0
0
1
0
0
1
1
1
1
0
0
0
0
0
1
1
1
1
1
0
0
a. x > y
b. y < x
c. x == y
d. x <= y
=
e. y >= x
=
f. x | y
=
g. x & y
=
h. x & (-y) =
i. (x > y) | (y < x)
j. (x > y) & (y < x)
8
5
9
6
9
7
10
10
0
6. Cu lnh if
D on kt qu v th li bng Matlab:
6.1
if n > 1
m = n+1
else
m=n1
end
6.2 if z < 5
w = 2*z
elseif z < 10
w=9-z
elseif z < 100
w = sqrt(z)
a. n = 7 m = ? = 8
b. n = 0 m = ? = -1
c. n = -10 m = ? = -11
a. z = 1 w = ? =2
b. z = 9 w = ? = 0
c. z = 60 w = ? = 1921/248
d. z = 200 w = ? = 200
else
w=z
end
6.3 if T < 30
h = 2*T + 1
elseif T < 10
h=T2
else
h=0
end
a. T = 50 h = 0
b. T = 15 h = 31
c. T = 0 h = 1
a. x = -1
b. x = 5
c. x = 30
d. x = 100
y = 500
y = 20
y = 300
y = 500
nu x < 0
nu x = 0
nu x > 0
if x>0
f=1
elseif x=0
f=0
else
f=-1
end
So snh vi hm sign ca MATLAB.
6.7 t(y) = 200
= 200 + 0.1 (y - 10,000)
= 1,200 + 0.15 (y - 20,000)
= 5,700 + 0.25 (y - 50,000)
if y<=10000
t=200
elseif (y>10000)&(y<=20000)
t=200 + 0.1*(y - 10000)
elseif (y>20000)&(y<=50000)
t=1200 + 0.15*(y - 20000)
elseif y>50000
t=5700 + 0.25*(y - 50000)
end
Th cc trng hp: a. y = 5,000
b. y = 17,000
c. y = 25,000
d. y = 75,000
t=200
t=900
t=1950
t=11950
7. Vng lp
7.1 Cho vector x = [1 8 3 9 0 1], vit cc dng lnh
a. Tnh tng tt c cc phn t. (So snh kt qu vi lnh sum.)
s=0;
n=size(x,1)*size(x,2);
for i=1:n
s=s+x(i);
end
s
Kt qu s=22
Ging hm sum(x)=22
b. Vit hm tnh tng dy con t 1 n j. (So snh kt qu vi lnh cumsum.)
n=size(x,2)
if n<=1
x
else
for j=2:n
x(:,j)=x(:,j)+x(:,j-1);
end
KQ=x
end
x=
1
11
>> cumsum(x,2)
ans =
1
10
15
16
17
Run scripts :
KQ =
10
15
16
17
Vy kt qu ging nhau
7.2 To ma trn M x N cc s ngu nhin (dng lnh rand). Chuyn cc gi tr nh hn 0.2 thnh 0,
cc gi tr ln hn hay bng 0.2 thnh 1.
M=input('nhap M: ')
N=input('nhap N: ')
X=rand(M,N)
X(X<0.2)=0;
X(X>=0.2)=1
////////
M=input('nhap M: ')
N=input('nhap N: ')
for i=1:M
for j=1:N
X(i,j)=rand
end
end
X(X<0.2)=0;
X(X>=0.2)=1;
X
7.3 Cho x = [4 1 6] , y = [6 2 7] tnh cc mng/vector/ma trn sau
a. aij = xiyj
x = [4 1 6] , y = [6 2 7]
for i=1:3
for j=1:3
a(i,j)=x(i)*y(j);
end
end
a
b. bij = xi/yj
x = [4 1 6] , y = [6 2 7]
for i=1:3
for j=1:3
b(i,j)=x(i)/y(j);
end
end
b
else
y= x((x(1:n-1)>=0.1)&(x(1:n-1)<=0.4));
if isempty(y)==1
disp('Ko co so nao thuoc khoang [0.1 0.4] truoc gia tri dau tien lon hon trung binh')
else
strcat('co :',num2str(length(y)),' so thuoc khoang [0.1 0.4] truoc gia tri dau tien lon hon trung binh')
end
end
7.5 Vit mt script yu cu nhp mt nhit Fahrenheit ri chuyn sang Celcius tng ng. Script
vn chy n khi khng nhp nhit na th thi. [S dng hm isempty.]
F=input('Nhap vao nhiet do Fahrenheit : ')
while isempty(F)==0
C=(F-32)*5/9
format rat
F=input('Nhap vao nhiet do Fahrenheit : ')
end
7.6 Vit mt script yu cu: Nhp lin tc cc s, dng li khi nhp 0, v xut ra trung bnh cng ca
cc s dng v trung bnh cng ca cc s m ca dy s ni trn
s=[]
x=input('Nhap vao mot so: ')
while x~=0
s=[s x];
x=input('Nhap vao mot so: ')
end
daysoduong=s(s>0)
Tbd=mean(daysoduong)
daysoam=s(s<0)
Tba=mean(daysoam)
7.7 Cn c tng s 50 t 3 loi bc 1, 2 v 5 ng. Vit on cript tm tt c cc phng n c th.
s=[]
for i=0:50
for k=0:25
for h=0:10
if (i+k*2+h*5)==50
s=[s;i k h]
end
end
end
end
ax by c
y phi nhp a,b,c,d,e,f
dx ey f
b=input('nhap b: ')
c=input('nhap c: ')
d=input('nhap d: ')
e=input('nhap e: ')
f=input('nhap f: ')
ngiem=[a b;d e]\[c;f]
7.9 Vit script cho php nhp vo a, b, c. Sau gii phng trnh bc hai ax 2 bx c 0
a=input('nhap a: ')
b=input('nhap b: ')
c=input('nhap c: ')
Delta=b^2-4*a*c;
x1=(-b-sqrt(Delta))/(2*a)
x2=(-b+sqrt(Delta))/(2*a)
8. Lp trnh
8.1 Lp chng trnh tnh n! , Cnk , x n (Khng dng hm ca Matlab)
n=input('Nhap he so giai thua can tinh vao day:');
while n<0|n~=round(n)
disp('Phai nhap mot so nguyen duong')
n=input('Nhap lai he so giai thua can tinh :')
end
if n==0
sprintf('Ket qua %d!=1',n)
else
KQ=1;
for i=1:n
KQ=KQ*i;
end
sprintf('Ket qua %d!=%d',n,KQ)
end
8.2 Vit hm tnh gi tri ln nht v nh nht ca ma trn (Khng s dng hm max va min)
Theo tng phn t
max=x(1);
for i=1:size(x,1)*size(x,2)
if x(i)>max
max=x(i);
end
end
max
Theo ct :
MAX=[];
for j=1:size(x,2)
max=x(1,j);
for i=1:size(x,1)
if x(i,j)>max
max=x(i,j);
end
end
MAX=[MAX max];
end
MAX
Theo hng :
MAX=[];
for i=1:size(x,1)
max=x(i,1);
for j=1:size(x,2)
if x(i,j)>max
max=x(i,j);
end
end
MAX=[MAX ;max];
end
MAX
8.3 Sp xp li 1 vect theo chiu tng dn ca cc phn t
kq=[];
while isempty(x)==0
min=x(1)
n=1
for i=1:size(x,2)
if x(i)<min
min=x(i);
n=i;
end
end
kq=[kq x(n)];
x(n)=[];
end
kq
2 8
1
2
2
16
n 1 (2 n 1) (2 n 1)
Gi tr ca n cn t n bao nhiu chnh xc l 1e-12 ? chnh xc l bao nhiu khi n = 100?
8.5 Dy s Fibonacci c mi quan h sau: Fn = Fn-1 + Fn-2, vi F0 = F1 = 1.
a. Tnh 10 s Fibonacci u tin.
n=input('Do lon:');
s=[];
if n==1|n==2
s=1;
else
s=[1 1]
for i=3:n
s= [s (s(i-1)+s(i-2))];
end
end
s
b. Trong 50 s Fibonacci u tin, tnh t l Fn / Fn-1. Nhn xt g cc gi t l ny so vi t l
vng ( (1 + sqrt(5))/2 )=1.6180.
1.0000
1.0000
2.0000
1.5000
1.6667
1.6000
1.6250
1.6154
1.6190
1.6176
1.6182
1.6180
1.6181
1.6180
1.6180
1.6180
1.6180
1.6180
end
8.8 Vit hm tnh tch dy con bt u t phn t u tin ca mt vector. Cho vector x, tch dy con
th j bng
pj = (x1)(x2) ... (xj) vi j chy t 1 n chiu di l ca vector x. Vit hm bng 2 cch:
a. S dng vng lp for.
x=[1 2 3 4 5 ;1 1 1 5 2;1 0 1 0 2]
n=size(x,2)
if n<=1
x
else
for j=2:n
x(:,j)=x(:,j).* x(:,j-1);
end
KQ=x
end
8.10 Vit hm sinh ngu nhin ma trn cc s nguyn:
function A = randint(a,b,M,N)
trong a v b l cc chn di v chn trn, cn M v N l kch thc ca ma trn (s dng, s ct).
function A = randint(a,b,M,N)
for i=1:M
for j=1:N
A(i,j)=a+(b-a)*rand;
end
end
a. Th li vi cc dng lnh nh sau:
x = randint(10,17,10,1);
hist(x,10:17)
Nhn xt biu m t.
x=
16.6509 11.6180 14.2479 13.4019
16.2391 15.3347 13.1953 10.1295
15.7499 13.1129
Biu miu t c khong bao nhiu x t cc ln cn gi tr 10,11,12,13,14,15,16,17
x = randint(7,-2,10,1);
hist(x,-2:7) (tuy ngc nhng khng sao)
xem li iu kin tham s cho ng.
x(3)=5
x(1:7)=3 1 5 7 9 2 6
x(1:end)= 3 1 5 7 9 2 6
x(1:end-1)= 3 1 5 7 9 2
x(6:-1:1) = 2 7 1
x([1 6 2 1 1])= 3 2 1 3 3
sum(x)= 33
e)
Ta c
2 4 1
A 6 7 2
3 5 9
>>B=mean(A)
B=
3.6667
5.3333
4.0000
>>C=[B;B;B]
C=
3.6667 5.3333 4.0000
3.6667 5.3333 4.0000
3.6667 5.3333 4.0000
>>E=A-C
E=
-1.6667 -1.3333 -3.0000
2.3333 1.6667 -2.0000
-0.6667 -0.3333 5.0000
>>F=E.*E
F=
2.7778 1.7778 9.0000
5.4444 2.7778 4.0000
0.4444 0.1111 25.0000
>> G=sum(F)
G=
>>e= L/sqrt(size(A,1))
=
1.2019
0.8819
2.5166
Hoc ch cn g
N = size(A,1), e = std(A)/sqrt(N)
e=
1.2019
0.8819
2.5166
5
2
4
8
number of columns.
f. [x ; y] > ng
ans =
1 4 8
2 1 5
g. A - 3 > ng
ans =
0 -2 3
2 -1 4
5. Cho ma trn A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], gii thch kt qu cc lnh sau:
a. reshape(A,2,6)
ans =
2 8 1 9
3 7 1 5
2
7
6
5
Nu
>> reshape(A,2,9) s li
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
b. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
>>[ [ A ; sum(A) ]
ans =
2 7 9 7
3 1 5 6
8 1 2 5
13 9 16 18
Nu c sai st s bo
??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of columns
>> [ sum(A,2) ; sum(A(:)) ]
ans =
25
15
16
56
>>X= [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
X=
2
3
8
13
7
1
1
9
9 7 25
5 6 15
2 5 16
16 18 56
Nu c sai st s bo
??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of rows
C=
7 7
1 6
1 5
c. chuyn A thnh ma trn 4 hng 3 ct
>> reshape(A,4,3)
ans =
2 1 2
3 1 7
8 9 6
7 5 5
d. Tm ma trn vi cc phn t gi tr nghch o ca mi phn t trong A
>> B=1./A
B=
0.0826 0.1575 0.0461
0.1976 0.3745 0.0695
-0.2994 -0.1266 0.1083
e. Tnh ma trn vi cc phn t cn bc 2 ca mi phn t trong A
>> C=sqrt(A(:,:))
C=
3.4799
2.5199
4.6583
2.2494
1.6340
3.7921
0 + 1.8276i 0 + 2.8107i
3.0381
8. D on kt qu ca cc cu lnh sau v kim tra li bng Matlab:
a. x = ones(1,10)
ones( hng , ct )
x=
b. y =zeros(5,1)
zeros(hng,ct)
y=
0
0
0
0
0
c. z = linspace(1,4,5)
z=
1.0000 1.7500 2.5000
3.2500
4.0000
d. t = logspace(1,3,4)
t=
10
100
1000
e. u = rand(2,4)
ans =
0.3784
0.8600
9. Cho ma trn A=[12.11 -7.9 9.23; 5.06 6.35 21.7;-3.34 2.67 14.38]
vit lnh Matlab :
a. Tm ln ca gi tr tuyt i tt c cc phn t ca A
>>A=abs(A);
>> log(A)
ans =
2.4940
1.6214
2.0669
1.8485
2.2225
3.0773
0.8537
0.5936
0.4966
0.8998
0.8216
0.6449
0.8976
0.8028
0.4265
0.9652
1.3365
1.1578
ans =
-3.3400 -7.9000 9.2300
g. Sp xp nhng phn t trong mi ct ca A theo th t tng dn.
>> sort(A)
ans =
-3.3400 -7.9000 9.2300
5.0600 2.6700 14.3800
12.1100 6.3500 21.7000
h. Sp xp nhng phn t trong mi ct ca A theo th t gim dn.
>> B=sort(A)
B=
-3.3400 -7.9000 9.2300
5.0600 2.6700 14.3800
12.1100 6.3500 21.7000
>> A=B(end:-1:1,:)
A=
12.1100 6.3500 21.7000
5.0600 2.6700 14.3800
-3.3400 -7.9000 9.2300
ANSWERS
2. A = [ 2 4 1 ; 6 7 2 ; 3 5 9]
x1 = A(1,:)
y = A(end-1:end,:)
c = sum(A)
d = sum(A,2) or d = sum(A')'
N = size(A,1), e = std(A)/sqrt(N)
5. A
B
C
c
d
e
=
=
=
=
=
=
[2 7 9 7 ; 3 1 5 6 ; 8 1 2 5]
A(:,2:2:end)
A(1:2:end,:)
reshape(A,4,3) or c = A' (they are different but are both 4x3)
1./A , rats(d)
sqrt(A)
6. randn('seed',123456789)
F = randn(5,10);
N = size(F,1)
avg = mean(F)
s = std(F)
tscore = (avg - 0)./(s/sqrt(N))
None were different at 90% LOC (all < 2.132).