Главная страница Случайная страница КАТЕГОРИИ: АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника |
Пример выполнения работы. Приложение. Код программы ⇐ ПредыдущаяСтр 5 из 5
Код программы z1.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { float xn, xn1; float e = (float)0.0001; ofstream out (" z1.out"); out.precision (4); out.setf (ios:: fixed); xn1 = 0.; xn = acos (xn1*xn1)/3.141592653589;
float q = 2.*fabs(xn1-xn); int i = 0; while (q> =e) { out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < q < < endl; xn1 = xn; xn = acos (xn1*xn1)/3.141592653589; q = 2.*fabs(xn1-xn); i++; } out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < q < < endl;
out < < xn; return 0; }
Код программы z3.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { float x [8] = {(float)0.00, (float)0.08, (float)0.13, (float)0.20, (float)0.27, (float)0.31, (float)0.38, (float)0.44}; float y [8] = {(float)1.00000, (float)1.72539, (float)1.97664, (float)1.92950, (float)1.45474, (float)1.07307, (float)0.42687, (float)0.09338}; float xx = (float)0.10101918; float yy = (float)0.;
ofstream out (" z3.out"); out.precision (5); out.setf (ios:: fixed);
for (int i=1; i< 4; i++) { float p = 1.; for (int j=1; j< 4; j++) { if (j! =i) { p *= (xx-x[j])/(x[i]-x[j]); } } yy += p*y[i]; }
out < < yy < < endl;
//Оценка погрешности метода float om = 1; for (i=1; i< 4; i++) om*=xx-x[i];
float a = 0; float p = 1;
for (i=0; i< 4; i++) { for (int j=0; j< 4; j++) { if (j! =i) { p+= 1/(x[i]-x[j]); } } a+= p; } float r = fabs(a*om); out < < " Погрешность метода равна: " < < endl; out < < r < < endl;
float l =.75; float eps = (float).00001; float d = eps*l;
out < < " Погрешность вычислений равна: " < < endl; out < < d < < endl;
out < < " Суммарная погрешность равна: " < < endl; out < < r+d; return 0; }
Код программы z4.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { float e = (float)0.001; float x; float a = (float)0.8, b = (float)1.2; float c;
ofstream out (" z4.out"); out.precision (3); out.setf (ios:: fixed);
//Метод половинного деления out < < " Метод бисекций: " < < endl; int i=0; while (fabs (a-b) > = e) { out < < i < < ' ' < < a < < ' ' < < b < < ' ' < < fabs(a-b) < < endl; c = (a+b)/2; if ((tan (2*a) + 2*a)*(tan (2*c) + 2*c) < 0.) b = c; else a = c; i++; } out < < i < < ' ' < < a < < ' ' < < b < < ' ' < < fabs(a-b) < < endl; x = (a+b)/2;
out < < x < < endl;
//Метод Ньютона out < < " Метод Ньютона: " < < endl;
float xn, xn1 = (float)0.8; xn = xn1 - (tan (2.*xn1) + 2.*xn1)/(2./(cos (xn1*2)*cos (xn1*2))+2.);
i = 0; while (fabs (xn-xn1) > = e) { out < < i < < ' ' < < xn < < ' ' < < fabs (xn-xn1) < < endl; xn1 = xn; xn = xn1 - (tan (2.*xn1) + 2.*xn1)/(2./(cos (xn1*2)*cos (xn1*2))+2.); i++; } out < < i < < ' ' < < xn < < ' ' < < fabs (xn-xn1) < < endl;
out < < xn1 < < endl;
//Метод хорд out < < " Метод хорд: " < < endl; float xn2; xn = (float)0.8; xn1 = (float)1.2; xn2 = xn1 - (tan (2.*xn1) + 2.*xn1)*(xn1 - xn)/((tan (2.*xn1) + 2.*xn1)-(tan (2.*xn) + 2.*xn));
i = 0; while (fabs (xn2-xn1) > = e) { out < < i < < ' ' < < xn2 < < ' ' < < fabs (xn2-xn1) < < endl; xn = xn1; xn1 = xn2; xn2 = xn1 - (tan (2.*xn1) + 2.*xn1)*(xn1 - xn)/((tan (2.*xn1) + 2.*xn1)-(tan (2.*xn) + 2.*xn)); i++; } out < < i < < ' ' < < xn2 < < ' ' < < fabs (xn2-xn1)< < endl;
out < < xn2; return 0; }
Код программы z5.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { ofstream out (" z5.out"); out.precision (14); out.setf (ios:: fixed);
float em = 0.5; while (em + 1. > 1.) { em = em/2.; } em = em*2.;
float xx = (float)1.1; float h = (float).1; float y0 = (float)3.55975, y1 = (float)3.89537, y2 = (float)4.25216;
float f1 = (y2-y0)/(2.*h); float f2 = (y2-2.*y1+y0)/(h*h); float d1 = fabs (2.*y1/h*em*f1);
out < < " f1 = " < < f1 < < ' ' < < d1 < < endl;
float d2 = fabs (4.*y1/(h*h)*em*f2);
out < < " f2 = " < < f2 < < ' ' < < d2 < < endl; return 0; }
Код программы z6.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { ofstream out (" z6.out"); out.precision (4); out.setf (ios:: fixed);
float x0 = 0., h; float y [13]; float y1 [25]; h = (float)1./(float)12.;
out < < " Trap: " < < endl; for (int i=0; i< 13; i++) { y[i] = sqrt(x0)*cos(x0); x0 += h; } x0 = 0.; for (i=0; i< 25; i++) { y1[i] = sqrt(x0)*cos(x0); x0 += h/2; }
//Формула трапеций float I = 0., I2 = 0.; I = h*(y[0]/2 + y [12]/2); for (i=1; i< 12; i++) { I += h*y[i]; }
I2 = (h/2)*(y1[0]/2 + y1[24]/2); for (i=1; i< 24; i++) { I2 += (h/2)*y1[i]; }
out < < I < < ' ' < < I2 < < endl; float R = fabs (I2-I)/3; I = I2 - (I2-I)/3; out < < I < < ' ' < < R < < endl;
//Формула Симпсона I = 0.; out < < " Simp: " < < endl;
I = (h/3)*(y [0] + y [12]); for (i=1; i< 12; i++) { if (i%2) I += 4*(h/3)*y[i]; else I += 2*(h/3)*y[i]; }
I2 = 0.; I2 = (h/6)*(y1 [0] + y1 [24]); for (i=1; i< 24; i++) { if (i%2) I2 += 4*(h/6)*y1[i]; else I2 += 2*(h/6)*y1[i]; } out < < I < < ' ' < < I2 < < endl;
R = fabs (I2-I)/15; I = I2 - (I2-I)/15;
out < < I < < ' ' < < R < < endl;
//формула прямоугольников out < < " Rectangle: " < < endl; I = 0.; for (i=1; i< 12; i++) { I += h*(sqrt((float)i*h+h/2)*cos((float)i*h+h/2)); } I2 = 0.; for (i=1; i< 24; i++) { I2 += (h/2)*(sqrt((float)i*(h/2)+h/4)*cos((float)i*(h/2)+h/4)); } out < < I < < ' ' < < I2 < < endl; R = fabs (I2-I)/3; I = I2 + (I2-I)/3; out < < I < < ' ' < < R < < endl; return 0; }
Код программы z7.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
int main () { ofstream out (" z7.out"); out.precision (11); out.setf (ios:: fixed); double I = 0.; double t [4] = {-0.86113631, -0.33998104, 0.33998104, 0.86113631}; double A [4] = {0.34785484, 0.65214516, 0.65214516, 0.34785484};
for (int i=0; i< 4; i++) { I += A[i]*(1/(10+t[i])); } double R = (double)40320/(double)(9*9*9*9*9*9*9*9*9)/(double)3472875; out < < I < < ' ' < < R; return 0; }
Код программы z12.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
float f (float q) { return q*q - cos (3.141592653589*q); }
float f1 (float q) { return 2*q + 3.141592653589*sin(3.141592653589*q); }
int main () { ofstream out (" z12.out"); out.precision (4); out.setf (ios:: fixed);
out < < " Square: " < < endl; float x0 = (float)0.2, x1 = (float)0.4, x2 = (float)0.5; float y0 = f(x0), y1 = f(x1), y2 = f(x2); float x; float e = (float)0.0001;
x = (y0*y0*y1*x2+y2*y2*y0*x1+y1*y1*y2*x0-y2*y2*y1*x0-y0*y0*y2*x1-y1*y1*y0*x2)/(y0*y0*y1+y2*y2*y0+y1*y1*y2-y1*y2*y2-y2*y0*y0-y0*y1*y1);
int i = 0; while (fabs (x-x2) > = e) { out < < i < < ' ' < < x0 < < ' ' < < x1 < < ' ' < < x2 < < ' ' < < x < < endl; x0 = x1; x1 = x2; x2 = x; y0 = f(x0); y1 = f(x1); y2 = f(x2); x = (y0*y0*y1*x2+y2*y2*y0*x1+y1*y1*y2*x0-y2*y2*y1*x0-y0*y0*y2*x1-y1*y1*y0*x2)/(y0*y0*y1+y2*y2*y0+y1*y1*y2-y1*y2*y2-y2*y0*y0-y0*y1*y1); i++; } out < < i < < ' ' < < x0 < < ' ' < < x1 < < ' ' < < x2 < < ' ' < < x< < endl; out < < x < < endl;
out < < " Newton: " < < endl;
x0 = x1 = (float)0.2; x1 = x0 - f(x0)/f1(x0);
i=0; while (fabs (x0-x1) > = e) { out < < i < < ' ' < < x0 < < ' ' < < x1 < < endl; x0 = x1; x1 = x0 - f(x0)/f1(x0); i++; }
out < < i < < ' ' < < x0 < < ' ' < < x1 < < endl; out < < x1 < < endl;
return 0; }
. Код программы z13.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
float f(float q) { return (2.*q-1)*(q-4.)*(q-4.)*(q-5.)*(q-5.); }
float f1 (float q) { return 10.*q*q*q*q-148.*q*q*q+780.*q*q-1682.*q+1160.; }
int main () { ofstream out (" z13.out"); out.precision (19); out.setf (ios:: fixed);
float em = 0.5; while (em + 1. > 1.) { em = em/2.; } em = em*2.;
out < < " Epsilon = " < < em < < endl;
out.precision (16); float a[6] = {-400., 1160., -841., 260., -37., 2.}; //Для простого корня x = 1/2 out < < " x = 1/2" < < endl; float nu12 [6];
for (int i=0; i< 6; i++) { float p = 1.; for (int j=0; j< i; j++) p = p*.5;
nu12[i] = 2.*p*fabs(a[i])/(5.*a[5]*0.0625+4.*a[4]*0.125+3.*a[3]*0.25+a[2]+a[1]); out < < i < < ' ' < < nu12[i] < < endl; } float d1 = 0.; for (i=0; i< 6; i++) { d1 += fabs(nu12[i]*em); } float d = d1*.5; out < < " dx = " < < d < < endl;
//Для кратного корня x = 4 out < < " x = 4" < < endl; float a2[5] = {1160., -1682., 780., -148., 10.}; float nu4 [5];
for (i=0; i< 5; i++) { float p = 1.; for (int j=0; j< i; j++) p = p*4.;
nu4[i] = p*fabs(a2[i])/(4.*a2[4]*64.+3.*a[3]*16.+2.*a[2]*4.+a[1])/4.; out < < i < < ' ' < < nu4[i] < < endl; } d1 = 0.; for (i=0; i< 5; i++) { d1 += fabs(nu4[i]*2.*em); } d = d1*4.; out < < " dx = " < < d < < endl;
//Для кратного корня x = 5 out < < " x = 5" < < endl; float nu5 [5];
for (i=0; i< 5; i++) { float p = 1.; for (int j=0; j< i; j++) p = p*5.;
nu5[i] = p*fabs(a2[i])/(4.*a2[4]*125.+3.*a[3]*25.+2.*a[2]*5.+a[1])/5.; out < < i < < ' ' < < nu5[i] < < endl; } d1 = 0.; for (i=0; i< 5; i++) { d1 += fabs(nu5[i]*2.*em); } d = d1*5.; out < < " dx = " < < d < < endl;
//Найдем корни методом Ньютона out < < " Newton: " < < endl;
out < < " Простой корень: " < < endl; float xn1 = (float)0., xn; float e = em*.5; xn = xn1 - f(xn1)/f1(xn1);
i=0; while (fabs (xn-xn1) > = e) { out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl; xn1 = xn; xn = xn1 - f(xn1)/f1(xn1); i++; } out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl;
out < < " x1 = " < < xn1 < < endl;
out < < " Первый кратный корень: " < < endl; xn1 = (float)3.5; e = em*4.; xn = xn1 - f(xn1)/f1(xn1);
i=0; while (fabs (xn-xn1) > = e) { out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl; xn1 = xn; xn = xn1 - f(xn1)/f1(xn1); i++; } out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl;
out < < " x2, 3 = " < < xn1 < < ' ' < < 4-xn1< < endl;
out < < " Второй кратный корень: " < < endl; xn1 = (float)5.5; e = em*5.; xn = xn1 - f(xn1)/f1(xn1);
i=0; while (fabs (xn-xn1) > = e) { out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl; xn1 = xn; xn = xn1 - f(xn1)/f1(xn1); i++; } out < < i < < ' ' < < xn1 < < ' ' < < xn < < ' ' < < fabs (xn-xn1)/em < < endl;
out < < " x4, 5 = " < < xn1 < < ' ' < < 5.-xn1< < endl; return 0; }
. Код программы z14.cpp
#include < iostream> #include < fstream> #include < math.h>
using namespace std;
float f (float r) { return r*r - cos(3.141592653589*r); }
float f1 (float r) { return 2.*r + 3.141592653589*sin(3.141592653589*r); }
float f2 (float r) { return 2. + 3.141592653589*3.141592653589*cos(3.141592653589*r); }
int main () { ofstream out (" z14.out"); out.precision (18); out.setf (ios:: fixed);
float em = 0.5; while (em + 1. > 1.) { em = em/2.; } em = em*2.;
out < < " Epsilon = " < < em < < endl;
float em3 = (float)0.00000605545444;
out < < " k = -10" < < endl; float h = em3*.0000000001;
float x = (float)0.26; float d1, d2; float y1, y2, y21, y11;
out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = -9" < < endl; h = em3*.000000001; out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21); out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = -2" < < endl; h = em3*.01; out < < " h = " < < h < < endl; y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = -1" < < endl; h = em3*.1; out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21); out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = 0" < < endl; h = em3; out < < " h = " < < h < < endl; y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = 1" < < endl; h = em3*10.; out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21); out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = 2" < < endl; h = em3*100.; out < < " h = " < < h < < endl;;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl;
out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = 9" < < endl; h = em3*1000000000.; out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl;
out < < " k = 10" < < endl; h = em3*10000000000.; out < < " h = " < < h < < endl;
y1 = (f(x+h) - f (x-h))/(2.*h); y2 = (f(x+h)-2.*f(x)+f(x-h))/(h*h); y11 = f1 (x); y21 = f2 (x); d1 = fabs (y1 - y11); d2 = fabs (y2 - y21);
out < < y1 < < ' ' < < y11 < < ' ' < < y2 < < ' ' < < y21 < < endl; out < < " d1 = " < < d1 < < ' ' < < " d2 = " < < d2 < < endl; return 0; }
Литература 1. Амосов А.А., Дубинский Ю.А., Копченова Н.В. Вычислительные методы для инженеров. М., Высшая школа, 2004. – 548 с. 2. Бахвалов Н.С., Жидков Н.П., Кобельков Г.М. Численные методы. М., Бином, 2004. – 636 с. 3. Бахвалов Н.С., Лапин А.В., Чижонков Е.В. Численные методы в задачах и упражнениях. М., Высшая школа, 2000. -254 с. 4. В.М. Вербжицкий. Основы численных методов. М., Высшая школа, 2002. -847 с. 5. Волков Е.А. Численные методы. СПб., Лань, 2004. -244 с. 6.. Калиткин Н.Н. Численные методы. М., Наука, 1978. -246с.
7. Крылов В.И., Бобков В.В., Монастырский П.И. Вычислительные методы.. Наука,. 1976. – 300с..(есть др. издания) 8. Научно-практический журнал " Exponenta Pro. Математика в приложениях" https://rrc.dgu.ru/res/exponenta/educat/systemat/systemat.asp.htm 9. Пирумов У.Г. Численные методы. М., Дрофа, 2003. -224 с. 10. Турчак Л.И., Плотников П.В. Основы численных методов. М., Физматлит, 2002. -356 с.
|