Muhammad al Xorazmiy nomidagi Toshkent axborot texnologiyalari universiteti
“Telekommunikatsiya texnologiyalari “ fakulteti 414_20-guruh 2-bosqich talabasi
Ismatullayeva Munavvarning Algoritmlarni loyhalash fanidan tayyorlagan
1-labaratoriya ishi
Variant №25
1.1-topshiriq varianlari
Berilgan integralni Simpson hamda Monte-Karlo usulida hisoblang. Oraliqni bo’linish soni
N , hamda sinovlar soni M ko’rsatilgan
Dastur kodi:
#include
#include
#include
using namespace std;
double f(double x)
{
return (pow(x,2)+x);
}
int i, m; float n,a,b,h,s;
int main(int argc, char *argv[]) {
cout<cout<cout<<" a= "; cin>>a;
cout<<" b= "; cin>>b;
cout<<" n= "; cin>>n;
h=(b-a)/n; cout<
m= int(n/2); cout<for (i=1; i<=m; i++){
s+=f(a+(2*i-2)*h) + 4*f(a+(2*i-1)*h) + f(a+2*i*h);
}
s=s*h/3;
cout<
return 0;
}
Dastur natijasi:
1-Vazifa. Tenglamalar yechimlari joylashgan [a; b] oraliqni grafik va analitik usullar bilan
ajrating.
2-Vazifa. Tenglamalar yechimlari joylashgan oraliqlar aniqlangandan so’ng taqribiy
yechimlarini oraliqni teng ikkiga bo’lish usulida E=0.001 aniqlikda hisoblang. Algoritmini tuzib,
dasturlash tilida dastur kodini yozib natija oling.
3-Vazifa. Algebraik va transtsendent tenglamalarning taqribiy yechimlarini vatarlar va
urinmalar usuli bilan toping. Algoritmini tuzib, dasturlash tilida dastur kodini yozib natija oling.
2-misol
A shart
#include
using namespace std;
#define EPSILON 0.001
double func(double x)
{
return 2*pow(x,3)+2*x+1;
}
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "Siz a va b ni to'g'ri kiritmadingiz\n";
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
c = (a+b)/2;
if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "Ildizning qiymati : " << c;
}
int main()
{
double a =-200, b = 300;
bisection(a, b);
return 0;
}
Dastur natijasi:
B shart
Dastur kodi
#include
using namespace std;
#define EPSILON 0.001
double func(double x)
{
return x*sqrt(x+2)-3;
}
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "Siz a va b ni to'g'ri kiritmadingiz\n";
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
c = (a+b)/2;
if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "Ildizning qiymati : " << c;
}
int main()
{
double a =0, b = 1;
bisection(a, b);
return 0;
}
Dastur natijasi:
3-misol
1) Tenglama ildizlarini ajratish iteratsion metodi yordamida 0,001 aniqlikda hisoblash.
2) Vatarlar va urinmalar usullari yordamida tenglama taqribiy ildizlarini 0,001 aniqlikda
hisoblash.
3) Tenglamaning taqribiy oraliqlari topib, ularning yaqinlashish tezligini baholash
A shart
Dastur kodi:
#include
#include
#include
using namespace std;
float math_function(float x) {
return sin(1/2+x)-2*x+1/2;
}
//yechimlar [1:3] oraliqda
int main() {
float a,b;
float c;
float x0,x1;
float E;
qayt:
cout << "Oraliqni kiriting" << endl;
cin >> a >> b;
x0 = math_function(a)*math_function(b);
if(x0>0){
cout << "Yechim bu oraliqda emas" << endl;
goto qayt;
}
cout << "aniqlikni kiriting" << endl;
cin >> E;
qayt1:
c=(a + b)/2;
x1 = math_function(a)*math_function(c);
if(x1<0){
b=c;
}
else {
a=c;
}
//
// if(abs(b-a)>E);{
// goto qayt1;
// }
c=(float(a) + float(b))/2;
cout << "Javob [" << a << ":" << b << "] oraliqda"<< endl;
cout << "Taqribiy yechim -> " << c << endl;
return 0;
}
B shart
Dastur kodi
#include
#include
#include
using namespace std;
float math_function(float x) {
return pow(x,3)+1/5*x-6/5;
}
//yechimlar [1:3] oraliqda
int main() {
float a,b;
float c;
float x0,x1;
float E;
qayt:
cout << "Oraliqni kiriting" << endl;
cin >> a >> b;
x0 = math_function(a)*math_function(b);
if(x0>0){
cout << "Yechim bu oraliqda emas" << endl;
goto qayt;
}
cout << "aniqlikni kiriting" << endl;
cin >> E;
qayt1:
c=(a + b)/2;
x1 = math_function(a)*math_function(c);
if(x1<0){
b=c;
}
else {
a=c;
}
//*
//if(abs(b-a)>E){
//goto qayt1;
//}
c=(float(a) + float(b))/2;
cout << "Javob [" << a << ":" << b << "] oraliqda"<< endl;
}
Do'stlaringiz bilan baham: |