МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
НУ”ЛЬВІВСЬКА ПОЛІТЕХНІКА”
КАФЕДРА АВТОМАТИКИ І ТЕЛЕМЕХАНІКИ
ЗВІТ
про виконання лабораторної роботи№4
з дисципліни „Алгоритмічні основи криптології”
на тему:
Чисельні методи розв’язування
Диференціальних рівнянь
Мета роботи: вивчити основні чисельні методи розв’язування систем лінійних диференціальних рівнянь.
Завдання: скласти програму для розв’язування диференціального рівняння другого порядку методом Рунге-Кутта.
варіант
диф.рівн.
поч.умови
проміжок
Крок інтегрув.
Похибка
4
y’’+4y=e3x(13x-7)
y(0)=0;y’(0)=0;
[0;0.2]
0.02
0.001
Блок-схема алгоритму (програма із сталим кроком інтегрування).
Блок-схема алгоритму (програма із змінним кроком інтегрування).
Текст програми (постійний крок).
#include<stdio.h>
#include<math.h>
#define a 0
#define b 0.2
#define e 0.001
float f (int i, float x, float y)
{if (i==1)
return y ; else
return(exp(3*x)*(13*x-7)-4*y);}
main()
{float k1,k2,k3,k4;int i,l; float h=0.002;
float yp[3];float ym1[3];float ym12[3],h1;
float x,t,t1,t2,t3,t4,res;
float poch[3];
poch[1]=yp[1]=-4;
poch[2]=yp[2]=0;
x=a;
do { t=x;
x+=h;
for (i=1;i<=2;i++)
{ k1=h*f(i,t,yp[i]);
k2=h*f(i,t+h/2,(yp[i])+k1/2);
k3=h*f(i,t+h/2,yp[i]+(k2/2));
k4=h*f(i,t+h,(yp[i])+k3);
if (i==1)
ym1[i]=yp[i+1]+(k1+2*k2+2*k3+k4)/6;else
ym1[i]=yp[i-1]+(k1+2*k2+2*k3+k4)/6;}
//-----------Pereprysvoennia1
for (i=1;i<=2;i++)
{if (i==1)
yp[i+1]=ym1[i]; else
yp[i-1]=ym1[i]; }
printf("x=%2.5f ",x);
for (i=1;i<=2;i++)
printf(" y%i=%2.5f ",i,ym1[i]);
putchar('\n');
} while (x<=b);
return 0; }
Результати обчислень.
x=0.00200 y1=-0.00801 y2=-4.01396
x=0.00400 y1=-0.01604 y2=-4.02789
x=0.00600 y1=-0.02411 y2=-4.04178
x=0.00800 y1=-0.03220 y2=-4.05565
x=0.01000 y1=-0.04032 y2=-4.06948
x=0.01200 y1=-0.04847 y2=-4.08327
x=0.01400 y1=-0.05664 y2=-4.09703
x=0.01600 y1=-0.06484 y2=-4.11076
x=0.01800 y1=-0.07307 y2=-4.12446
x=0.02000 y1=-0.08133 y2=-4.13812
x=0.02200 y1=-0.08961 y2=-4.15174
x=0.02400 y1=-0.09793 y2=-4.16533
x=0.02600 y1=-0.10627 y2=-4.17888
x=0.02800 y1=-0.11463 y2=-4.19240
x=0.03000 y1=-0.12302 y2=-4.20587
x=0.03200 y1=-0.13144 y2=-4.21932
x=0.03400 y1=-0.13989 y2=-4.23272
x=0.03600 y1=-0.14837 y2=-4.24609
x=0.03800 y1=-0.15687 y2=-4.25942
x=0.04000 y1=-0.16539 y2=-4.27271
x=0.04200 y1=-0.17395 y2=-4.28596
x=0.04400 y1=-0.18253 y2=-4.29917
x=0.04600 y1=-0.19114 y2=-4.31234
x=0.04800 y1=-0.19977 y2=-4.32547
x=0.05000 y1=-0.20843 y2=-4.33856
x=0.05200 y1=-0.21711 y2=-4.35161
x=0.05400 y1=-0.22583 y2=-4.36462
x=0.05600 y1=-0.23456 y2=-4.37758
x=0.05800 y1=-0.24333 y2=-4.39051
x=0.06000 y1=-0.25212 y2=-4.40339
x=0.06200 y1=-0.26093 y2=-4.41623
x=0.06400 y1=-0.26977 y2=-4.42902
x=0.06600 y1=-0.27864 y2=-4.44178
x=0.06800 y1=-0.28753 y2=-4.45448
x=0.07000 y1=-0.29645 y2=-4.46714
x=0.07200 y1=-0.30539 y2=-4.47976
x=0.07400 y1=-0.31436 y2=-4.49233
x=0.07600 y1=-0.32336 y2=-4.50486
x=0.07800 y1=-0.33238 y2=-4.51734
x=0.08000 y1=-0.34142 y2=-4.52977
x=0.08200 y1=-0.35049 y2=-4.54215
x=0.08400 y1=-0.35958 y2=-4.55449
x=0.08600 y1=-0.36870 y2=-4.56678
x=0.08800 y1=-0.37784 y2=-4.57902
x=0.09000 y1=-0.38701 y2=-4.59121
x=0.09200 y1=-0.39620 y2=-4.60335
x=0.09400 y1=-0.40542 y2=-4.61544
x=0.09600 y1=-0.41466 y2=-4.62748
x=0.09800 y1=-0.42392 y2=-4.63947
x=0.10000 y1=-0.43321 y2=-4.65141
x=0.10200 y1=-0.44252 y2=-4.66330
x=0.10400 y1=-0.45186 y2=-4.67513
x=0.10600 y1=-0.46122 y2=-4.68691
x=0.10800 y1=-0.47060 y2=-4.69864
x=0.11000 y1=-0.48001 y2=-4.71031
x=0.11200 y1=-0.48944 y2=-4.72193
x=0.11400 y1=-0.49889 y2=-4.73350
x=0.11600 y1=-0.50837 y2=-4.74500
x=0.11800 y1=-0.51787 y2=-4.75646
x=0.12000 y1=-0.52739 y2=-4.76785
x=0.12200 y1=-0.53693 y2=-4.77919
x=0.12400 y1=-0.54650 y2=-4.79048
x=0.12600 y1=-0.55609 y2=-4.80170
x=0.12800 y1=-0.56571 y2=-4.81287
x=0.13000 y1=-0.57534 y2=-4.82397
x=0.13200 y1=-0.58500 y2=-4.83502
x=0.13400 y1=-0.59468 y2=-4.84601
x=0.13600 y1=-0.60438 y2=-4.85693
x=0.13800 y1=-0.61410 y2=-4.86780
x=0.14000 y1=-0.62385 y2=-4.87860
x=0.14200 y1=-0.63362 y2=-4.88934
x=0.14400 y1=-0.64340 y2=-4.90002
x=0.14600 y1=-0.65321 y2=-4.91064
x=0.14800 y1=-0.66305 y2=-4.92119
x=0.15000 y1=-0.67290 y2=-4.93168
x=0.15200 y1=-0.68277 y2=-4.94210
x=0.15400 y1=-0.69267 y2=-4.95245
x=0.15600 y1=-0.70258 y2=-4.96274
x=0.15800 y1=-0.71252 y2=-4.97297
x=0.16000 y1=-0.72247 y2=-4.98312
x=0.16200 y1=-0.73245 y2=-4.99321
x=0.16400 y1=-0.74244 y2=-5.00323
x=0.16600 y1=-0.75246 y2=-5.01318
x=0.16800 y1=-0.76250 y2=-5.02306
x=0.17000 y1=-0.77255 y2=-5.03287
x=0.17200 y1=-0.78263 y2=-5.04261
x=0.17400 y1=-0.79272 y2=-5.05227
x=0.17600 y1=-0.80284 y2=-5.06187
x=0.17800 y1=-0.81297 y2=-5.07139
x=0.18000 y1=-0.82313 y2=-5.08084
x=0.18200 y1=-0.83330 y2=-5.09021
x=0.18400 y1=-0.84349 y2=-5.09951
x=0.18600 y1=-0.85370 y2=-5.10873
x=0.18800 y1=-0.86392 y2=-5.11788
x=0.19000 y1=-0.87417 y2=-5.12695
x=0.19200 y1=-0.88443 y2=-5.13594
x=0.19400 y1=-0.89472 y2=-5.14486
x=0.19600 y1=-0.90502 y2=-5.15370
x=0.19800 y1=-0.91533 y2=-5.16245
x=0.20000 y1=-0.92567 y2=-5.17113
Текст програми (змінний крок).
#include<stdio.h>
#include<math.h>
#define a 0
#define b 0.2
#define e 0.00001
float f (int i, float x, float y)
{ if (i==1)
return y ; else
return(exp(3*x)*(13*x-7)-4*y);}
main()
{float k1,k2,k3,k4;int i,l; float h=0.002;
float yp[3],ym1[3],ym12[3],h1;float x,t,t1,t2,t3,t4; float poch[3];
poch[1]=yp[1]=-4;
poch[2]=yp[2]=0;
x=a;
while (x<=b)
{t=x; t1=yp[1]; t2=yp[2];t3=poch[1]; t4=poch[2];
x+=h;
for (i=1;i<=2;i++)
{k1=h*f(i,t,yp[i]);
k2=h*f(i,t+h/2,(yp[i])+k1/2);
k3=h*f(i,t+h/2,yp[i]+(k2/2));
k4=h*f(i,t+h,(yp[i])+k3);
if (i==1)
ym1[i]=yp[i+1]+(k1+2*k2+2*k3+k4)/6;else
ym1[i]=yp[i-1]+(k1+2*k2+2*k3+k4)/6;}
//-----------Pereprysvoennia
for (i=1;i<=2;i++)
{if (i==1)
yp[i+1]=ym1[i]; else
yp[i-1]=ym1[i];}
//*--------- Pererahunok------------*//
h1=h/2;
for (l=1;l<=2;l++)
{t+=h/2;
for (i=1;i<=2;i++)
{k1=h1*f(i,t,poch[i]);
k2=h1*f(i,t+h1/2,poch[i]+k1/2);
k3=h1*f(i,t+h1/2,poch[i]+k2/2);
k4=h1*f(i,t+h1,poch[i]+k3);
if (i==1)
ym12[i]=poch[i+1]+(k1+2*k2+2*k3+k4)/6; else
ym12[i]=poch[i-1]+(k1+2*k2+2*k3+k4)/6; }
//*----------Pereprysvoennia2----------*//
for (i=1;i<=2;i++)
{if (i==1)
poch[i+1]=ym12[i]; else
poch[i-1]=ym12[i]; } }
/*-----------Perevirka umovy-----------------*/
if (fabs(ym12[1]-ym1[1])<e)
{printf("x=%2.5f ",x);
for (i=1;i<=2;i++)
printf(" y%i=%2.5f ",i,ym1[i]);
for (i=1;i<=2;i++)
printf("poh=%2.5f ", fabs(ym12[i]-ym1[i]));
putchar('\n');
h=2*h; /*Zmineno krok */ }
if (fabs(ym12[1]-ym1[1])>e)
{x=x-h;
h=h/2;
yp[1]=t1; yp[2]=t2;poch[1]=t3; poch[2]=t4;}
}
return 0; }
Результати обчислень :
x=0.11199 y1=-0.48964 y2=-4.72264 poh=0.00001 poh=0.00010
x=0.11207 y1=-0.49004 y2=-4.72312 poh=0.00001 poh=0.00010
x=0.11212 y1=-0.49026 y2=-4.72339 poh=0.00001 poh=0.00010
x=0.11217 y1=-0.49049 y2=-4.72368 poh=0.00001 poh=0.00010
x=0.11222 y1=-0.49075 y2=-4.72400 poh=0.00001 poh=0.00010
x=0.11228 y1=-0.49103 y2=-4.72434 poh=0.00001 poh=0.00010
x=0.11232 y1=-0.49119 y2=-4.72453 poh=0.00001 poh=0.00010
------------------------------------------------------------------------------------
x=0.11738 y1=-0.51520 y2=-4.75371 poh=0.00001 poh=0.00010
x=0.11765 y1=-0.51648 y2=-4.75525 poh=0.00001 poh=0.00010
x=0.11780 y1=-0.51718 y2=-4.75610 poh=0.00001 poh=0.00010
x=0.11784 y1=-0.51737 y2=-4.75633 poh=0.00001 poh=0.00010
x=0.11802 y1=-0.51821 y2=-4.75734 poh=0.00001 poh=0.00010
x=0.11840 y1=-0.52005 y2=-4.75954 poh=0.00001 poh=0.00011
x=0.11861 y1=-0.52105 y2=-4.76075 poh=0.00001 poh=0.00011
x=0.11884 y1=-0.52215 y2=-4.76206 poh=0.00001 poh=0.00011
x=0.11910 y1=-0.52336 y2=-4.76350 poh=0.00001 poh=0.00011
x=0.11911 y1=-0.52344 y2=-4.76360 poh=0.00001 poh=0.00011
x=0.11927 y1=-0.52416 y2=-4.76446 poh=0.00001 poh=0.00011
x=0.11935 y1=-0.52455 y2=-4.76493 poh=0.00001 poh=0.00011
x=0.11944 y1=-0.52498 y2=-4.76545 poh=0.00001 poh=0.00011
x=0.11954 y1=-0.52545 y2=-4.76601 poh=0.00001 poh=0.00011
x=0.11964 y1=-0.52597 y2=-4.76662 poh=0.00001 poh=0.00011
x=0.11967 y1=-0.52611 y2=-4.76679 poh=0.00001 poh=0.00011
x=0.11980 y1=-0.52673 y2=-4.76753 poh=0.00001 poh=0.00011
x=0.11987 y1=-0.52706 y2=-4.76793 poh=0.00001 poh=0.00011
x=0.12003 y1=-0.52780 y2=-4.76881 poh=0.00001 poh=0.00011
x=0.12020 y1=-0.52861 y2=-4.76977 poh=0.00001 poh=0.00011
x=0.12029 y1=-0.52905 y2=-4.77030 poh=0.00001 poh=0.00011
x=0.12049 y1=-0.53001 y2=-4.77145 poh=0.00001 poh=0.00011
x=0.12055 y1=-0.53028 y2=-4.77176 poh=0.00001 poh=0.00011
x=0.12079 y1=-0.53143 y2=-4.77314 poh=0.00001 poh=0.00011
x=0.12106 y1=-0.53270 y2=-4.77464 poh=0.00001 poh=0.00011
x=0.12120 y1=-0.53339 y2=-4.77546 poh=0.00001 poh=0.00011
x=0.12136 y1=-0.53415 y2=-4.77636 poh=0.00001 poh=0.00011
x=0.12153 y1=-0.53497 y2=-4.77734 poh=0.00001 poh=0.00011
x=0.12163 y1=-0.53543 y2=-4.77788 poh=0.00001 poh=0.00011
x=0.12173 y1=-0.53592 y2=-4.77846 poh=0.00001 poh=0.00011
x=0.12184 y1=-0.53646 y2=-4.77911 poh=0.00001 poh=0.00011
x=0.12191 y1=-0.53676 y2=-4.77946 poh=0.00001 poh=0.00011
x=0.12204 y1=-0.53741 y2=-4.78022 poh=0.00001 poh=0.00011
x=0.12208 y1=-0.53759 y2=-4.78043 poh=0.00001 poh=0.00011
x=0.12240 y1=-0.53914 y2=-4.78227 poh=0.00001 poh=0.00011
---------------------------------------------------------------------------------
x=0.12989 y1=-0.57510 y2=-4.82418 poh=0.00001 poh=0.00011
x=0.12993 y1=-0.57531 y2=-4.82442 poh=0.00001 poh=0.00011
x=0.13031 y1=-0.57712 y2=-4.82649 poh=0.00001 poh=0.00011
x=0.13051 y1=-0.57810 y2=-4.82762 poh=0.00001 poh=0.00011
x=0.13074 y1=-0.57918 y2=-4.82886 poh=0.00001 poh=0.00011
x=0.13077 y1=-0.57933 y2=-4.82903 poh=0.00001 poh=0.00011
x=0.13103 y1=-0.58062 y2=-4.83051 poh=0.00001 poh=0.00011
x=0.13162 y1=-0.58345 y2=-4.83374 poh=0.00001 poh=0.00011
x=0.13178 y1=-0.58423 y2=-4.83462 poh=0.00001 poh=0.00011
x=0.13196 y1=-0.58507 y2=-4.83559 poh=0.00001 poh=0.00011
x=0.13205 y1=-0.58554 y2=-4.83611 poh=0.00001 poh=0.00011
x=0.13226 y1=-0.58655 y2=-4.83727 poh=0.00001 poh=0.00011
x=0.13249 y1=-0.58766 y2=-4.83853 poh=0.00001 poh=0.00011
x=0.13274 y1=-0.58887 y2=-4.83991 poh=0.00001 poh=0.00011
x=0.13288 y1=-0.58954 y2=-4.84066 poh=0.00001 poh=0.00011
x=0.13295 y1=-0.58990 y2=-4.84107 poh=0.00001 poh=0.00011
x=0.13312 y1=-0.59069 y2=-4.84197 poh=0.00001 poh=0.00011
x=0.13330 y1=-0.59156 y2=-4.84296 poh=0.00001 poh=0.00011
x=0.13339 y1=-0.59204 y2=-4.84350 poh=0.00001 poh=0.00011
x=0.13350 y1=-0.59256 y2=-4.84409 poh=0.00001 poh=0.00011
x=0.13362 y1=-0.59312 y2=-4.84473 poh=0.00001 poh=0.00011
x=0.13388 y1=-0.59437 y2=-4.84614 poh=0.00001 poh=0.00011
x=0.13416 y1=-0.59573 y2=-4.84768 poh=0.00001 poh=0.00011
x=0.13446 y1=-0.59722 y2=-4.84936 poh=0.00001 poh=0.00011
x=0.13463 y1=-0.59803 y2=-4.85028 poh=0.00001 poh=0.00011
x=0.13472 y1=-0.59848 y2=-4.85078 poh=0.00001 poh=0.00011
x=0.13492 y1=-0.59945 y2=-4.85188 poh=0.00001 poh=0.00011
x=0.13503 y1=-0.59998 y2=-4.85248 poh=0.00001 poh=0.00011
x=0.13515 y1=-0.60057 y2=-4.85313 poh=0.00001 poh=0.00011
x=0.13529 y1=-0.60121 y2=-4.85385 poh=0.00001 poh=0.00011
x=0.13557 y1=-0.60260 y2=-4.85542 poh=0.00001 poh=0.00012
x=0.13573 y1=-0.60336 y2=-4.85628 poh=0.00001 poh=0.00012
x=0.13590 y1=-0.60420 y2=-4.85721 poh=0.00001 poh=0.00012
x=0.13595 y1=-0.60443 y2=-4.85747 poh=0.00001 poh=0.00012
x=0.13615 y1=-0.60543 y2=-4.85859 poh=0.00001 poh=0.00012
x=0.13627 y1=-0.60597 y2=-4.85920 poh=0.00001 poh=0.00012
x=0.13633 y1=-0.60627 y2=-4.85954 poh=0.00001 poh=0.00012
x=0.13660 y1=-0.60758 y2=-4.86100 poh=0.00001 poh=0.00012
x=0.13689 y1=-0.60901 y2=-4.86260 poh=0.00001 poh=0.00012
x=0.13705 y1=-0.60980 y2=-4.86348 poh=0.00001 poh=0.00011
x=0.13723 y1=-0.61065 y2=-4.86443 poh=0.00001 poh=0.00011
x=0.13742 y1=-0.61159 y2=-4.86548 poh=0.00001 poh=0.00011
x=0.13763 y1=-0.61261 y2=-4.86662 poh=0.00001 poh=0.00011
x=0.13786 y1=-0.61374 y2=-4.86787 poh=0.00001 poh=0.00011
----------------------------------------------------------------------------------------
x=0.14587 y1=-0.65289 y2=-4.91078 poh=0.00001 poh=0.00012
x=0.14590 y1=-0.65304 y2=-4.91094 poh=0.00001 poh=0.00012
x=0.14603 y1=-0.65369 y2=-4.91164 poh=0.00001 poh=0.00012
x=0.14618 y1=-0.65441 y2=-4.91241 poh=0.00001 poh=0.00012
x=0.14626 y1=-0.65480 y2=-4.91283 poh=0.00001 poh=0.00012
x=0.14643 y1=-0.65565 y2=-4.91375 poh=0.00001 poh=0.00012
x=0.14681 y1=-0.65752 y2=-4.91576 poh=0.00001 poh=0.00012
x=0.14692 y1=-0.65803 y2=-4.91630 poh=0.00001 poh=0.00012
----------------------------------------------------------------------------------------
x=0.15683 y1=-0.70702 y2=-4.96781 poh=0.00001 poh=0.00013
x=0.15700 y1=-0.70787 y2=-4.96869 poh=0.00001 poh=0.00013
x=0.15738 y1=-0.70974 y2=-4.97060 poh=0.00001 poh=0.00013
x=0.15748 y1=-0.71025 y2=-4.97113 poh=0.00001 poh=0.00013
x=0.15770 y1=-0.71137 y2=-4.97227 poh=0.00001 poh=0.00013
x=0.15795 y1=-0.71259 y2=-4.97352 poh=0.00001 poh=0.00013
x=0.15802 y1=-0.71292 y2=-4.97387 poh=0.00001 poh=0.00013
x=0.15816 y1=-0.71365 y2=-4.97461 poh=0.00001 poh=0.00013
x=0.15848 y1=-0.71525 y2=-4.97625 poh=0.00001 poh=0.00013
x=0.15866 y1=-0.71612 y2=-4.97714 poh=0.00001 poh=0.00013
x=0.15876 y1=-0.71660 y2=-4.97763 poh=0.00001 poh=0.00013
x=0.15897 y1=-0.71765 y2=-4.97870 poh=0.00001 poh=0.00013
x=0.15920 y1=-0.71879 y2=-4.97986 poh=0.00001 poh=0.00013
x=0.15926 y1=-0.71911 y2=-4.98018 poh=0.00001 poh=0.00013
x=0.15953 y1=-0.72048 y2=-4.98158 poh=0.00001 poh=0.00013
x=0.15968 y1=-0.72123 y2=-4.98234 poh=0.00001 poh=0.00013
x=0.15973 y1=-0.72143 y2=-4.98255 poh=0.00001 poh=0.00013
x=0.15982 y1=-0.72188 y2=-4.98300 poh=0.00001 poh=0.00013
x=0.16001 y1=-0.72286 y2=-4.98400 poh=0.00001 poh=0.00013
x=0.16023 y1=-0.72393 y2=-4.98509 poh=0.00001 poh=0.00013
x=0.16029 y1=-0.72423 y2=-4.98539 poh=0.00001 poh=0.00013
x=0.16042 y1=-0.72487 y2=-4.98604 poh=0.00001 poh=0.00013
x=0.16070 y1=-0.72627 y2=-4.98746 poh=0.00001 poh=0.00013
x=0.18041 y1=-0.82557 y2=-5.08361 poh=0.00001 poh=0.00048
x=0.19119 y1=-0.88067 y2=-5.13323 poh=0.00000 poh=0.00049
x=0.19709 y1=-0.91102 y2=-5.15940 poh=0.00000 poh=0.00048
x=0.20354 y1=-0.94440 y2=-5.18724 poh=0.00001 poh=0.00045
Висновок:
Розв’язання систем лінійних диференціальних рівнянь методом Ейлера, Рунге – Кута або методами із змінним кроком інтегрування можна досить легко реалізувати на ЕОМ за допомогою програми в середовищі Сі. Основна перевага використання методу із змінним кроком інтегрування полягає в існуванні можливості розв’язування систем диференціальних рівнянь із заданою похибкою. Розв’язання таких систем на ЕОМ є дуже актуальним, оскільки цей процес є набагато легшим та короткотривалішим ніж розв’язання систем лінійних диференціальних рівнянь вручну.