>> m1
a=4
m=6
0.3806
2. n-natural son berilgan bo’lsin. Ildizni hisоblash dasturini tuzing.
function m2()
n=input('n=');
s=0;
for i=1:n
s=sqrt(2+s);
end
disp(s);
end
Natija:
>> m2
n=15
2.0000
3. Bеrilgan n-natural sоn uchun quyidagi yig’indini hisoblash dasturini tuzing.
function m3()
n=input('n=');
s=0;
surat=1;
maxraj=1;
for i=1:n
surat= surat*sin(2*i-1);
maxraj=maxraj*cos(2*i);
s=surat/maxraj;
end;
disp(s);
end
Natija:
>> m3
n=15
-1.8662
4. Bеrilgan n-natural sоn uchun quyidagi yig’indini hisoblash dasturini tuzing.
function m4()
n=input('n=');
s=0;
surat=1;
maxraj=1;
for i=1:n
maxraj=maxraj+cos(2*i);
s=s+surat/maxraj;
end;
disp(s)
end
Natija:
>> m4
n=100
-293.5395
5. Bеrilgan n-natural sоn uchun quyidagi ifodani hisoblash dasturini tuzing.
function m5 ()
n=input('n=');
x=0;
s=0;
for i=1:n
x=x+cos(i);
s=x+(1+cos(i));
end
disp(s);
end
Natija:
>> misol5
n=125
1.1177
6. Quyidagi ifodani hisoblash dasturini tuzing. x-haqiqiy son.
function m6 ()
x=input('x=');
s=1;
for i=1:6
s=s*(x-(2^i-1))/(x-2^i);
end
disp(s);
end
Natija:
>> m6
x=245
1.0274
7. Yig’indini hisоblash dasturini tuzing. n-natural sоn x-haqiqiy son.
function misol7 ()
x=input('x=');
n=input('n=');
s=0;
for i=1:n
x=cos(x^i);
s=s+x;
end
disp(s);
end
Natija:
>> misol7
x=452
n=12
9.3927
8. Ildizni hisоblash dasturini tuzing. n-natural sоn .
function m8()
n=input('n=');
s=1;
for i=1:n;
s=(sqrt(3*n+s))-3*n*n;
end
disp(s);
end
Natija:
>> m8
n=4
-47.4969 + 5.9817i
9. bo’lsa kеtma-kеtlik yig’indini hisоblash dasturini tuzing.
function m9()
x=input('x=');
a=input('a=');
b=input('b=');
c=input('c=');
n=input('n=');
for k=0:n-1;
y(k+1)=x*x-a*b*x+c^k;
end;
for k=1:n
disp(y(k));
end;
end
Natija:
>> m9
x=5
a=1
b=3
c=4
n=3
11
14
2
6-Mavzu: Matlab 2014 dasturda bir o’lchovli massivlar va ular ustida amallar bajarish.
Nazariy qism:
Murakkab hisoblarni bajarish uchun Besic yoki Paskal dasturlash tillarida maxsus dasturlarni tuzish talab qilingan bo’lar edi. Matlab – vektorlar matrissalar va massivlar ustida murakkab hisoblarni bajarish uchun mo’ljallangan maxsus tizimdir. Bundan u har qanday berilgan o’zgaruvchini uning konkret qiymatiga asoslanib vektor matrissa yoki massiv deb qabul qiladi. Masalan , x=1 berilganj bo’lsa demak x qiymati birga teng bo’lgan yagona elementli vektor. Agar uch elementli vektorni berish zarur bo’lsa uning elementlarining qiymatlarini probbellar bilan ajratilib, kvadrat kavs ichida yozib chiqish kerak. Matrissa va vektorlarning ayrim elementlari bilan bir qatorda ularning hamma elementlari (massivlar) ustida amallar bajarish mumkin buning uchun amal belgisining oldiga nuqta qo’yiladi. Masalan , * operatori vektorlari yoki matrissalarini ko’paytirishni anglatadi, Operator esa massivning hamma elementlarini elementlararo ko’paytirishni bildiradi. Matrissa skalyarga ko’paytiriladigan M *2 va M.*2 ifodalar teng kuchlidir. Vektorlarni ko’paytirish M=M1*M2 ifodasida ko’paytirish belgisidan oldin nuqta qo’yilmaganligi uchun tizim hato to’g’risidagi Inner matrix dimensions must agree (matrissalarning o’lchamlari o’zaro moslashishi kerak) degan axborotni berdi. Hato tuzatilgandan keyin to’g’ri natija olindi. Matrissalarni transponerlash, ya’ni ustunlarini satrlari bilan almashtirish uchun .’ Operatoridan foydalaniladi. Ustunlaridagi elementlarning yig’indisi sum operatori yordamida hisoblaniladi. Satrlaridagi elementlarining yig’indisini hisoblash uchun matrissa avval transponerlaniladi va transponerlangan matrissaning ustunlaridagi elementlarining yig’indisi aniqlaniladi. Matrissalarni Matlab 2014 dasturida ishlaganda ullarda for operatoridan foydalanib sikl bajariladi va e’lon qilinadi.
matrix(m, n, [[a11, a12, ...], [a21, a22, ...], ...]) returns an m×n matrix of the domain type Dom::Matrix().
matrix(m, n, [a11, a12, ..., a21, a22, ..., a.m.n]) returns an m×n matrix of the domain type Dom::Matrix().
matrix(m, 1, [a1, a2, ...]) returns an m×1 column vector of the domain type Dom::Matrix().
Amaliy qism:
1.n-natural son berilgan bo’lsin.1 dan n gacha bo’lgan sonlarni ekranga chiqaruvchi dastur tuzing.
function m1()
n=input('n=');
for i=1:n
a(i)=i;
disp(a(i));
end
end
Natija:
>> m1
n=5
1
2
3
4
5
2. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikning juft elementlarini va ularning soni hisoblang.
function m2()
n=input('n=');
k=0;
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
for i=1:n
if mod(a(i),2)==0
k=k+1;
disp(a(i));
end
end
fprintf('k=%.d',k);
end
Natija:
>> m2
n=5
a(1)=4
a(2)=9
a(3)=145
a(4)=268
a(5)=458
4
268
458
k=3>>
3. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikning toq elementlarini va ularning soni hisoblang.
function m3()
n=input('n=');
k=0;
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
for i=1:n
if mod(a(i),2)==1
k=k+1;
disp(a(i));
end
end
fprintf('k=%.d',k);
end
Natija:
>> m3
n=3
a(1)=579
a(2)=876
a(3)=463
579
463
k=2>>
4. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikning dastlab juft elementlarini keyin toq elementlarini ekranga chiqaruvchi dastur tuzing.
function m4()
n=input('n=');
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
for i=1:n
if mod(a(i),2)==0
disp(a(i));
end
end
for i=1:n
if mod(a(i),2)==1
disp(a(i));
end
end
end
Natija:
>> m4
n=5
a(1)=489
a(2)=562
a(3)=325
a(4)=789
a(5)=628
562
628
489
325
789
5. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikdagi barcha tub sonlarni ekranga chiqaring va ularning sonini hisoblang.
function m5()
n=input('n=');
k=0;
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
for i=1:n
for j=1:a(i)
m=mod(a(i),j);
if m==0
k=k+1;
end
end
if k==2
fprintf('a(%.d)=%.d',i,a(i));
disp('\n');
end
k=0;
end
end
Natija:
>> m5
n=3
a(1)=2
a(2)=31
a(3)=26
a(1)=2\n
a(2)=31\n
6. n-natural, k-haqiqiy son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlik elementlarini k soniga bo’lib chiqing.(k!=0).
function m6 ()
n=input('n=');
k=input('k=');
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
for i=1:n
s=a(i)/k;
disp(s);
end
end
Natija:
>> m6
n=5
k=3
a(1)=3
a(2)=6
a(3)=9
a(4)=21
a(5)=33
1
2
3
7
11
7. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikning eng katta elementini toping.
function m7()
n=input('n=');
max=0;
for i=1:n
fprintf('a(%.d)=',i);
x(i)=input('');
end
for i=1:n
if max
max=x(i);
end
end
disp(max);
end
Natija:
>> m7
n=3
a(1)=458
a(2)=786
a(3)=897
897
8. n-natural son va x{n} ketma-ketlik berilgan bo’lsin. Ketma – ketlikning eng kichik elementini toping.
function misol8()
n=input('n=');
for i=1:n
fprintf('a(%.d)=',i);
a(i)=input('');
end
min=a(1);
for i=2:n
if min>a(i)
min=a(i);
end
end
disp(min);
end
Natija:
>> misol8
n=5
a(1)=25
a(2)=43
a(3)=78
a(4)=14
a(5)=89
14
7-Mavzu: Matlab 2014 dasturda ikki o’lchovli massivlar va ular ustida amallar bajarish.
Nazariy qism:
Matlab 2014 dasturida ikki o’lchovli massivlar va ular ustida amallar bajarish. Quyidagi ko’rinishida belgilangan asosda aniqlaniladi va dasturga kiritiladi. matlabda ma'lumotlar faqat matritsa shaklida tashkil qilinadi. Buning esa 3ta usuli bor:
ma'lumotlarni klaviaturadan to’qridan-to’qri kiritish;
ma'lumotlarni faylli disklardan kiritish;
ma'lumotlarni matlab komandalari yordamida xosil qilish.
Klaviaturadan to’g’ridan-to’g’ri kiritishga misollar ko’raylik:
>> х= [ 5 4 -3]yoki >> х= [ 5, 4, -3]
tеrilsa, x – vеktor-qator dеb qabul qilinib х(1) =5, х(2)=4, х(3)= -3 bo’ladi.
>> у= [ 0 2 2 3
5 -3 6 2 ] yoki у= [ 0 2 2 3; 5 -3 6 2 ]
у-(2х4) o’lchovli matritsa bo’ladi vа у(1.1)=0, у(1.2)=2, у(1.3)=2, у(1.4)=3, у(2.1)=5, у(2.2)=3, у(2.3)= 6, у(2.4)=2 bo’ladi. Matritsada (;) qatorlar orasini ajratish uchun kеrak.
Matritsa elеmеntlari ifoda bo’lishi mumkin:
Z= [sin(0) sqrt(4) 2^3+1 5/2 3^2].
U holda quyidagi vеktor aniqlanadi:
Z= [0 4.000 9.000 2.500 9.000]
Bеrilgan matritsani kеngaytirish orqali ham matritsa qolishi mumkin. Masalan, x1q [x 1 2] dеb olsak,
х1= [5 4 -3 1 2] xosil bo’ladi.
Agar х(5)= 7 dеsak, avvalgi x vеktor х= [5 4 -3 0 8] kabi kеngaytiriladi, bunda ko’rinib turibdiki, x(4) ga “0” qiymat bеrildi.
Endi u matritsadan foydalanib,
c= [1 2 3 4]
y1= [y; c]
bеlgilash natijasida
y1= [0 2 2 3
5 -3 6 2
1 2 3 4]
matritsani xosil qilamiz.
Amaliy qism:
O’lchamlari nxm bo’lgan matritsa elementlari yig’indisini toping. n,m-natural son.
function m1()
n=input('n=');
m=input('m=');
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
a
end
Natija:
>> m1
n=2
m=2
a(1,1)=1
a(1,2)=4`
a(2,1)=3
a(2,2)=5
a = 1 4
3 5
O’lchamlari nxm bo’lgan matritsa hosil qiling.n,m-natural son.
function m2()
n=input('n=');
m=input('m=');
s=0;
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
for i=1:n
for j=1:m
s=s+a(i,j);
end
end
disp(s);
end
Natija: >> m2
n=3
m=3
a(1,1)=4
a(1,2)=6
a(1,3)=8
a(2,1)=9
a(2,2)=5
a(2,3)=3
a(3,1)=4
a(3,2)=8
a(3,3)=9
56
3.O’lchamlari nxm bo’lgan matritsaning eng kichik elementlari yig’indisini toping. n,m-natural son
function m3()
n=input('n=');
m=input('m=');
s=0;
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
k=a(1,1);
for i=1:n
for j=1:m
if k>a(i,j);
k=a(i,j);
end
end
end
for i=1:n
for j=1:m
if k==a(i,j);
s=s+a(i,j);
end
end
end
disp(s);
end
Natija:
>> m3
n=2
m=2
a(1,1)=4
a(1,2)=8
a(2,1)=9
a(2,2)=12
4
4.O’lchamlari nxm bo’lgan matritsaning eng katta elementlari yig’indisini toping.
function m4()
n=input('n=');
m=input('m=');
s=0;
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
k=a(1,1);
for i=1:n
for j=1:m
if k
k=a(i,j);
end
end
end
for i=1:n
for j=1:m
if k==a(i,j);
s=s+a(i,j);
end
end
end
disp(s);
end
Natija:
>> m4
n=2
m=2
a(1,1)=12
a(1,2)=25
a(2,1)=78
a(2,2)=68
78
6.O’lchamlari nxm bo’lgan matritsani toq satridagi elementlarini chiqaring. n,m-natural son.
function m6()
n=input('n=');
m=input('m=');
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
for i=1:2:n
for j=1:m
disp(a(i,j))
end
end
end
Natija:
>> m6
n=2
m=2
a(1,1)=4
a(1,2)=15
a(2,1)=36
a(2,2)=76
4
15
7.O’lchamlari nxm bo’lgan matritsaning juft satrdagi elementlarini chiqaring. n,m-natural son.
function m7()
n=input('n=');
m=input('m=');
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
for i=2:2:n
for j=1:m
disp(a(i,j))
end
end
end
Natija:
>> m7
n=2
m=2
a(1,1)=16
a(1,2)=18
a(2,1)=25
a(2,2)=34
25
34
8.A matrisaga B matrisa ko`paytirilsin. Hosil bo`lgan C matrisani n dan katta elementlarini o`rtacha kiymati hisoblansin.
function m8()
n=input('n=');
n1=input('n1=');
m1=input('m1=');
for i=1:n1
for j=1:m1
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
for i=1:n1
for j=1:m1
fprintf('b(%.d,%.d)=',i,j);
b(i,j)=input('');
end
end
c=a*b;
disp(c);
s=0;
k=0;
for i=1:n1
for j=1:m1
if n
k=k+1;
s=s+c(i,j);
end
end
end
s=s/k;
disp(s);
end
Natija:
>> m8
n=2
n1=2
m1=2
a(1,1)=23
a(1,2)=45
a(2,1)=29
a(2,2)=89
b(1,1)=1
b(1,2)=27
b(2,1)=87
b(2,2)=99
3938 5076
7772 9594
6595
9.O’lchamlari nxm bo’lgan matrisa ustida quyidagi amallar bajarilsin: matrisani eng kichik elementi joylashgan qator va ustun elementlarini o’rni almashtirilsin. n,m-natural son.
function m9()
n=input('n=');
m=input('m=');
c=1;
b=1;
p=0;
for i=1:n
for j=1:m
fprintf('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
k=a(1,1);
for i=1:n
for j=1:m
if k>a(i,j)
k=a(i,j);
b=i;
c=j;
end
end
end
k=1;
for i=1:n
for j=1:m
if j==c
for q=1:m
p=a(q,c);
a(q,c)=a(b,q);
a(b,q)=p;
end
end
end
break;
end
disp(a)
end
Natija:
>> m9
n=2
m=2
a(1,1)=13
a(1,2)=64
a(2,1)=59
a(2,2)=79
13 59
64 79
O’lchamlari nxm bo’lgan matrisa ustida quyidagi amallar bajarilsin: matrisani eng katta elementi joylashgan qator va ustun elementlarini o’rni almashtirilsin. n,m-natural son.
function m10()
n=input('n=');
m=input('m=');
c=1;
b=1;
p=0;
for i=1:n
for j=1:m
fprintf ('a(%.d,%.d)=',i,j);
a(i,j)=input('');
end
end
disp(a);
k=a(1,1);
for i=1:n
for j=1:m
if k
k=a(i,j);
b=i;
c=j;
end
end
end
k=1;
for j=1:m
if j==c
for q=1:m
p=a(q,c);
a(q,c)=a(b,q);
a(b,q)=p;
end
end
end
disp(a);
end
Natija:
>> m10
n=2
m=2
a(1,1)=44
a(1,2)=56
a(2,1)=150
a(2,2)=256
44 56
150 256
44 150
56 256
Do'stlaringiz bilan baham: |