Program integral1(input,output);
Uses crt;
var a,b,h,s,J:real;
i,n:integer;
{nostandart funktsiyani tavsiflaymiz}
function f(x:real):real;
begin
f:=ln(x*x+3*x+1);
end;
begin clrscr;
write(‘quyi chegara a=’); readln(a);
write(‘yuqori chegara b=’); readln(b);
write(‘bo’laqlar soni n=’); readln(n);
s:=(f(a)+f(b))/2; h:=(b-a)/n;
for i:=2 to n do
s:=s+f(a+(i-1)*h);
J:=h*s; textcolor(13);
writeln(‘integral kiymati J=’,J:3:4);
end.
|
{Simpson usuli}
Program integral2(input,output);
Uses crt;
var a,b,h,s,J:real;
i,n,k:integer;
function f(x:real):real;
begin
f:=ln(x*x+3*x+1); end;
begin clrscr;
write(‘quyi chegara a=’); readln(a);
write(‘yuqori chegara b=’); readln(b);
write(‘bo’laqlar soni n=’); readln(n);
h:=(b-a)/n; s:=f(a)+f(b); k:=1;
for i:=2 to n do
begin
s:=s+(3+k)*f(a+(i-1)*h); k=-k
end;
J:=s*h/3; textcolor(2);
writeln(‘integral qiymati J=’,J:3:4);
end.
|
Quyidagi masalalar uchun algoritm tuzing va uni tahlil qiling. Dastur kodini yozib natija oling.
1-masala.
Savol : Ikki o’lchamli kvadrat matritsa berilgan. Uning har bir satridagi eng kata elementlarini aniqlash dasturini tuzing.
Dastur kodi :
#include
#include
using namespace std;
void matrix_print(int a[10][10], int m, int n)
{
// matritsani jadval shaklida chiqarish
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "\t";
}
cout << "\n";
}
}
int satr_max(int a[], int n)
{
// massivning eng katta elementini aniqlash
int max = a[0];
for (int i = 1; i < n; i++)
if (max < a[i]) max = a[i];
return max;
}
int main()
{
int m, n, a[10][10];
cout << "Satrlar sonini kiriting \nm="; cin >> m;
cout << "Ustunlar sonini kiriting \nn="; cin >> n;
cout <<"Massiv elementlarini kiriting \n";
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> a[i][j];
cout << "Kiritilgan matritsa\n";
//funksiyaga matritsa, satrlar va ustunlar soni jo'natiladi
matrix_print(a, m, n);
for (int i = 0; i < m; i++)
{
// funksiyaga i-satrning 0-elementi adresini
// va elementlar sonini jo'natamiz
cout << i << "-satrning eng kattasi=" <<
satr_max(&a[i][0], n);
cout << endl;
}
return 0;
}
2-masala.
Quyidagi funksiyani to’rtburchaklar, Trapetsiya va Simpson formulalari yordamida taqribiy hisoblash dasturini tuzing
.
To’g’ri to’rtburchaklar formulasi. Bu formulani keltirib chiqarish uchun dastlab kesmani nuqtalar bilan n ta teng bo’lakka bo’lamiz.
Buning uchun integrallash kesmasini ta bo’lakka bo’lamiz va hisoblashlar natijalarini keltiramiz:
Bizning misolda bo’lgani uchun, to’g’ri to’rtburchaklar formulasiga asosan, quyidagi natijani hosil qilamiz.
X=0.1; =0.9901
X=0.2; =0.9615
X=0.3; =0.9174
X=0.4; =0.8621
….
X=1.0 =0.5000
7.5998
integralni trapetsiyalar formulasi yordamida taqribiy hisoblang. Bunda ham
deb oldik :
Yechish: va
bo’lgani uchun
Dastur kodi
#include
#include
using namespace std;
double funk(double x)
{
return (sin(x)) / (1+x*x));
}
int main()
{
double a,b,S=0, xa;
int n=10;
cout<<"integral chegarasini kiriting"<
cin>>a>>b;
xa=a+0.1;
while (xa
{
S+=funk(xa);
xa+=0.1;
}
S=(a+b)/2+S;
S=S*fabs(b-a)/n;
cout << S;
return 0;
}
Do'stlaringiz bilan baham: |