1-variant 1-2-topshiriq Quyidagi chiziqli va tarmoqlanuvchi jarayonlarni yechish algoritmi va dasturi tuzilsin


Download 1.71 Mb.
Sana24.12.2022
Hajmi1.71 Mb.
#1057941

1-variant
1-2-topshiriq
Quyidagi chiziqli va tarmoqlanuvchi jarayonlarni yechish algoritmi
va dasturi tuzilsin.

1)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double x,b,y=0;
cout<<"b = ";
cin>>b;
cout<<"x = ";
cin>>x;
y = 2*sin(x)-b*b*b;
cout<<"y = "<return 0;
}

2)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double m,x,b,y;
cout<<"x = ";
cin>>x;
cout<<"m = ";
cin>>m;
b= 3*m;
y = 2*sin(x)-b*b*b;
cout<<"y = "<return 0;
}

3)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double a,x,y=0;
cout<<"x = ";
cin>>x;
cout<<"a = ";
cin>>a;
if(x<2){
y = x*x+2;
}
else if(x>=2 && x<=5){
y = x*x-4*x;
}
else{
y = a*x*x*x;
}
cout<<"y = "<return 0;
}
4)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double a,k,x,y=0;
cout<<"x = ";
cin>>x;
cout<<"a = ";
cin>>a;
k = log(a)/log(5);
if(x>3){
y = pow(pow(x,5),1.0/3);
}
else if(x>-4 && x<=3){
y = 1/atan(x);
}
else{
y = k*exp(5);
}
cout<<"y = "<return 0;
}

3-topshiriq
Quyidagi takrorlanuvchi jarayonlarni yechish algoritmi, hamda
while va do while operatorlari yordamida dasturi tuzilsin.

1)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double y=0,h,S=0,b;
double x1,x2;
cout<<"b = ";
cin>>b;
cout<<"x ning boshlanish qiymati : "<cout<<"x1 = ";
cin>>x1;
cout<<"x ning oxirgi qiymati :"<cout<<"x2 = ";
cin>>x2;
cout<<"x ning o'sib borish intervali :"<cout<<"h = ";
cin>>h;
double x=x1;
while(xy = pow(sin(x),2)-b;
cout<<"y = "<S+=y;
x+=h;
}
cout<<"y lar yiug'indisi = "<return 0;
}


2)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double a,b,y=0,S=0,d1,d2,hd;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"d ning boshlanish qiymati : "<cout<<"d1 = ";
cin>>d1;
cout<<"d ning oxirgi qiymati :"<cout<<"d2 = ";
cin>>d2;
cout<<"d ning o'sib borish intervali :"<cout<<"hd = ";
cin>>hd;
double d=d1;
do
{
y = a*pow(b,3)-2*d;
cout<<"y = "<S+=y;
d+=hd;
}while(d<=d2);
cout<<"y lar yiug'indisi = "<return 0;
}

4-topshiriq
Quyidagi takrorlanuchi jarayonlarni yechish algoritmi hamda for
operatori yordamida dasturi tuzilsin.

1)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double b,c,h,k,y=0,S=0,a1,a2,ha;
cout<<"c = ";
cin>>c;
cout<<"h = ";
cin>>h;
cout<<"k = ";
cin>>k;
cout<<"b = ";
cin>>b;
cout<<"a ning boshlanish qiymati : "<cout<<"a1 = ";
cin>>a1;
cout<<"a ning oxirgi qiymati :"<cout<<"a2 = ";
cin>>a2;
cout<<"a ning o'sib borish intervali :"<cout<<"ha = ";
cin>>ha;
double a=a1+ha;
do
{
if(b!=0){
y = (a*h)/b+3*c*k;
cout<<"y = "<S+=y;
a+=ha;
}
}while(acout<<"y lar yiug'indisi = "<return 0;
}
2)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double h1,h2,a1,a2,ha,hh,S=0,y=0;

cout<<"h ning boshlanish qiymati : "<
cout<<"h1 = ";
cin>>h1;
cout<<"h ning oxirgi qiymati :"<cout<<"h2 = ";
cin>>h2;
cout<<"h ning o'sib borish intervali :"<cout<<"hh = ";
cin>>hh;
cout<<"a ning boshlanish qiymati : "<cout<<"a1 = ";
cin>>a1;
cout<<"a ning oxirgi qiymati :"<cout<<"a2 = ";
cin>>a2;
cout<<"a ning o'sib borish intervali :"<cout<<"ha = ";
cin>>ha;
for(double h=h1;h<=h2;h+=hh){
for(int a=a1;a<=a2;a+=ha){
y = h*h*h+a*h*h+exp(h);
cout<<"y = "<S+=y;
}
}
cout<<"y lar yiug'indisi = "<return 0;
}


5-topshiriq
Quyidagi tenglamalar sistemalarini
Gauss usuli yordamida yeching.

Dastur kodi:
#include
using namespace std;

#define N 4


int forwardElim(double mat[N][N]);
void backSub(double mat[N][N]);
void gaussianElimination(double mat[N][N])
{
int singular_flag = forwardElim(mat);
if (singular_flag != -1)
{
printf("Singular matritsa.\n");
if (mat[singular_flag][N])
printf("Mos kelmaydi.");
else
printf("Cheksiz ko'p yechimlar.");

return;
}


backSub(mat);
}

void swap_row(double mat[N][N], int i, int j)


