OʻZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI
VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT
AXBOROT TEXNOLOGIYALARI UNIVERSITETI
1 – Laboratoriya topshirig'i
MAVZU: Chiziqli, tarmoqlanuvchi va takrorlanuvchi algoritmlar
Infokommunikatsiya injiniring fakulteti
430-21 - guruh talabasi
Bajardi: Tlepbaev Allambergen
Tekshirdi: Begimov O'ktam
x = a + (b-a)*rand(1,M)
F(monte_carlo) = (b-a)/M * sum(f)
F(monte_carlo)
Tamom
F(simpson)
F (simpson)= (h/3)*(f(1) + 4*sum(f(2:2:end-1)) + 2*sum(f(3:2:end-2)) + 4*sum(f(4:2:end-3)) + f(end))
h= (b-a)/N
a, b, N, h
F(x) = (x.^3+x+1)*exp(-x.^2)
Boshlash
OR
OR
OR
#include
#include
#include
#include
using namespace std;
double f(double x) {
return (x.^3+x+1)*exp(-x.^2);
}
double simpson(double a, double b, int N) {
double h = (b - a) / N;
double sum = 0;
for (int i = 0; i <= N; i++) {
double xi = a + i * h;
if (i == 0 || i == N) {
sum += f(xi);
} else if (i % 2 == 1) {
sum += 4 * f(xi);
} else {
sum += 2 * f(xi);
}
}
return h / 3 * sum;
}
double monte_carlo(double a, double b, int M) {
double sum = 0;
srand(time(NULL));
for (int i = 0; i < M; i++) {
double x = a + (b - a) * rand() / RAND_MAX;
sum += f(x);
}
return (b - a) * sum / M;
}
int main() {
int N, M;
double a = 0, b = 2;
cout << "Integralni hisoblash uchun sinovlar va bo'linish sonini kiriting:\n";
cout << "N = "; cin >> N;
cout << "M = "; cin >> M;
double integral_simpson = simpson(a, b, N);
double integral_monte_carlo = monte_carlo(a, b, M);
cout << "Simpson usuli bilan integral qiymati: " << integral_simpson << endl;
cout << "Monte-Karlo usuli bilan integral qiymati: " << integral_monte_carlo << endl;
return 0;
}
Boshlash
F(x)=ln(x+2) -x^2
F(x)=x^3+4x-7
b-a
f(a)f(с)<0
с=(a+b)/2
F
F(a)(f(b)0
a, b
C
C=(b+a)/2
Tamom
Tenglamani Newton-Raphson usuli bilan yechish uchun quyidagi kod yoziladi:
#include
#include
using namespace std;
double f(double x) {
return x*x*x - x + 7;
}
double df(double x) {
return 3*x*x - 1;
}
double newtonRaphson(double x0, double E) {
double x1 = x0 - f(x0)/df(x0);
while (abs(x1-x0) > E) {
x0 = x1;
x1 = x0 - f(x0)/df(x0);
}
return x1;
}
int main() {
double x0 = 1, E = 0.001;
double yechim = newtonRaphson(x0, E);
cout << "Tenglamaning yechimi: " << yechim << endl;
return 0;
}
Algebraik tenglama uchun vatarlar va urinmalar usuli bilan yechish:
#include
#include
using namespace std;
double g(double x) {
return x*x*x + 4*x - 6;
}
double algebraikTenglama(double a, double b, double E) {
while (abs(b-a) > E) {
double c = (a+b)/2;
if (g(c) == 0) {
return c;
} else if (g(a) * g(c) < 0) {
b = c;
} else {
a = c;
}
}
return (a+b)/2;
}
int main() {
double a = 1, b = 2, E = 0.001;
double yechim = algebraikTenglama(a, b, E);
cout << "Algebraik tenglama uchun yechim: " << yechim << endl;
return 0;
}
Transtsendent tenglama uchun vatarlar va urinmalar usuli bilan yechish:
#include
#include
using namespace std;
double f(double x) {
return x*x- long(x+2);
}
double transtsendentTenglama(double x, double y, double E) {
while (abs(y-x) > E) {
double z = (x+y)/2;
if (f(z) == 0) {
return z;
} else if ((f(x) - x) * (f(z) - z) < 0) {
y = z;
} else {
x = z;
}
}
return (x+y)/2;
}
int main() {
double x = 0, y = M_PI/2, E = 0.001;
double yechim = transtsendentTenglama(x, y, E);
cout << "Transtsendent tenglama uchun yechim: " << yechim << endl;
return 0;
}
#include
#include
using namespace std;
double f(double x) {
return log(x) + 2*sqrt(x);
}
double transtsendentTenglama(double x, double y, double E) {
while (abs(y-x) > E) {
double z = (x+y)/2;
if (f(z) == 0) {
return z;
} else if ((f(x) - x) * (f(z) - z) < 0) {
y = z;
} else {
x = z;
} }
return (x+y)/2;
}
int main() {
double x = 0.001, y = 1, E = 0.001;
double yechim = transtsendentTenglama(x, y, E);
cout << "Transtsendent tenglama uchun yechim: " << yechim << endl;
return 0;
}
Boshlash
1) Tenglama ildizlarini ajratish iteratsion metodi yordamida 0,001 aniqlikda hisoblash:
#include
#include
using namespace std;
double f(double x) {
return x * pow(2, x) - 1;
}
double iteratsionMetodi(double x, double E) {
double xn = x;
while (true) {
double xn1 = log10(1/xn)/log10(2);
if (abs(xn1-xn) < E) {
return xn1;
}
xn = xn1;
}
}
int main() {
double x = 0.5, E = 0.001;
double yechim = iteratsionMetodi(x, E);
cout << "Tenglama ildizlari uchun yechim: " << yechim << endl;
return 0;
}
2) Vatarlar va urinmalar usullari yordamida tenglama taqribiy ildizlarini 0,001 aniqlikda hisoblash:
#include
#include
using namespace std;
double f(double x) {
return 2*x + log10(x) + 0.5;
}
double vatarlarVaUrinmalar(double a, double b, double E) {
double c = a;
while ((b-a) >= E) {
c = (a+b)/2;
if (f(c) == 0.0) {
break;
} else if (f(c)*f(a) < 0) {
b = c;
} else {
a = c;
}
}
return c;
}
int main() {
double a = 0, b = 1, E = 0.001;
double yechim = vatarlarVaUrinmalar(a, b, E);
cout << "Vatarlar va urinmalar usuli bilan yechim: " << yechim << endl;
return 0;
}
3) Tenglamaning taqribiy oraliqlari topib, ularning yaqinlashish tezligini baholash:
#include
#include
using namespace std;
double f(double x) {
return 2*x + log10(x) + 0.5;
}
void taqribiyOraliqlar(double x, double E) {
double h = 0.0001;
double f1 = (f(x+h)-f(x))/h;
double f2 = (f(x+h)-2*f(x)+f(x-h))/(h*h);
double R1 = abs(f1*x/f(x));
double R2 = abs(f2*x*x/(2*f(x)));
cout << "Taqribiy oraliqlar: " << endl;
cout << "R1 = " << R1 << endl;
cout << "R2 = " << R2 << endl;
double yaqinlashtirishTezligi = abs(f(x)/(x*f1));
cout << "Yaqinlashtirish tezligi: " << yaqinlashtirishTezligi << endl;
}
int main() {
double x = 0.641, E = 0.001;
taqribiyOraliqlar(x, E);
return 0;
}
0>
Do'stlaringiz bilan baham: |