MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
RADIO VA MOBIL ALOQA FAKULTETI
Fan: Algoritmlarni loyihalash
Fakultet: Mobil tizimlar
Guruh: CAL002-L1
Bajardi: Umirov Ulugbek
28-variant
1.Berilgan integral qiymatini to‘g‘ri to‘rtburchaklar, trapetsiyalar, Simpson usullarida ε>0 aniqlikda hisoblang. Aniqlikka erishganlik sharti sifatida |S2n-Sn| <ε;
Trapetsiyalar usuli
public class Misol2TrapetsiyalarUsuli {
public static void main(String[] args) {
float x0 = 0;
float xn = 2;
int n = 10;
System.out.println("Natija: " + Math.round(trapezoidal(x0, xn, n) * 10000.0) / 10000.0);
}
static float y(float x) {
return (float) ((float) (x*x + x + 1)/(Math.sqrt(x*x*x + 5)));
}
static float trapezoidal(float a, float b, float n) {
float h = (b - a) / n;
float s = y(a) + y(b);
for (int i = 1; i < n; i++)
s += 2 * y(a + i * h);
return (h / 2) * s;
}
}
Simpson usuli
public class Misol2SimpsonUsuli {
public static void main(String[] args) {
float lower_limit = 0; // quyi chegara
float upper_limit = 2; // yuqori chegara
int n = 10;
System.out.println("Javob: " + simpsons_(lower_limit, upper_limit, n));
}
static float func(float x) {
return (float) ((float) (x*x + x + 1)/(Math.sqrt(x*x*x + 5)));
}
static float simpsons_(float ll, float ul,
int n) {
float h = (ul - ll) / n;
float[] x = new float[100];
float[] fx = new float[100];
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;
} }
To'rtburchaklar usuli
public class Misol2TortburchakUsuli {
public static void main(String[] args) {
double a, b, w, sum, x_i;
int i, n;
a = 0.0;
b = 2.0;
n = 10;
w = (b - a) / n;
sum = 0.0;
double e = Math.E;
for (i = 1; i <= n; i++) {
x_i = a + (i - 1) * w;
sum += (w * (((x_i*x_i + x_i + 1)/(Math.sqrt(x_i*x_i*x_i) + 5))));
}
System.out.println("Natija: " + sum);
}
}
1.2 Berilgan tenglamaning taqribiy yechimini ε>0 aniqlikda urinmalar (Nyuton) va vatarlar usullarida hisoblang. Aniqlikka erishganlik sharti sifatida |xn+1-xn|< ε;
package Laboratoriya1.Misol1;
import java.util.Scanner;
Urinmalar usuli
public class Misol1UrinmalarUsuli {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 0;
System.out.print("[a, b] oraliqni kiritng: ");
double a = scanner.nextDouble(), b = scanner.nextDouble();
double x1, x2 = 0, e = 0.001;
if (func1(a) < 0 && func1(b) > 0) {
if (func2((float) a) * func3((float) a) > 0) {
do {
x1 = b;
x1 = x2;
x2 = x1 - func1(x1) / func2((float) x1);
} while (Math.abs(x2 - x1) > e);
}
if (func2((float) a) * func3((float) a) < 0) {
do {
x1 = a;
x1 = x2;
x2 = x1 - func1(x1) / func2((float) x1);
}
while (Math.abs(x2 - x1) > e);
}
System.out.println(x2);
} else
System.out.println("Bu oraliqda yechim yo'q.");
}
public static float func1(double x)
{
return (float) (5*x-8*Math.log(x) - 8);
}
public static float func2(float x)
{
return (float) (5-8/x);
}
public static float func3(float x)
{
return 8/x*x;
}
}
Vatarlar usuli
public class Misol1VatarlarUsuli {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 0;
System.out.print("[a, b] oraliqni kiritng: ");
double a = scanner.nextDouble(), b = scanner.nextDouble();
double x1, x2 = 0, e = 0.001;
if (func1(a) < 0 && func1(b) > 0) {
if (func2((float) a) * func3((float) a) < 0) {
do {
x1 = b;
x1 = x2;
x2 = x1 - func1(x1) * (x1 - a) / (func1(x1) - func1(a));
} while (Math.abs(x2 - x1) > e);
}
System.out.println(x2);
} else {
System.out.println("Bu oraliqda yechim yo'q.");
}
}
public static float func1(double x) {
return (float) (5*x-8*Math.log(x) - 8);
}
public static float func2(float x) {
return (float) (5-8/x);
}
public static float func3(float x) {
return (float) 8/(x*x);
}
}
Do'stlaringiz bilan baham: |