Laboratoriya ishi -1 Mavzu: Chiziqli, tarmoqlanuvchi va takrorlanuvchi algoritmlar Bajardi: 714 -guruh talabasi Mazamqulov Sultonbek toshkent 2022
Sana 29.05.2022 Hajmi 0,89 Mb. #618270
Bog'liq
algoritm lab 888888888888.1
O`ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
kafedrasi
Algoritmlarni loyihalash fanidan
Laboratoriya ishi -1
Mavzu: Chiziqli, tarmoqlanuvchi va takrorlanuvchi algoritmlar
Bajardi:714 -guruh talabasi
Mazamqulov Sultonbek
TOSHKENT 2022
8.
Yechim joylashgan [a; b] oraliqni aniqlang vaoraliqni ikkiga bo’lish, vatarlar va urinmalar usuli bilan toping. E=0.001.
a) x3 -2x2 +7x-1=0
b)
1.Oraliqni ikkiga bo’lish usuli
#include
#include
using namespace std ;
float funksiya(float x){
return x*x*x - 2*x*x+7*x -1;
}
int main() {
boshlanish:
float a, b, c;
cout << " a = ";cin >> a;
cout << " b = ";cin >> b;
if(funksiya(a)*funksiya(b) < 0){
yechim:
c = (a + b) / 2;
if(funksiya(a)*funksiya(c) < 0){
b = c;
}
if(funksiya(c)*funksiya(b) < 0){
a = c;
}
if(fabs(a - b) < 0.001){
cout << " Funksiya [" << a << ";" << b << "] oraliqda yechimga ega " << endl;
cout << " c = " << c << endl;
}
else {
goto yechim;
}
}
else{
cout << " yechimga ega emas" << endl;
cout << " a va b larni qaytadan kiriting:" << endl;
goto boshlanish;
}
return 0;}
2.Vatarlar usuli
#include
#include
using namespace std;
double func(double x)
{
return x*x*x - 2*x*x+7*x -1 ;
}
int main() {
double a, b, x, e;
cout << " e = "; cin >> e;
yechim:
cout << " a = "; cin >> a;
cout << " b = "; cin >> b;
if (func(a) * func(b) > 0.0f)
{
cout << " a va b larni qaytadan kiriting:" << endl;
goto yechim;
return -1; }
x = (a * func(b) - b * func(a)) / (func(b) - func(a));
while (abs(func(x)) > e) {
if (func(a) * func(x) < 0) {
x = (x * func(a) - a * func(x)) / (func(a) - func(x));
}
else {
x = (x * func(b) - b * func(x)) / (func(b) - func(x));
}
}
cout << " Yechim: x = " << x << endl;
int as;
cin >> as;
return 0;
}
3.Urinmalar usuli
#include
#include
#define e 0.001
using namespace std;
double func(double x)
{
return x*x*x - 2*x*x+7*x -1;
}
double dfunc(double x)
{
return 3*x*x -4*x+7; // tepadigi funksiyani hosilasi yoziladi
}
void urinma(double x)
{
double h = func(x) / dfunc(x);
while (abs(h) >= e)
{
h = func(x)/dfunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
}
cout << " Tenglama ildizining qiymati: x = " << x;
}
int main()
{
double x0 = 1;
urinma(x0);
return 0; }
)
1. Oraliqni ikkiga bolish usuli
#include
#include
using namespace std;
float funksiya(float x){
return x-sqrt(log10(x+2));
}
int main() {
boshlanish:
float a, b, c;
cout << " a = ";cin >> a;
cout << " b = ";cin >> b;
if(funksiya(a)*funksiya(b) < 0){
yechim:
c = (a + b) / 2;
if(funksiya(a)*funksiya(c) < 0){
b = c;
}
if(funksiya(c)*funksiya(b) < 0){
a = c;
}
if(fabs(a - b) < 0.001){
cout << " Funksiya [" << a << ";" << b << "] oraliqda yechimga ega " << endl;
cout << " c = " << c << endl;
}
else {
goto yechim;
}
}
else{
cout << " yechimga ega emas" << endl;
cout << " a va b larni qaytadan kiriting:" << endl;
goto boshlanish;
}
return 0;}
2.Vatarlar usuli
#include
#include
using namespace std;
double func(double x)
{
return x-sqrt(log10(x+2)) ;
}
int main() {
double a, b, x, e;
cout << " e = "; cin >> e;
yechim:
cout << " a = "; cin >> a;
cout << " b = "; cin >> b;
if (func(a) * func(b) > 0.0f)
{
cout << " a va b larni qaytadan kiriting:" << endl;
goto yechim;
return -1;
}
x = (a * func(b) - b * func(a)) / (func(b) - func(a));
while (abs(func(x)) > e) {
if (func(a) * func(x) < 0) {
x = (x * func(a) - a * func(x)) / (func(a) - func(x));
}
else {
x = (x * func(b) - b * func(x)) / (func(b) - func(x));
}
}
cout << " Yechim: x = " << x << endl;
int as;
cin >> as;
return 0;
}
3.Urinmalar usuli
#include
#include
#define e 0.001
using namespace std;
double func(double x)
{
return x-sqrt(log10(x+2));
}
double dfunc(double x)
{
return 1-1/(6.4*(x+2)*(sqrt(log10(x+2)))); // tepadigi funksiyani hosilasi yoziladi
}
void urinma(double x)
{
double h = func(x) / dfunc(x);
while (abs(h) >= e)
{
h = func(x)/dfunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
}
cout << " Tenglama ildizining qiymati: x = " << x;
}
int main()
{
double x0 = 1;
urinma(x0);
return 0;
}
To’g’ri to’rtburchaklar, trapetsiya va Simpson usullarida hisoblang. N bo’lish soni, E=0.001.
[0;1]
12
1.To’g’ri tortburchak usuli.
#include
#include
using namespace std;
float f(float a, float b, float N)
{
float h = (b - a)/N;
float x, s = 0;
for(int i = 1; i <= N; i++)
{
x = a + i*h;
s += (x*x+5)*tan(x*x+1);
}
s *= h;
return s ;
}
int main()
{
cout << endl << " f(x) = (x*x+5)*tan(x*x+1)" << endl;
cout << endl << " Integralni hisoblashning To'g'ri to'rtburchaklar usuli:" << endl << endl;
float a, b, N;
cout << " a = ";
cin >> a;
cout << " b = ";
cin >> b;
cout << " N = ";
cin >> N;
cout << " S = " << f(a, b, N) << endl;
return 0;
}
2.Trapetsiya usuli
#include
#include
using namespace std;
float f(float a, float b, float N)
{
float h = (b - a) / N;
float x, s = 0;
for(int i = 1; i <= N - 1; i++)
{
x = a + i * h;
s += (x*x+5)*tan(x*x+1);
}
return s;
}
float fx(float x)
{
return (x*x+5)*tan(x*x+1);
}
int main()
{
cout << endl << " f(x) = (x*x+5)*tan(x*x+1)" << endl;
cout << endl << " Integralni hisoblashning Trapetsiyalar usuli:" << endl << endl;
float a, b, N;
cout << " a = ";
cin >> a;
cout << " b = ";
cin >> b;
cout << " N = ";
cin >> N;
float h = (b - a) / N;
float s = 0;
s += ((fx(a) + fx(b)) / 2 + f(a, b, N)) * h;
cout << " s = " << s << endl;
return 0;
}
3. Simpson usuli
#include
#include
#include
using namespace std;
float f(float x)
{
return (x*x+5)*tan(x*x+1);
}
int i, m;
float N, a, b, h, s;
int main()
{
cout << endl << " f(x) = (x*x+5)*tan(x*x+1)" << endl;
cout << endl << " Integralni hisoblashning Simpson usuli:" << endl << endl;
cout << " a = ";
cin >> a; // a = 0
cout << " b = ";
cin >> b; // b = 1
cout << " N = ";
cin >> N; // n = 12
h = (b - a) / N;
m = int(N/2);
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 << " S = " << s << endl;
return 0;
}
Do'stlaringiz bilan baham: