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


Download 19.61 Kb.
Sana24.12.2022
Hajmi19.61 Kb.
#1057958

2-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)-sqrt(b);
cout<<"y = "<return 0;
}
2)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double n,m,x,y=0,t;
cout<<"x = ";
cin>>x;
cout<<"n = ";
cin>>n;
cout<<"m = ";
cin>>m;
if(n!=0){
n = m/(3*n);
}
y = 2*sin(x)+5*n;
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<0){
y = sin(pow(abs(x),3));
}
else if(x>0){
y = pow(x,7);
}
else{
y = a*x*x;
}
cout<<"y = "<return 0;
}
4)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double a,b,m,x,y=0;
cout<<"x = ";
cin>>x;
cout<<"a = ";
cin>>a;
cout<<"m = ";
cin>>m;
b = (2*m)/a;
if(x>1){
y = 5*x*x*x+b;
}
else if(x>=1 && x<3){
y = pow((x-2),2)*b;
}
else{
y = a*sqrt(x);
}
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 = 2*sin(x)-b*b*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 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;
if(b>=2 && b<=5){
while(xy = 2*sin(x)-b*b*b;
cout<<"y = "<S+=y;
x+=h;
}
}
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 a,y=0,S=0,h1,h2,hh;
cout<<"a = ";
cin>>a;
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;
double h=h1;
do
{
y = log(h)-a*a;
cout<<"y = "<S+=y;
h+=hh;
}while(h


cout<<"y lar yiug'indisi = "<return 0;
}
2)
Dastur kodi:
#include
#include
using namespace std;

int main()


{
double b1,b2,a1,a2,ha,hb,S=0,y=0;

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;
cout<<"b ning boshlanish qiymati : "<cout<<"b1 = ";
cin>>b1;
cout<<"b ning oxirgi qiymati :"<cout<<"b2 = ";
cin>>b2;
cout<<"b ning o'sib borish intervali :"<cout<<"hb = ";
cin>>hb;
for(double a=a1;a<=a2;a+=ha){
for(int b=b1;b<=b2;b+=hb){
y = (cos(a)+log(b))/(b*b-pow(sin(a),2));
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] = {{8.2, -3.2, 14.2, 14.8},
{5.6, -12.0, 15.0, -6.4},
{5.7, 3.6, -12.4, -2.3},
{6.8, 13.2, -6.3, -8.7}
};

gaussianElimination(mat);

return 0;
}


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;


}

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;
}


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;
}


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;
}


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 19.61 Kb.

Do'stlaringiz bilan baham:




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