You are on page 1of 9

Bi tp chng 1.

Bi 1

Gii

Vit chng trnh trong M-file :


function [x y]= hebac1(a,b,m,c,d,n)
a=input('nhap he so a=');
b=input('nhap he so b=');
m=input('nhap he so m=');
c=input('nhap he so c= ');
d=input('nhap he so d=');
n=input('nhap he so n= ');
D=a*d-c*b;
Dx=m*d-n*b;
Dy=a*n-c*m;
if(d~=0)
disp('he phuong trinh co nghiem la');
x=Dx/D;
y=Dy/D;
else
if(dx~=0&&dy~=0)
disp('phuong trinh vo nghiem');
x=inf;
y=inf;
end
if(dx==0&&dy==0)
disp('phuong trinh vo so nghiem');
x=inf;
y=inf;
end
end

% lu vo file hebac1.m

Sau thc thi bi ton trn trong command window vi


gi tr a,b,m,c,d,n ty ta s c c kt kt qu .
Bi 2
Gii
a) v cc hm c bn sin(x), cos(x), tan(x).. dng lnh
fplot.
Thc hin bi ton bn trong ca s command window :
fplot([sin(x), cos(x), tan(x)],2*pi*[-1 1]) enter
b)v hm y= sin(x)/x , y=x, y=sin(x) trn cng mt th
Vit trong M-file dng function :
function Y = myfun(x)
Y(:,1)=sin(x(:))/(x(:));
Y(:,2)=x(:);
Y(:,3)=sin(x(:));

%luu vo file myfun.m

Thc hin bi ton bn trong ca s command window:


>> fplot('myfun1',2*pi*[-1 1]) n enter
Bi 4
Gii
Vit chng trnh trong M-file :
function [x1,x2]=GPTB2(a,b,c)
if(a==0)
if(b==0)
if(c==0)
disp('phuong trinh vo so nghiem');
x1=inf;
x2=inf;
else

else

disp('phuong trinh vo nghiem');


x1=nan;
x2=nan;
end

disp ( 'phuong trinh co nghiem la ');


x1=-c/b;
x2=x1;
end
else
delta=b*b-4*a*c;
disp('nghiem cua phuong trinh la')
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
end
end

% luu vo file GPTB2.m


Sau thc thi trn ca s command window vi h s
a,b,c ty gii phng trnh .
Bi 5 :
Gii
Vit trong M-file dng function :
function [q,r]=divide(a,b)
r=mod(a,b);
q=(a-r)/b;
end

% luu vo file divede.m


Sau thc thi trn ca s command window:

Bi tp chng 2
Bi 1
Gii
Trn ca s command Window ta vit nh sau :
>>syms x y z
>>Y= x^2+x+y+z+1 enter
Y= x^2+x+y+z+1

Bi 2

Gii

To hm symbolic :Y= 1/( 5+ 4* cos(x) ) bng nhiu cch khc nhau :


Cch 1 :
Thc hin trn ca s command Window :
>> syms x
>> Y= 1/ (5+4*cos(x)) enter
Y= 1/ (5+4*cos(x))
Cch 2 : to hm symbolic t M-file
Function Y=tao_ham(x)
Y= 1/ (5+4*cos(x)) ;
% lu vo ng dn C:\matlabR12\toolbox\sym\tao_ham
Sau khi lu xong thc hin trn ca s command Window :
>> syms x
>> Y=tao_ham(x) nenter
Y= 1/ (5+4*cos(x))
Cch 3 : to hm trc tip trn ca s command Window :
>> Y=sym(1/ (5+4*cos(x))) n enter
Y= 1/ (5+4*cos(x))

Tnh o hm v tch phn :


Thc hin trn ca s command Window :
>> syms x
>> Y= 1/ (5+4*cos(x)) enter
Y= 1/ (5+4*cos(x))
>>diff (Y) n enter
ans =
4/(5+4*cos(x))^2*sin(x)
>> int(Y) n enter
ans =
2/3*atan(1/3*tan(1/2*x))

Bi 3 . v th hm Y= 1/ (5+4*cos(x)) theo cch thng thng


v s dng symbolic

Gii
Theo cch thng thng :
Thc hin trn ca s command Window :
fplot ( '[1/ (5+4*cos(x))]', 2*pi*[-1 1]) n enter
th c dng l

S dng symbolic :
Thc hin trn ca s command Window :
>> syms x Y; n enter
>> Y= 1/ (5+4*cos(x)) ; n enter
>> ezplot(Y,[-2*pi 2*pi]) n enter
D th ging cach 1

Bi tp chng 3 :

Bi 1 :
Gii
Thc hin trn ca s command Window :
>> a=[1 2 3 ;4 5 6 ]; n enter
>> b=[5 6 7 ; 8 9 10]; n enter
>> Addad = a+b n enter
Addad =
6 8 10
12 14 16
>> Subsab = a-b n enter
Subsab =
-4 -4 -4
-4 -4 -4
>> Multab=a.*b

n enter

Multab =
5 12 21
32 45 60
>> Multab=b.*a
Multab =
5 12 21
32 45 60

n enter

>> Divab=a/b

n enter

Divab =
2.3333 -1.3333
1.3333 -0.3333
>> Divab= b/a

n enter

Divab =
-0.3333 1.3333
-1.3333 2.3333
Bi 3

Gii

1. gii h cho
Thc hin trn ca s command Window :
>> A=[1/2 1/3 1/4 ; 1/3 1/4 1/5 ;1/4 1/5 1/6 ];
>> B=[ 0.95;0.67;0.52];
>> X=invhilb(3)*B
X=
0.029999999999999
0.840000000000018
1.500000000000000
2. gii h khi thay i B(3)= 0.53
Thc hin trn ca s command Window :
>> A=[1/2 1/3 1/4 ; 1/3 1/4 1/5 ;1/4 1/5 1/6 ];
>> B=[ 0.95;0.67;0.53];
>> X=invhilb(3)*B

X=
0.329999999999998
-0.959999999999980
3.299999999999997
So snh hai kt qu :
thay i B(3)= 0.53 so vi gi tr c l 0.52 tc l tng 1% tng
ng vi gi tr X(1) tng 30% , X(2) gim 180% v X(3) tng 180
%

You might also like