|
Fizik jarayonlarni kompyuterda modellashtirish
|
bet | 103/135 | Sana | 22.05.2024 | Hajmi | 15,08 Mb. | | #250347 |
Bog'liq Fizik jarayonlarni kompyuterda modellashtirishint main()
{
string user_pass;
cout << "Parolni kiriting: ";
getline (cin, user_pass);
check_pass (user_pass);
return 0;
}
Yana bitta misol keltiramiz:
1 #include
2 void function_name () {
3 cout << "Hello, world" << endl; }
4 int main() { function_name(); // Вызов функции
5 return 0;
6 }
Parol qiymatini qaytaruvchi funktsiyani parol misolida ko’ramiz:
1 #include
2 #include
3 using namespace std;
4 string check_pass (string password)
5{
6 string valid_pass = "qwerty123";
7 string error_message;
8 if (password == valid_pass)
9{
10 error_message = "Доступ разрешен.";
11}
12 else { error_message = "Неверный пароль!"; }
13 return error_message; }
14 int main()
15 {
16 string user_pass; cout << "Введите пароль: ";
17 getline (cin, user_pass);
18 string error_msg = check_pass (user_pass);
19 cout << error_msg << endl;
20 return 0;
21}
Rekursiydan foydalanib parolni tekshirish dasturi. Rekursiya-bu funktsiya o’zini o’zi chaqirishi.
1 include
2 #include
3 using namespace std;
4 bool password_is_valid
5 (string password)
6 {
7 string valid_pass = "qwerty123";
8 if (valid_pass == password)
9 return true; else return false;
10 }
11 void get_pass ()
12 {
13 string user_pass; cout << "Parolni kiriting: ";
14 getline(cin, user_pass);
15 if (!password_is_valid(user_pass))
16 {
17 cout << "Noto’gri parol!" << endl; get_pass (); // Bu yerda rekursiya qilamiz
18 }
19 else { cout << "Kirish hal etildi." << endl;
20 }
21}
22 int main()
23 {
24 get_pass ();
25 return 0;
26 }
Funktsiya va rekursiyalar dasturchi ishini osonlashtiradi.
Amaliy topshiriqlar:
Erkin tushish formulasi asosida dasturlarni yarating.
Jismlarni erkin tushishiga dasturlarni yarating.
Quyidagi dasturlarni kompyuterda bajaring:
Berilgan harakteristikalar bilan jismni tushishini turli qovushqoq muhitlarda modellashtiring (massa, shakl). Harakat jarayoniga qovishqoq muhitlarni ta’sirini o’rganing.Harakat tezligi katta bo’lmasligi kerak, bunda qarshilik kuchining kvadratik tashkil etuvchisini inobatga olmaslik mumkin bo’lsin.
Dasstur kodi:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include
#include
#include
#include
#define _USE_MATH_DEFINES
#include
using namespace std;
int main()
{
setlocale(LC_ALL,"O’zbek");
double s, c, r, m, Ftyazhest, v, my, k1, k2, Farchimed;
int Ptela, Psred;
double g = 9.8;
double t0 = 0;
double u0 = 0;
double a0 = 9.8;
double x0 = 1000;
double timer;
double th;
double u,x;
cout<<"Muhit zichligini kiriting: "; Psred = 960; //cin>>Psred;
cout<<"Jism zichligini toping: "; Ptela = 7750; //cin>>Ptela;
cout<<"Jism radiusini kiriting: "; r = 0.05; //cin>>r;
cout<<"Muhitning dinamk yopishqoqligini kiriting: ";
my = 0.987; //cin>>my;
cout<<"Jismni ixtiyoriy qarshilik koeffitsitentinin kiriting: "; c = 0.4; //cin>>c;
cout<<"Taymer qadami: "; th = 0.1; //cin>>th;
s = M_PI * pow(r,2);
cout<<"Ko’ndalang kesim zichligi: "<v = (4 * M_PI * pow(r,3))/3;
cout<<"Jism hajmi: "<m = Ptela * v;
cout<<"Jism massasi: "<Ftyazhest = m * g;
cout<<"Og’irlik kuchi: "<Farchimed = Psred * g * v;
cout<<"Arhimed kuchi: "<k1 = 6 * M_PI * r * my;
cout<<"Koeffitsient 1: "<k2 = (1 * c * s * Psred)/2;
cout<<" Koeffitsient 2: "<for (timer = 0; timer <=5; timer = timer + th)
{
x = x0 - (u0 + a0 * ((pow(th,2))/2));
cout<<"Masofani o’zgarishi: "<u = u0 + a0 * th;
cout<<"Tezlikni o’zgarishi: "<a0 = (Ftyazhest - k1 * u - k2 * u * u - Farchimed)/m;
cout<<"Tezlanishnio’zgarishi: "<}
system("pause");
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
3
|
Kodni bajaring:
|
1
2
3
4
5
6
7
8
9
10
11
|
for (timer = 0; timer <=5; timer = timer + th)
{
x = x0 - (u0 + a0 * ((pow(th,2))/2));
cout<<"Masofani o’zgartirish: "<u = u0 + a0 * th;
cout<<"ezlikni o’zgartirish: "<a0 = (Ftyazhest - k1 * u - k2 * u * u - Farchimed)/m;
cout<<"Tezlanishni o’zgartirish: "<}
|
|
|
Kodni bajaring:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
for (timer = 0; timer <=5; timer = timer + th)
{
x = x0 - (u0 + a0 * ((pow(th,2))/2));
cout<<"Masofani o’zgartirish: "<u = u0 + a0 * th;
cout<<"Tezlikni o’zgartirish: "<a0 = (Ftyazhest - k1 * u - k2 * u * u - Farchimed)/m;
cout<<"Tezlanishni o’zgartirish: "<x0=x;
u0=u;
}
|
|
|
|
Balandlikdan yer sirtiga toshni tushish vaqtini aniqlang.
Yechish algoritmi
Masala shartini avval keltiramiz:
Masala shartida boshlang’ich tezlik haqida hech narsa aytilmaganligi uchun, uni 0ga teng deb olamiz.
Natijani aniqligi haqida ham hech narsa aytilmaganligi uchun, biz aniq natijani hisoblashga harakat qilamiz, shunda masalamiz haqiqatga aniq bo’ladi. Javobni aniqlikda chiqarish kerak.
Balandlik va vaqt CI birligida berilishi talab etiladi.
Dastur kodi:
C++
1
2
3
4
6
7
8
9
10
11
12
13
|
//ML11.h balandlikdan tushish vaqti
#include
#include
#include //bu "setprecision" manipulyatorni qo’llash uchun ishlatiladi
using namespace std;
int main ()
{
double h;
cin>>h;
// setprecision manipulyator natijani aniqroq berishga
qo’llaniladi
cout< return 0;
}
|
Dasturni bajaring:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public partial class MainForm : Form
{
RectangleF r;
float vel;
SynchronizationContext sc;
public MainForm()
{
InitializeComponent();
r = new RectangleF(100,100,50,50);
sc = SynchronizationContext.Current;
vel = 0;
((AsyncCallback) delegate
{
while (true)
{
vel += (float)((9.8 * Math.Pow(0.2,2))/2.0);
r.Y+=vel;
if (r.Y>=750)
{
vel = 0;
r.Y=750;
}
Thread.Sleep(50);
sc.Post( delegate { Invalidate(); }, null);
}
}).BeginInvoke(null, null, null);
}
void MainFormPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Red, 0, 800, 800, 800);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillEllipse(Brushes.Black, r);
string s = string.Format("{0:0.000}", vel);
g.DrawString("Velocity:"+s,new Font("Arial",12), Brushes.Black, new PointF(400,100));
}
void MainFormKeyPress(object sender, KeyPressEventArgs e)
{
vel-=(float)0.6;
}
}
|
|
|
Kodlarni bajaring:
Burchak ostida otilgan jism harakati:
Masala sharti:
Jism gorizontga nisbatan alfa burchak ostida h balandlikdan v0 tezlik bilan tashlangan.quyidagilarni toppish kerak:
1) Yerdan H maksimal ko’tarish balandligini ;
2) t uchish vaqtini ;
3) s gorizontal uchish uzoqligini ;
4) Yerga nisbatan urilishdagi V tezligini.
(h =12,7 м; V0=27,4 м/с; 0 ≤ α ≤90 ).
Jism harakati modelini yaratish va tahlil qilish.. Imkoniyat darajasida vaqtga nisbatan tezlik va masofani bog’liqligini aniqlang.Mening yechimim:
Jism qanday masofaga ko’tarilishini aniqlaymiz:
Balandlikni toppish uchun ko’tarilish uchun ketgan vaqtni bilish kerak.Eng yuqori nuqtada tezlik 0 nolga teng bo’ladi.
=2.8 c
Pastga jism boshlang’ich tezliksiz м balandlik bilan tushadi Y’ani jismni ko’tarishning maksimal balandligi 51 m bo’ladi. Tushish vaqti formulasidan:
Demak, umumiy uchish vaqti: 2.8+3.2=6 c
Jism tushishi
Berilgan harakteristikalar bilan jismni tushishini turli qovushqoq muhitlarda modellashtiring (massa, shakl). Harakat jarayoniga qovishqoq muhitlarni ta’sirini o’rganing.Harakat tezligi katta bo’lmasligi kerak, bunda qarshilik kuchining kvadratik tashkil etuvchisini inobatga olmaslik mumkin bo’lsin. Endi Runge Kuta metodidan foydalanib, masalani yechamiz. Kodni bajaramiz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#include
#include
#include
#include
using namespace std;
double m=1;
double Fx(double t, double x, double y)
{
return y;
}
double Fy(double t, double x, double y)
{
return ( t*t )/m;
}
void RK4_2(int n, double * t, double * &x, double * &y)
{
double h=(t[n-1]-t[0])/n;
double kx1,kx2,kx3,kx4, ky1,ky2,ky3,ky4, Kx,Ky;
for(int i=1; i<=n; i++)
{
kx1=Fx(t[i-1],x[i-1],y[i-1]);
ky1=Fy(t[i-1],x[i-1],y[i-1]);
kx2=Fx(t[i-1]+0.5*h, x[i-1]+0.5*h*kx1, y[i-1]+0.5*h*ky1);
ky2=Fy(t[i-1]+0.5*h, x[i-1]+0.5*h*kx1, y[i-1]+0.5*h*ky1);
kx3=Fx(t[i-1]+0.5*h, x[i-1]+0.5*h*kx2, y[i-1]+0.5*h*ky2);
ky3=Fy(t[i-1]+0.5*h, x[i-1]+0.5*h*kx2, y[i-1]+0.5*h*ky2);
kx4=Fx(t[i-1]+h, x[i-1]+h*kx3, y[i-1]+h*ky3);
ky4=Fy(t[i-1]+h, x[i-1]+h*kx3, y[i-1]+h*ky3);
Kx= kx1/6 + 2*kx2/6 + 2*kx3/6 + kx4/6;
Ky= ky1/6 + 2*ky2/6 + 2*ky3/6 + ky4/6;
x[i]=x[i-1]+h*Kx;
y[i]=y[i-1]+h*Ky;
}
}
int main(int argc, char *argv[])
{
cout<<"rk4";
int n;
double h, t0,tn, x0,y0;
double *t, *x, *y;
cout<<"t0="; cin>>t0;
cout<<"tn="; cin>>tn;
cout<<"n="; cin>>n;
cout<<"x0="; cin>>x0;
cout<<"y0="; cin>>y0;
t= new double [n+1];
x= new double [n+1];
y= new double [n+1];
h=(tn-t0)/n;
for (int i=0; i<=n; i++) t[i]=t0+i*h;
x[0]=x0; y[0]=y0;
RK4_2(n, t, x, y);
ofstream fn("d:\\dataRK.txt");
for (int i=0; i<=n; i++) fn<fn.close();
cout<system("PAUSE");
return EXIT_SUCCESS;
}
|
|
|
|
| |