О’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGIMUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
1-labaratoriya
Fan : Komyuterni tashkillashtirish
Guruh : 211-20
Bajardi : Abdumannono.B.
Toshkent - 2022
1-labaratoriya ishi
1-masala. Berilgan integralni Simpson hamda Monte-Karlo usulida hisoblang. Oraliqni bo’linish soni N , hamda sinovlar soni M ko’rsatilgan.
C++ dagi cod:
#include
#include
#include
using namespace std;
float f(float x) { return sin(x*x + x + 1)/ (x*x*x + 5*x + 1);
}
int i,m; float a,b,n,h,s;
int main( int argc,char argv[])
{
cout<<"Integralni Simpson usulida hisoblash"< cout<<"a = ";cin>>a;
cout<<"b = ";cin>>b;
cout<<"n = ";cin>>n;
h=(b-a)/n;cout<<"h = "<m=int(n/2);cout<<"m = "< 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<<"integral = "< }
Natija:
MOTE-KARLO USULI:
#include
#include
#include
#include
double rnd();
double function (double x);
void Monte_Carlo_1 (long* s);
void Monte_Carlo_2 (long* d);
void Monte_Carlo_3 (double x, long* f);
int main()
{
double x;
long max = 10, n = 0, s = 0, d = 0, f = 0;
printf("\n*** Integral ( I : 1 ): Monte Karlo usuli ***\n");
printf("\nTikslus natijalari = 0.0250 \n \n");
printf("Iter. \t I \t II \t III \n \n");
for (int j=0; j < 140; j++)
{
for (int i = 0; i < max; i++)
{
Monte_Carlo_1(&s);
Monte_Carlo_2(&d);
Monte_Carlo_3(x,&f);
n++;
}
printf("%6ld %7.4lf %7.4lf %7.4lf \n", max, (double)s / n - 1, (double)d / n - 1, f / n - 1);
max *= 2;
}
}
double rnd()
{
return (double)rand() / ( RAND_MAX );
}
void Monte_Carlo_1 (long* s)
{
double x = rnd();
double y = rnd();
if(y < function(x))
{
(*s)++;
}
}
void Monte_Carlo_2 (long* d)
{
(*d) += rnd();
}
void Monte_Carlo_3 (double x, long* f)
{
x = sqrt(rnd());
*f += (x / sin(x * x + x + 1) / (x * x * x + 5 * x + 1) / x);
}
double function (double x)
{
return (sin(x * x + x + 1) / (x * x * x + 5 * x + 1));
}
NATIJA:
2-masala.
Algebraik va transtsendent tenglamalarni yechishda oraliqni teng ikkiga bo’lish, iteratsiya usullari.
C++ Dastur kodi:
#include
#include
using namespace std;
float fx(float x){
float y=x*x*x -2*x + 2;
return y;
}
int main(){
float a,b;
cout<<" A va b oraliqni kiriting "<cout<<"a = ";cin>>a;
cout<<"b = ";cin>>b;
if(fx(a)*fx(b)<0){
cout<<"Funksiya yechimga ega "<int i=0,k=1;
float c;
while(k==1){
i++;
c=(a+b)/2;
if(fx(a)*fx(c)<0){
b=c;
}
if(fx(c)*fx(b)<0){
a=c;
}
if(fabs(b-a)<0.001){
cout<<"x= "<k=0;
}
}
}
else{
cout<<" Bu oraliqda yechimga ega emas "< }} Natija:
B)
C++ dastur kodi:
#include
#include
using namespace std;
float fx(float x){
float y=x*4 -exp(x);
return y;
}
int main(){
float a,b;
cout<<" A va b oraliqni kiriting "<cout<<"a = ";cin>>a;
cout<<"b = ";cin>>b;
if(fx(a)*fx(b)<0){
cout<<"Funksiya yechimga ega "<int i=0,k=1;
float c;
while(k==1){
i++;
c=(a+b)/2;
if(fx(a)*fx(c)<0){
b=c;
}
if(fx(c)*fx(b)<0){
a=c;
}
if(fabs(b-a)<0.001){
cout<<"x= "<k=0;
}
}
}
else{
cout<<" Bu oraliqda yechimga ega emas "< }}Natija:
3-masala.
C++ dastur kodi:
#include
#include
using namespace std;// trapetisya usulida hisoblash
float f(float x){
float y=x*x + 4*sin(x);
return y;
}
float fun(float a, float b, int n){
float h=(b-a)/n;
float s=0;
for(int i=1; i<=n ; i++){
s=s+f(a+i*h);
}
s=(f(a)+f(b))/2+s;
s=s*h;
return s;
}
int main() {
float a=1,b=2;
int n=12;
cout<<"S= "<}
Natija:
Blok sxemalar barcha takorlanuvchi dasturlar uchun shu kabi ketma-ketlikda alamga oshadi.
Boshlash
1)
cos(pow(x, 2) + 1) * pow((pow(x, 3) + 3*x + 1), 1/3)
int i,m; float a,b,n,h,s;
a,b,n
m=n/2
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
Tamom
Natija= S
Do'stlaringiz bilan baham: |