{

for (int k=0; k<=N; k++)


{
double temp = mat[i][k];
mat[i][k] = mat[j][k];
mat[j][k] = temp;
}
}
void print(double mat[N][N])
{
for (int i=0; ifor (int j=0; j<=N; j++)
printf("%lf ", mat[i][j]);

printf("\n");


}
int forwardElim(double mat[N][N])
{
for (int k=0; k{
int i_max = k;
int v_max = mat[i_max][k];
for (int i = k+1; i < N; i++)
if (abs(mat[i][k]) > v_max)
v_max = mat[i][k], i_max = i;
if (!mat[k][i_max])
return k;
if (i_max != k)
swap_row(mat, k, i_max);

for (int i=k+1; i
{
double f = mat[i][k]/mat[k][k];
for (int j=k+1; j<=N; j++)
mat[i][j] -= mat[k][j]*f;
mat[i][k] = 0;
}

}
return -1;


}

void backSub(double mat[N][N])


{
double x[N];
for (int i = N-1; i >= 0; i--)
{
x[i] = mat[i][N];

for (int j=i+1; j
{
x[i] -= mat[i][j]*x[j];
}
x[i] = x[i]/mat[i][i];
}

printf("\nYechim:\n");


for (int i=0; iprintf("%lf\n", x[i]);
}

int main()


{
double mat[N][N] = {{1.7, -1.8, 1.9, -57.4},
{1.1, -4.3, 1.5, -1.7},
{1.2, 1.4, 1.6, 1.8},
{7.1, -1.3, -4.1, 5.2}
};

gaussianElimination(mat);

return 0;
}
Natija:______8-topshiriq._Quyidagi_shartli_minimallash_masalalarini_Simpleks_usuli_bilan_yeching'>Natija:______7-Topshiriq_Quyidagi_integrallarni_to’g’ri_to’rtburchaklar,_trapetsiyalar_va'>Natija:_______Eyler_usuli_Dastur_kodi'>Natija:_____6-Topshiriq_Quyidagi_differensial_tenglamalarni_Eyler_va_Runge-Kutta_usullari_yordamida_taqriban_yeching'>Natija:




6-Topshiriq
Quyidagi differensial tenglamalarni Eyler va Runge-Kutta usullari
yordamida taqriban yeching

Dastur kodi: Runge-Kutta
#include
using namespace std;
float dydx(float x, float y)
{
return((x - y)/2);
}

float rungeKutta(float x0, float y0, float x, float h)


{
int n = (int)((x - x0) / h);

float k1, k2, k3, k4, k5;


float y = y0;
for (int i=1; i<=n; i++)
{
k1 = h*dydx(x0, y);
k2 = h*dydx(x0 + 0.5*h, y + 0.5*k1);
k3 = h*dydx(x0 + 0.5*h, y + 0.5*k2);
k4 = h*dydx(x0 + h, y + k3);
y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);;
x0 = x0 + h;
}

return y;


}

int main()


{
float x0,y,x,h;
cout<<"x0 = ";
cin>>x0;
cout<<"x = ";
cin>>x;
cout<<"y = ";
cin>>y;
cout<<"h = ";
cin>>h;
cout << "y ning x da qiymati : " <<
rungeKutta(x0, y, x, h);

return 0;


}
Natija:






Eyler usuli
Dastur kodi:
#include
using namespace std;
float func(float x, float y)
{
return (x + y + x * y);
}

void euler(float x0, float y, float h, float x)


{
float temp = -0;
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
cout << "Taxminiy yechim x = "
<< x << " va " << y << endl;
}

int main()


{
float x0;
float x;
cout<<"x0 = ";
cin>>x0;
cout<<"x = ";
cin>>x;
float y0;
cout<<"y0 = ";
cin>>y0;
float h;
cout<<"h = ";
cin>>h;

euler(x0, y0, h, x);


return 0;
}
Natija:





7-Topshiriq
Quyidagi integrallarni to’g’ri to’rtburchaklar, trapetsiyalar va
Simpson formulalari yordamida taqribiy hisoblang

Simpson
Dastur kodi:
#include
#include
using namespace std;
float func(float x)
{
return log(x);
}

float simpsons_(float ll, float ul, int n)


{
float h = (ul - ll) / n;
float x[10], fx[10];
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}

int main()


{
float pastki;
float yuqori;
cout<<"pastki = ";
cin>>pastki;
cout<<"yuqori = ";
cin>>yuqori;
int n = 6;
cout << simpsons_(pastki, yuqori, n);
return 0;
}

Natija:





8-topshiriq.
Quyidagi shartli minimallash masalalarini Simpleks usuli bilan
yeching:

Dastur kodi:
#include
#include
#include
#include
#include
#include
using namespace std;
#define Maxn 25
const double eps = 0.00000001, INF = 1e15;
int n, m;
int id[Maxn * 2];
double a[Maxn][Maxn];
double myabs(double x) { return x > 0 ? x : -x; }
void Pivot(int l, int e)
{
swap(id[n + l], id[e]);
double t = a[l][e];
a[l][e] = 1;
for (int j = 0; j <= n; j++)
a[l][j] /= t;
for (int i = 0; i <= m; i++)
if (i != l && (myabs(a[i][e]) > eps))
{
t = a[i][e];
a[i][e] = 0;
for (int j = 0; j <= n; j++)
a[i][j] -= a[l][j] * t;
}
}
bool init()
{
while (1)
{
int e = 0, l = 0;
for (int i = 1; i <= m; i++)
if ((a[i][0] < -eps) && (!l || (rand() & 1)))
l = i;
if (!l)
break;
for (int j = 1; j <= n; j++)
if ((a[l][j] < -eps) && (!e || (rand() & 1)))
e = j;
if (!e)
{
printf("Mumkin emas\n");
return 0;
}
Pivot(l, e);
}
return 1;
}
bool simplex()
{
while (1)
{
int l = 0, e = 0;
double mn = INF;
for (int j = 1; j <= n; j++)
if (a[0][j] > eps)
{
e = j;
break;
}
if (!e)
break;
for (int i = 1; i <= m; i++)
if ((a[i][e] > eps) && (a[i][0] / a[i][e] < mn))
mn = a[i][0] / a[i][e], l = i;
if (!l)
{
printf("Mumkin\n");
return 0;
}

Pivot(l, e);


}
return 1;
}

double ans[Maxn];



int main()
{
srand(time(0));
int t;
scanf("%d%d%d", &n, &m, &t);
for (int i = 1; i <= n; i++)
scanf("%lf", &a[0][i]);
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
scanf("%lf", &a[i][j]);
scanf("%lf", &a[i][0]);
}
for (int i = 1; i <= n; i++)
id[i] = i;
if (init() && simplex())
{
printf("%.8lf\n", -a[0][0]);
if (t)
{
for (int i = 1; i <= m; i++)
ans[id[n + i]] = a[i][0];
for (int i = 1; i <= n; i++)
printf("%.8lf ", ans[i]);
}
}
return 0;
}
Natija:






9-topshiriq.

Jadval ko‟rinishida berilgan quyidagi transport masalalarini
potensiallar usuli bilan yeching:

Dastur kodi:
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
setlocale( LC_ALL,"Russian" );
int n,m,F=0,min2=0;
int min,i,j,sum_a=0,sum_b=0,ind_i=0,ind_j=0;
cout<<"Qatorlar soni: ";
cin>>n;
cout<<"Ustunlar soni: ";
cin>>m;
int u[n],v[m];
int matr[m][n];
int matr_ind[m][n];
int matr_pm[m][n];
int matr_koef[m][n];
int a[n];
int b[m];
for(i=0;i{
for(j=0;j{
matr_ind[i][j]=0;
matr_pm[i][j]=0;
}
}
for(i=0;i{
u[i]=100;
}
for(j=0;j{
v[j]=100;
}
cout<<"Matritsa koeffitsienti"<<"\n";
for(i=0;i{
for(j=0;jcin>>matr_koef[i][j];
}
cout<<"a kiriting:"<<"\n";
for(i=0;icin>>a[i];
cout<<"b kiriting:"<<"\n";
for(i=0;icin>>b[i];
for(i=0;isum_a+=a[i];
for(i=0;isum_b+=b[i];
if(sum_a==sum_b)
{
cout<<"matritsa yaqinlashadi";
cout<<"\n";
}
if(sum_a>sum_b)
{
cout<<"matritsa yaqinlashmaydi";
system("PAUSE");
return EXIT_SUCCESS;
}
if(sum_a{
cout<<"matritsa yaqinlashmaydi";
system("PAUSE");
return EXIT_SUCCESS;
}
cout<<"\n";
for(i=0;i{
min = 500;
for(j=0;j{
if(matr_koef[i][j]{
min=matr_koef[i][j];
ind_i=i;
ind_j=j;
}
}
matr_ind[ind_i][ind_j]++;
}
for(j=0;j{
min=500;
for(i=0;i{
if(matr_koef[i][j]{
min=matr_koef[i][j];
ind_i=i;
ind_j=j;
}
}
matr_ind[ind_i][ind_j]++;
}
for(i=0;i{
for(j=0;j{
if(matr_ind[i][j]==2)
if(a[i]>=b[j])
{
matr[i][j]=a[i]-(a[i]-b[j]);
a[i]=a[i]-b[j];
b[j]=0;
}
else
{
matr[i][j]=b[j]-(b[j]-a[i]);
b[j]=b[j]-a[i];
a[i]=0;
}
}
}
for(i=0;i{
for(j=0;j{
if(matr_ind[i][j]==1)
if(a[i]>=b[j])
{
matr[i][j]=a[i]-(a[i]-b[j]);
a[i]=a[i]-b[j];
b[j]=0;
}
else
{
matr[i][j]=b[j]-(b[j]-a[i]);
b[j]=b[j]-a[i];
a[i]=0;
}
}
}
for(i=0;i{
for(j=0;j{
if(matr_ind[i][j]==0)
if(a[i]>=b[j])
{
matr[i][j]=a[i]-(a[i]-b[j]);
a[i]=a[i]-b[j];
b[j]=0;
}
else
{
matr[i][j]=b[j]-(b[j]-a[i]);
b[j]=b[j]-a[i];
a[i]=0;
}
}
}
for(i=0;i{
for(j=0;j{
cout<}
cout<<"\n";
}
for(i=0;i{
for(j=0;j{
F+=matr[i][j]*matr_koef[i][j];
}
}
cout<<"F(x)="<u[0]=0;
for(i=0;i{
for(j=0;j{
if((matr[i][j]!=0)&&(v[j]==100)&(u[i]!=100))
{
v[j]=matr_koef[i][j]-u[i];
}
if((matr[i][j]!=0)&&(v[j]!=100)&(u[i]==100))
{
u[i]=matr_koef[i][j]-v[j];
}
}
}
for(i=0;i{
for(j=0;j{
if((u[i]==100)&&(v[j]!=100)&&(matr[i][j]!=0))
u[i]=matr_koef[i][j]-v[j];
if((u[i]!=100)&&(v[j]==100)&&(matr[i][j]!=0))
v[j]=matr_koef[i][j]-u[i];
}
}
cout<<"Potensial V:";
for(i=0;i{
cout<}
cout<<"\n"<<"Potentsial U:";
for(j=0;j{
cout<}
cout<<"\n";
for(i=0;i{
for(j=0;j{
if((matr[i][j]==0)&&(u[i]+v[j]-matr_koef[i][j]>0.1))
{
cout<<"Malumot rejasi ketma-ket optimal emas "<matr_pm[i][j]++;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Download 1.71 Mb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling