How Can I Model a Damped Pendulum Using Runge-Kutta in C++?

In summary, a Runge Kutta Pendulum is a mathematical model used to simulate the motion of a pendulum by approximating the solution of a differential equation. It works by dividing the motion into small time intervals and calculating the position and velocity at each interval. This model has advantages such as accuracy and efficiency, allowing for the simulation of complex systems. It is commonly used in scientific research to study pendulum behavior and make predictions in various fields. However, limitations include being a numerical approximation, requiring specific initial conditions, and potentially being inaccurate for chaotic or highly non-linear systems.
  • #1
brokenlynx
4
0
Im using a C++ class to try and model the motion of a simple pendulum using numerical analysis, at this stage of my implementation I am trying to produce oscillatory output values of theta and v denoting the displacement and velocity of the pendulum of a period of time. I have successfully managed to do so with values of v equal to 0 but am unable to gett the model working with an initial velocity causing increasing output.

Code:
#include <iomanip>
#include <math.h>
#include <fstream>
#include <tchar.h>
#include <iostream>

using namespace std;


class SinglePendulum {

	double theta0, L, T, m, g, n, y[2];
	double tau, I, alpha, theta;
	double delta_t_roof, delta_t;

	/*==================================================================*/
	/*  Variable list
	/*	theta0 - initial theta value for pendulum
	/*	L - length of pendulum rope
	/*	m - mass of pendulum bob (needed?, kept for extra function)
	/*	g - gravitational constant (currently 9.8)
	/*	delta_t(+ _roof) - time step size (may need amending)
	/*	n - number of steps
	/*	y[2] - input variables y[0] = v, y[1] = theta0					
	/*==================================================================*/

	/*==================================================================*/
	/*  Miscellaneous variables
	/*	tau, I, alpha (check for additional function)
	/*==================================================================*/

public: 

	void SinglePendulum::initialise() {
		theta0 = 1.4;
		L = 9;
		g = 9.8;
		delta_t_roof = 0.016;
		delta_t = 0.016;
		n = 600;
		m = 5;
		y[0] = 2;			// Initial velocity
		y[1] = theta0;		// Initial Angle
		

		//Clean up these comments (21/01/08)
		//T = 0;
		//delta_t_roof = sqrt(g/L)* delta_t;
		//initial angle
		
		cout<< "Pendulum Variables initialised \n";
		
	}


	void SinglePendulum::derivatives(double t, double* in, double* out) {
		out[0] = in[1];					//theta' = omega (1)
		out[1] = -g/L * sin(in[0]);		//omega' = -g/L * sin(theta) (2)
	}



	void SinglePendulum::rk2(){


		int i;
		double t_h;
		double yout[2], y_h[2], k1[2], k2[2], y_k[2];

		t_h=0;
		y_h[0]=y[0];			//theta'
		y_h[1]=y[1];			//omega'
		std::ofstream fout("rk2.out", std::ios::out);
		//ofstream fout("rk2.out");		
		fout.setf(ios::fixed);
		fout.precision(4);
		cout<<"";
		cout<< "Time\t\t y_h0\t\t y_h1\n";
		for(i=1; i<=n; i++){
			/*Calculation of k1 */
			derivatives(t_h, y_h, yout);
			k1[1]=yout[1]*delta_t_roof;
			k1[0]=yout[0]*delta_t_roof;
			y_k[0]=y_h[0]+k1[0]*0.5;
			y_k[1]=y_h[1]+k1[1]*0.5;


			derivatives(t_h+delta_t_roof*0.5, y_k, yout);
			k2[1]=yout[1]*delta_t_roof;
			k2[0]=yout[0]*delta_t_roof;
			yout[1]=y_h[1]+k2[1];
			yout[0]=y_h[0]+k2[0];
			fout<<t_h<<"\t\t"<<i*delta_t<<"\t\t"<<yout[0]<<"\t\t"<<yout[1]<<"\n";
			t_h+=delta_t_roof;
			y_h[0]=yout[0];
			y_h[1]=yout[1];
			cout<< t_h << "\t\t"<< y_h[0]<< "\t\t"<< y_h[1]<< "\t\t\n";
		}
		fout.close();
	}
};


int _tmain(int argc, _TCHAR* argv[]) {
	SinglePendulum pendulum;
	pendulum.initialise();
	pendulum.rk2();
	system("pause");
	return 0;
}

And here's the ouput (the first 2 columns denoting the time steps the last 2 are velocity and theta respectively.

Code:
0.0000		0.0160		2.0223		1.3842
0.0160		0.0320		2.0443		1.3686
0.0320		0.0480		2.0661		1.3532
0.0480		0.0640		2.0876		1.3380
0.0640		0.0800		2.1089		1.3229
0.0800		0.0960		2.1299		1.3081
0.0960		0.1120		2.1507		1.2934
0.1120		0.1280		2.1713		1.2789
0.1280		0.1440		2.1917		1.2647
0.1440		0.1600		2.2118		1.2506
0.1600		0.1760		2.2317		1.2367
0.1760		0.1920		2.2514		1.2231
0.1920		0.2080		2.2708		1.2096
0.2080		0.2240		2.2901		1.1964
0.2240		0.2400		2.3091		1.1834
0.2400		0.2560		2.3279		1.1707
0.2560		0.2720		2.3466		1.1581
0.2720		0.2880		2.3650		1.1458
0.2880		0.3040		2.3832		1.1337
0.3040		0.3200		2.4013		1.1218
0.3200		0.3360		2.4191		1.1102
0.3360		0.3520		2.4368		1.0988
0.3520		0.3680		2.4543		1.0876
0.3680		0.3840		2.4716		1.0767
0.3840		0.4000		2.4888		1.0660
0.4000		0.4160		2.5057		1.0555
0.4160		0.4320		2.5225		1.0453
0.4320		0.4480		2.5392		1.0353
0.4480		0.4640		2.5557		1.0255
0.4640		0.4800		2.5720		1.0160
0.4800		0.4960		2.5882		1.0068
0.4960		0.5120		2.6042		0.9977
0.5120		0.5280		2.6201		0.9889
0.5280		0.5440		2.6359		0.9804
0.5440		0.5600		2.6515		0.9720
0.5600		0.5760		2.6670		0.9640
0.5760		0.5920		2.6823		0.9561
0.5920		0.6080		2.6976		0.9485
0.6080		0.6240		2.7127		0.9411
0.6240		0.6400		2.7277		0.9340
0.6400		0.6560		2.7426		0.9271
0.6560		0.6720		2.7573		0.9205
0.6720		0.6880		2.7720		0.9141
0.6880		0.7040		2.7866		0.9079
0.7040		0.7200		2.8011		0.9020
0.7200		0.7360		2.8155		0.8963
0.7360		0.7520		2.8297		0.8908
0.7520		0.7680		2.8440		0.8856
0.7680		0.7840		2.8581		0.8806
0.7840		0.8000		2.8721		0.8758
0.8000		0.8160		2.8861		0.8713
0.8160		0.8320		2.9000		0.8670
0.8320		0.8480		2.9139		0.8630
0.8480		0.8640		2.9276		0.8592
0.8640		0.8800		2.9413		0.8556
0.8800		0.8960		2.9550		0.8522
0.8960		0.9120		2.9686		0.8491
0.9120		0.9280		2.9822		0.8462
0.9280		0.9440		2.9957		0.8436
0.9440		0.9600		3.0092		0.8412
0.9600		0.9760		3.0226		0.8390
0.9760		0.9920		3.0360		0.8370
0.9920		1.0080		3.0494		0.8353
1.0080		1.0240		3.0628		0.8338
1.0240		1.0400		3.0761		0.8326
1.0400		1.0560		3.0894		0.8315
1.0560		1.0720		3.1027		0.8307
1.0720		1.0880		3.1160		0.8302
1.0880		1.1040		3.1293		0.8298
1.1040		1.1200		3.1425		0.8297
1.1200		1.1360		3.1558		0.8299
1.1360		1.1520		3.1691		0.8302
1.1520		1.1680		3.1824		0.8308
1.1680		1.1840		3.1957		0.8317
1.1840		1.2000		3.2090		0.8327
1.2000		1.2160		3.2223		0.8340
1.2160		1.2320		3.2357		0.8355
1.2320		1.2480		3.2491		0.8373
1.2480		1.2640		3.2625		0.8393
1.2640		1.2800		3.2759		0.8415
1.2800		1.2960		3.2894		0.8439
1.2960		1.3120		3.3029		0.8466
1.3120		1.3280		3.3165		0.8495
1.3280		1.3440		3.3301		0.8527
1.3440		1.3600		3.3438		0.8561
1.3600		1.3760		3.3575		0.8597
1.3760		1.3920		3.3713		0.8635
1.3920		1.4080		3.3851		0.8676
1.4080		1.4240		3.3991		0.8719
1.4240		1.4400		3.4130		0.8765
1.4400		1.4560		3.4271		0.8813
1.4560		1.4720		3.4412		0.8863
1.4720		1.4880		3.4555		0.8916
1.4880		1.5040		3.4698		0.8971
1.5040		1.5200		3.4842		0.9028
1.5200		1.5360		3.4987		0.9088
1.5360		1.5520		3.5132		0.9150
1.5520		1.5680		3.5279		0.9214
1.5680		1.5840		3.5427		0.9281
1.5840		1.6000		3.5576		0.9350
1.6000		1.6160		3.5727		0.9422
1.6160		1.6320		3.5878		0.9496
1.6320		1.6480		3.6030		0.9572
1.6480		1.6640		3.6184		0.9651
1.6640		1.6800		3.6339		0.9732
1.6800		1.6960		3.6496		0.9816
1.6960		1.7120		3.6653		0.9902
1.7120		1.7280		3.6812		0.9990
1.7280		1.7440		3.6973		1.0081
1.7440		1.7600		3.7135		1.0174
1.7600		1.7760		3.7299		1.0269
1.7760		1.7920		3.7464		1.0367
1.7920		1.8080		3.7630		1.0467
1.8080		1.8240		3.7799		1.0570
1.8240		1.8400		3.7969		1.0675
1.8400		1.8560		3.8140		1.0782
1.8560		1.8720		3.8314		1.0892
1.8720		1.8880		3.8489		1.1004
1.8880		1.9040		3.8666		1.1118
1.9040		1.9200		3.8844		1.1235
1.9200		1.9360		3.9025		1.1354
1.9360		1.9520		3.9208		1.1475
1.9520		1.9680		3.9392		1.1599
1.9680		1.9840		3.9579		1.1725
1.9840		2.0000		3.9768		1.1853
2.0000		2.0160		3.9958		1.1983
2.0160		2.0320		4.0151		1.2115
2.0320		2.0480		4.0346		1.2250
2.0480		2.0640		4.0543		1.2387
2.0640		2.0800		4.0742		1.2526
2.0800		2.0960		4.0944		1.2667
2.0960		2.1120		4.1148		1.2810
2.1120		2.1280		4.1354		1.2955
2.1280		2.1440		4.1562		1.3102
2.1440		2.1600		4.1773		1.3251
2.1600		2.1760		4.1986		1.3401
2.1760		2.1920		4.2202		1.3554
2.1920		2.2080		4.2420		1.3708
2.2080		2.2240		4.2641		1.3865
2.2240		2.2400		4.2864		1.4022
2.2400		2.2560		4.3089		1.4182
2.2560		2.2720		4.3317		1.4343
2.2720		2.2880		4.3548		1.4505
2.2880		2.3040		4.3782		1.4669
2.3040		2.3200		4.4018		1.4835
2.3200		2.3360		4.4256		1.5001
2.3360		2.3520		4.4498		1.5169
2.3520		2.3680		4.4742		1.5338
2.3680		2.3840		4.4988		1.5507
2.3840		2.4000		4.5238		1.5678
2.4000		2.4160		4.5490		1.5850
2.4160		2.4320		4.5745		1.6022
2.4320		2.4480		4.6003		1.6195
2.4480		2.4640		4.6263		1.6368
2.4640		2.4800		4.6527		1.6542
2.4800		2.4960		4.6793		1.6716
2.4960		2.5120		4.7062		1.6890
2.5120		2.5280		4.7333		1.7064
2.5280		2.5440		4.7608		1.7238
2.5440		2.5600		4.7885		1.7412
2.5600		2.5760		4.8165		1.7586
2.5760		2.5920		4.8448		1.7759
2.5920		2.6080		4.8733		1.7931
2.6080		2.6240		4.9021		1.8103
2.6240		2.6400		4.9312		1.8273
2.6400		2.6560		4.9606		1.8443
2.6560		2.6720		4.9903		1.8611
2.6720		2.6880		5.0202		1.8778
2.6880		2.7040		5.0503		1.8943
2.7040		2.7200		5.0808		1.9107
2.7200		2.7360		5.1115		1.9268
2.7360		2.7520		5.1424		1.9428
2.7520		2.7680		5.1737		1.9585
2.7680		2.7840		5.2051		1.9740
2.7840		2.8000		5.2368		1.9892
2.8000		2.8160		5.2688		2.0041
2.8160		2.8320		5.3010		2.0188
2.8320		2.8480		5.3334		2.0331
2.8480		2.8640		5.3660		2.0471
2.8640		2.8800		5.3989		2.0607
2.8800		2.8960		5.4320		2.0740
2.8960		2.9120		5.4652		2.0869
2.9120		2.9280		5.4987		2.0995
2.9280		2.9440		5.5324		2.1116
2.9440		2.9600		5.5663		2.1232
2.9600		2.9760		5.6004		2.1345
2.9760		2.9920		5.6346		2.1452
2.9920		3.0080		5.6690		2.1555
3.0080		3.0240		5.7036		2.1653
3.0240		3.0400		5.7383		2.1746
3.0400		3.0560		5.7732		2.1833
3.0560		3.0720		5.8082		2.1916
3.0720		3.0880		5.8433		2.1993
3.0880		3.1040		5.8785		2.2064
3.1040		3.1200		5.9139		2.2130
3.1200		3.1360		5.9494		2.2190
3.1360		3.1520		5.9849		2.2244
3.1520		3.1680		6.0205		2.2292
3.1680		3.1840		6.0562		2.2335
3.1840		3.2000		6.0920		2.2371
3.2000		3.2160		6.1278		2.2401
3.2160		3.2320		6.1637		2.2425
3.2320		3.2480		6.1996		2.2442
3.2480		3.2640		6.2355		2.2454
3.2640		3.2800		6.2714		2.2459
3.2800		3.2960		6.3074		2.2458
3.2960		3.3120		6.3433		2.2450
3.3120		3.3280		6.3792		2.2437
3.3280		3.3440		6.4151		2.2417
3.3440		3.3600		6.4510		2.2391
3.3600		3.3760		6.4868		2.2359
3.3760		3.3920		6.5225		2.2321
3.3920		3.4080		6.5582		2.2276
3.4080		3.4240		6.5938		2.2226
3.4240		3.4400		6.6293		2.2170
3.4400		3.4560		6.6647		2.2108
3.4560		3.4720		6.7001		2.2040
3.4720		3.4880		6.7353		2.1967
3.4880		3.5040		6.7703		2.1888
3.5040		3.5200		6.8053		2.1804
3.5200		3.5360		6.8401		2.1714
3.5360		3.5520		6.8748		2.1620
3.5520		3.5680		6.9093		2.1520
3.5680		3.5840		6.9437		2.1415
3.5840		3.6000		6.9778		2.1306
3.6000		3.6160		7.0118		2.1192
3.6160		3.6320		7.0456		2.1074
3.6320		3.6480		7.0793		2.0952
3.6480		3.6640		7.1127		2.0825
3.6640		3.6800		7.1459		2.0695
3.6800		3.6960		7.1789		2.0561
3.6960		3.7120		7.2117		2.0423
3.7120		3.7280		7.2443		2.0282
3.7280		3.7440		7.2766		2.0137
3.7440		3.7600		7.3087		1.9990
3.7600		3.7760		7.3406		1.9839
3.7760		3.7920		7.3722		1.9686
3.7920		3.8080		7.4036		1.9531
3.8080		3.8240		7.4347		1.9373
3.8240		3.8400		7.4656		1.9212
3.8400		3.8560		7.4962		1.9050
3.8560		3.8720		7.5265		1.8886
3.8720		3.8880		7.5566		1.8720
3.8880		3.9040		7.5864		1.8553
3.9040		3.9200		7.6160		1.8384
3.9200		3.9360		7.6453		1.8214
3.9360		3.9520		7.6743		1.8043
3.9520		3.9680		7.7030		1.7871
3.9680		3.9840		7.7314		1.7699
3.9840		4.0000		7.7596		1.7526
4.0000		4.0160		7.7875		1.7352
4.0160		4.0320		7.8152		1.7178
4.0320		4.0480		7.8425		1.7004
4.0480		4.0640		7.8696		1.6830
4.0640		4.0800		7.8964		1.6655
4.0800		4.0960		7.9229		1.6482
4.0960		4.1120		7.9491		1.6308
4.1120		4.1280		7.9751		1.6135
4.1280		4.1440		8.0007		1.5962
4.1440		4.1600		8.0261		1.5790
4.1600		4.1760		8.0513		1.5619
4.1760		4.1920		8.0761		1.5448
4.1920		4.2080		8.1007		1.5279
4.2080		4.2240		8.1250		1.5110
4.2240		4.2400		8.1490		1.4943
4.2400		4.2560		8.1728		1.4777
4.2560		4.2720		8.1963		1.4612
4.2720		4.2880		8.2196		1.4449
4.2880		4.3040		8.2426		1.4287
4.3040		4.3200		8.2653		1.4126
4.3200		4.3360		8.2878		1.3968
4.3360		4.3520		8.3100		1.3810
4.3520		4.3680		8.3320		1.3655
4.3680		4.3840		8.3537		1.3501
4.3840		4.4000		8.3752		1.3349
4.4000		4.4160		8.3964		1.3199
4.4160		4.4320		8.4174		1.3051
4.4320		4.4480		8.4382		1.2904
4.4480		4.4640		8.4587		1.2760
4.4640		4.4800		8.4790		1.2618
4.4800		4.4960		8.4991		1.2477
4.4960		4.5120		8.5189		1.2339
4.5120		4.5280		8.5386		1.2203
4.5280		4.5440		8.5580		1.2069
4.5440		4.5600		8.5772		1.1938
4.5600		4.5760		8.5962		1.1808
4.5760		4.5920		8.6150		1.1681
4.5920		4.6080		8.6336		1.1556
4.6080		4.6240		8.6519		1.1433
4.6240		4.6400		8.6701		1.1312
4.6400		4.6560		8.6881		1.1194
4.6560		4.6720		8.7060		1.1078
4.6720		4.6880		8.7236		1.0965
4.6880		4.7040		8.7411		1.0854
4.7040		4.7200		8.7583		1.0745
4.7200		4.7360		8.7754		1.0638
4.7360		4.7520		8.7924		1.0534
4.7520		4.7680		8.8091		1.0432
4.7680		4.7840		8.8258		1.0333
4.7840		4.8000		8.8422		1.0236
4.8000		4.8160		8.8585		1.0141
4.8160		4.8320		8.8747		1.0049
4.8320		4.8480		8.8907		0.9959
4.8480		4.8640		8.9065		0.9871
4.8640		4.8800		8.9223		0.9786
4.8800		4.8960		8.9378		0.9704
4.8960		4.9120		8.9533		0.9623
4.9120		4.9280		8.9686		0.9545
4.9280		4.9440		8.9838		0.9470
4.9440		4.9600		8.9989		0.9397
4.9600		4.9760		9.0139		0.9326
4.9760		4.9920		9.0288		0.9258
4.9920		5.0080		9.0435		0.9192
5.0080		5.0240		9.0582		0.9128
5.0240		5.0400		9.0728		0.9067
5.0400		5.0560		9.0872		0.9008
5.0560		5.0720		9.1016		0.8951
5.0720		5.0880		9.1159		0.8897
5.0880		5.1040		9.1300		0.8845
5.1040		5.1200		9.1442		0.8796
5.1200		5.1360		9.1582		0.8749
5.1360		5.1520		9.1722		0.8704
5.1520		5.1680		9.1860		0.8662
5.1680		5.1840		9.1999		0.8622
5.1840		5.2000		9.2136		0.8584
5.2000		5.2160		9.2273		0.8549
5.2160		5.2320		9.2410		0.8516
5.2320		5.2480		9.2546		0.8485
5.2480		5.2640		9.2681		0.8457
5.2640		5.2800		9.2817		0.8431
5.2800		5.2960		9.2951		0.8407
5.2960		5.3120		9.3086		0.8386
5.3120		5.3280		9.3220		0.8367
5.3280		5.3440		9.3353		0.8350
5.3440		5.3600		9.3487		0.8335
5.3600		5.3760		9.3620		0.8323
5.3760		5.3920		9.3753		0.8314
5.3920		5.4080		9.3886		0.8306
5.4080		5.4240		9.4019		0.8301
5.4240		5.4400		9.4152		0.8298
5.4400		5.4560		9.4284		0.8298
5.4560		5.4720		9.4417		0.8299
5.4720		5.4880		9.4550		0.8304
5.4880		5.5040		9.4683		0.8310
5.5040		5.5200		9.4816		0.8319
5.5200		5.5360		9.4949		0.8330
5.5360		5.5520		9.5082		0.8343
5.5520		5.5680		9.5216		0.8359
5.5680		5.5840		9.5350		0.8377
5.5840		5.6000		9.5484		0.8397
5.6000		5.6160		9.5619		0.8420
5.6160		5.6320		9.5754		0.8445
5.6320		5.6480		9.5889		0.8472
5.6480		5.6640		9.6025		0.8502
5.6640		5.6800		9.6161		0.8534
5.6800		5.6960		9.6298		0.8568
5.6960		5.7120		9.6435		0.8605
5.7120		5.7280		9.6573		0.8644
5.7280		5.7440		9.6712		0.8685
5.7440		5.7600		9.6851		0.8729
5.7600		5.7760		9.6991		0.8775
5.7760		5.7920		9.7132		0.8823
5.7920		5.8080		9.7273		0.8874
5.8080		5.8240		9.7416		0.8927
5.8240		5.8400		9.7559		0.8982
5.8400		5.8560		9.7703		0.9040
5.8560		5.8720		9.7848		0.9100
5.8720		5.8880		9.7994		0.9163
5.8880		5.9040		9.8142		0.9228
5.9040		5.9200		9.8290		0.9295
5.9200		5.9360		9.8439		0.9365
5.9360		5.9520		9.8589		0.9437
5.9520		5.9680		9.8741		0.9511
5.9680		5.9840		9.8894		0.9588
5.9840		6.0000		9.9048		0.9667
6.0000		6.0160		9.9203		0.9749
6.0160		6.0320		9.9360		0.9833
6.0320		6.0480		9.9518		0.9920
6.0480		6.0640		9.9677		1.0008
6.0640		6.0800		9.9838		1.0100
6.0800		6.0960		10.0000		1.0193
6.0960		6.1120		10.0164		1.0289
6.1120		6.1280		10.0330		1.0387
6.1280		6.1440		10.0497		1.0488
6.1440		6.1600		10.0665		1.0591
6.1600		6.1760		10.0836		1.0697
6.1760		6.1920		10.1008		1.0805
6.1920		6.2080		10.1181		1.0915
6.2080		6.2240		10.1357		1.1027
6.2240		6.2400		10.1534		1.1142
6.2400		6.2560		10.1713		1.1259
6.2560		6.2720		10.1894		1.1379
6.2720		6.2880		10.2077		1.1501
6.2880		6.3040		10.2262		1.1625
6.3040		6.3200		10.2449		1.1751
6.3200		6.3360		10.2638		1.1879
6.3360		6.3520		10.2830		1.2010
6.3520		6.3680		10.3023		1.2143
6.3680		6.3840		10.3218		1.2278
6.3840		6.4000		10.3416		1.2415
6.4000		6.4160		10.3615		1.2555
6.4160		6.4320		10.3817		1.2696
6.4320		6.4480		10.4022		1.2839
6.4480		6.4640		10.4228		1.2985
6.4640		6.4800		10.4437		1.3132
6.4800		6.4960		10.4649		1.3282
6.4960		6.5120		10.4862		1.3433
6.5120		6.5280		10.5078		1.3586
6.5280		6.5440		10.5297		1.3741
6.5440		6.5600		10.5518		1.3897
6.5600		6.5760		10.5742		1.4055
6.5760		6.5920		10.5968		1.4215
6.5920		6.6080		10.6197		1.4376
6.6080		6.6240		10.6428		1.4539
6.6240		6.6400		10.6662		1.4703
6.6400		6.6560		10.6898		1.4869
6.6560		6.6720		10.7138		1.5035
6.6720		6.6880		10.7380		1.5203
6.6880		6.7040		10.7624		1.5372
6.7040		6.7200		10.7871		1.5542
6.7200		6.7360		10.8121		1.5713
6.7360		6.7520		10.8374		1.5885
6.7520		6.7680		10.8630		1.6057
6.7680		6.7840		10.8888		1.6230
6.7840		6.8000		10.9149		1.6404
6.8000		6.8160		10.9413		1.6578
6.8160		6.8320		10.9680		1.6752
6.8320		6.8480		10.9949		1.6926
6.8480		6.8640		11.0221		1.7100
6.8640		6.8800		11.0496		1.7274
6.8800		6.8960		11.0774		1.7448
6.8960		6.9120		11.1055		1.7621
6.9120		6.9280		11.1338		1.7794
6.9280		6.9440		11.1624		1.7967
6.9440		6.9600		11.1913		1.8138
6.9600		6.9760		11.2204		1.8308
6.9760		6.9920		11.2499		1.8478
6.9920		7.0080		11.2796		1.8645
7.0080		7.0240		11.3095		1.8812
7.0240		7.0400		11.3398		1.8977
7.0400		7.0560		11.3703		1.9140
7.0560		7.0720		11.4010		1.9301
7.0720		7.0880		11.4320		1.9460
7.0880		7.1040		11.4633		1.9617
7.1040		7.1200		11.4948		1.9771
7.1200		7.1360		11.5266		1.9923
7.1360		7.1520		11.5586		2.0071
7.1520		7.1680		11.5908		2.0217
7.1680		7.1840		11.6232		2.0360
7.1840		7.2000		11.6559		2.0499
7.2000		7.2160		11.6888		2.0635
7.2160		7.2320		11.7220		2.0767
7.2320		7.2480		11.7553		2.0896
7.2480		7.2640		11.7888		2.1020
7.2640		7.2800		11.8226		2.1140
7.2800		7.2960		11.8565		2.1256
7.2960		7.3120		11.8906		2.1367
7.3120		7.3280		11.9249		2.1474
7.3280		7.3440		11.9593		2.1576
7.3440		7.3600		11.9939		2.1672
7.3600		7.3760		12.0287		2.1764
7.3760		7.3920		12.0635		2.1851
7.3920		7.4080		12.0986		2.1932
7.4080		7.4240		12.1337		2.2008
7.4240		7.4400		12.1690		2.2078
7.4400		7.4560		12.2044		2.2143
7.4560		7.4720		12.2399		2.2202
7.4720		7.4880		12.2754		2.2255
7.4880		7.5040		12.3111		2.2302
7.5040		7.5200		12.3468		2.2343
7.5200		7.5360		12.3826		2.2377
7.5360		7.5520		12.4184		2.2406
7.5520		7.5680		12.4543		2.2429
7.5680		7.5840		12.4902		2.2445
7.5840		7.6000		12.5261		2.2455
7.6000		7.6160		12.5620		2.2459
7.6160		7.6320		12.5980		2.2457
7.6320		7.6480		12.6339		2.2448
7.6480		7.6640		12.6698		2.2433
7.6640		7.6800		12.7057		2.2412
7.6800		7.6960		12.7415		2.2385
7.6960		7.7120		12.7773		2.2352
7.7120		7.7280		12.8130		2.2312
7.7280		7.7440		12.8487		2.2266
7.7440		7.7600		12.8843		2.2215
7.7600		7.7760		12.9198		2.2158
7.7760		7.7920		12.9552		2.2094
7.7920		7.8080		12.9905		2.2025
7.8080		7.8240		13.0257		2.1951
7.8240		7.8400		13.0607		2.1871
7.8400		7.8560		13.0957		2.1786
7.8560		7.8720		13.1305		2.1695
7.8720		7.8880		13.1651		2.1599
7.8880		7.9040		13.1996		2.1499
7.9040		7.9200		13.2339		2.1393
7.9200		7.9360		13.2680		2.1283
7.9360		7.9520		13.3020		2.1168
7.9520		7.9680		13.3358		2.1049
7.9680		7.9840		13.3693		2.0926
7.9840		8.0000		13.4027		2.0799
8.0000		8.0160		13.4359		2.0667
8.0160		8.0320		13.4689		2.0532
8.0320		8.0480		13.5016		2.0394
8.0480		8.0640		13.5341		2.0252
8.0640		8.0800		13.5664		2.0107
8.0800		8.0960		13.5985		1.9959
8.0960		8.1120		13.6303		1.9808
8.1120		8.1280		13.6619		1.9654
8.1280		8.1440		13.6932		1.9498
8.1440		8.1600		13.7243		1.9340
8.1600		8.1760		13.7551		1.9179
8.1760		8.1920		13.7856		1.9016
8.1920		8.2080		13.8159		1.8852

Any help with this would be appreciated, and if any extra info is needed please ask.
 
Physics news on Phys.org
  • #2
You may think about posting a formulaic (functional) representation of your problem to help elicit responses, especially if you think that's where the problem is (rather than the code itself).
 
  • #3
Initial Conditions for the pendulum:

theta0 = 1.4; // Just under 90 degrees apprx.
L = 3;
g = 9.8;
delta_t_roof = 0.016;
delta_t = 0.016;
n = 600;
m = 5;
y[0] = 0; // Initial velocity
y[1] = theta0; // Initial angular displacement (radians)


The derivatives method:
Code:
void SinglePendulum::derivatives(double t, double* in, double* out) {
		out[0] = in[1];					//theta' = omega (1)
		out[1] = -g/L * sin(in[0]);		//omega' = -g/L * sin(theta) (2)
	}

The Runge-Kutta method implementation:
Code:
void SinglePendulum::rk2(){
		/*We are using the second-order-Runge-Kutta-algorithm
		* We have to calculate the parameters k1 and k2 for omega and theta,
		* so we use to arrays k1[2] and k2[2] for this
		* k1[0], k2[0] are the parameters for theta,
		* k1[1], k2[1] are the parameters for omega
		*/


		int i;
		double t_h;
		double yout[2], y_h[2], k1[2], k2[2], y_k[2];

		t_h=0;
		y_h[0]=y[0];			//theta'
		y_h[1]=y[1];			//omega'
		std::ofstream fout("rk2.out", std::ios::out);
		//ofstream fout("rk2.out");		PREVENT ERROR: C2374 (multiple initialisation)
		fout.setf(ios::fixed);
		fout.precision(4);
		cout<<"";
		cout<< "Time\t\t y_h0\t\t y_h1\n";
		for(i=1; i<=n; i++){
			/*Calculation of k1 */
			derivatives(t_h, y_h, yout);
			k1[1]=yout[1]*delta_t_roof;
			k1[0]=yout[0]*delta_t_roof;
			y_k[0]=y_h[0]+k1[0]*0.5;
			y_k[1]=y_h[1]+k1[1]*0.5;

			/*Calculation of k2 */
			derivatives(t_h+delta_t_roof*0.5, y_k, yout);
			k2[1]=yout[1]*delta_t_roof;
			k2[0]=yout[0]*delta_t_roof;
			yout[1]=y_h[1]+k2[1];
			yout[0]=y_h[0]+k2[0];
			fout<<t_h<<"\t\t"<<i*delta_t<<"\t\t"<<yout[0]<<"\t\t"<<yout[1]<<"\n";
			t_h+=delta_t_roof;
			y_h[0]=yout[0];
			y_h[1]=yout[1];
			cout<< t_h << "\t\t"<< y_h[0]<< "\t\t"<< y_h[1]<< "\t\t\n";
		}
		fout.close();
	}
};

...and the output:
Columns: t (time); t+1 (step+1); velocity; angular displacement
Code:
0.0000		0.0160		0.0224		1.3994
0.0160		0.0320		0.0448		1.3977
0.0320		0.0480		0.0671		1.3947
0.0480		0.0640		0.0894		1.3906
0.0640		0.0800		0.1116		1.3854
0.0800		0.0960		0.1337		1.3790
0.0960		0.1120		0.1558		1.3715
0.1120		0.1280		0.1776		1.3628
0.1280		0.1440		0.1994		1.3530
0.1440		0.1600		0.2209		1.3421
0.1600		0.1760		0.2423		1.3301
0.1760		0.1920		0.2635		1.3170
0.1920		0.2080		0.2845		1.3029
0.2080		0.2240		0.3052		1.2877
0.2240		0.2400		0.3257		1.2715
0.2400		0.2560		0.3459		1.2542
0.2560		0.2720		0.3658		1.2360
0.2720		0.2880		0.3854		1.2168
0.2880		0.3040		0.4047		1.1967
0.3040		0.3200		0.4237		1.1757
0.3200		0.3360		0.4424		1.1537
0.3360		0.3520		0.4606		1.1309
0.3520		0.3680		0.4785		1.1073
0.3680		0.3840		0.4961		1.0828
0.3840		0.4000		0.5132		1.0575
0.4000		0.4160		0.5299		1.0315
0.4160		0.4320		0.5462		1.0047
0.4320		0.4480		0.5621		0.9772
0.4480		0.4640		0.5775		0.9490
0.4640		0.4800		0.5924		0.9201
0.4800		0.4960		0.6069		0.8906
0.4960		0.5120		0.6209		0.8605
0.5120		0.5280		0.6345		0.8298
0.5280		0.5440		0.6475		0.7985
0.5440		0.5600		0.6600		0.7668
0.5600		0.5760		0.6720		0.7345
0.5760		0.5920		0.6835		0.7017
0.5920		0.6080		0.6945		0.6684
0.6080		0.6240		0.7049		0.6348
0.6240		0.6400		0.7148		0.6007
0.6400		0.6560		0.7241		0.5663
0.6560		0.6720		0.7329		0.5315
0.6720		0.6880		0.7411		0.4963
0.6880		0.7040		0.7488		0.4609
0.7040		0.7200		0.7559		0.4252
0.7200		0.7360		0.7624		0.3892
0.7360		0.7520		0.7683		0.3530
0.7520		0.7680		0.7737		0.3165
0.7680		0.7840		0.7785		0.2799
0.7840		0.8000		0.7826		0.2431
0.8000		0.8160		0.7862		0.2062
0.8160		0.8320		0.7892		0.1692
0.8320		0.8480		0.7917		0.1320
0.8480		0.8640		0.7935		0.0948
0.8640		0.8800		0.7947		0.0575
0.8800		0.8960		0.7953		0.0202
0.8960		0.9120		0.7953		-0.0171
0.9120		0.9280		0.7948		-0.0545
0.9280		0.9440		0.7936		-0.0918
0.9440		0.9600		0.7918		-0.1290
0.9600		0.9760		0.7895		-0.1661
0.9760		0.9920		0.7865		-0.2032
0.9920		1.0080		0.7830		-0.2401
1.0080		1.0240		0.7788		-0.2769
1.0240		1.0400		0.7741		-0.3136
1.0400		1.0560		0.7688		-0.3500
1.0560		1.0720		0.7629		-0.3862
1.0720		1.0880		0.7564		-0.4222
1.0880		1.1040		0.7494		-0.4580
1.1040		1.1200		0.7418		-0.4935
1.1200		1.1360		0.7336		-0.5286
1.1360		1.1520		0.7249		-0.5634
1.1520		1.1680		0.7156		-0.5979
1.1680		1.1840		0.7057		-0.6320
1.1840		1.2000		0.6953		-0.6657
1.2000		1.2160		0.6844		-0.6990
1.2160		1.2320		0.6730		-0.7318
1.2320		1.2480		0.6610		-0.7641
1.2480		1.2640		0.6485		-0.7960
1.2640		1.2800		0.6355		-0.8273
1.2800		1.2960		0.6221		-0.8580
1.2960		1.3120		0.6081		-0.8882
1.3120		1.3280		0.5936		-0.9177
1.3280		1.3440		0.5787		-0.9467
1.3440		1.3600		0.5633		-0.9749
1.3600		1.3760		0.5475		-1.0025
1.3760		1.3920		0.5313		-1.0293
1.3920		1.4080		0.5146		-1.0554
1.4080		1.4240		0.4975		-1.0808
1.4240		1.4400		0.4800		-1.1053
1.4400		1.4560		0.4621		-1.1290
1.4560		1.4720		0.4439		-1.1519
1.4720		1.4880		0.4252		-1.1739
1.4880		1.5040		0.4063		-1.1951
1.5040		1.5200		0.3870		-1.2152
1.5200		1.5360		0.3674		-1.2345
1.5360		1.5520		0.3475		-1.2528
1.5520		1.5680		0.3273		-1.2701
1.5680		1.5840		0.3069		-1.2864
1.5840		1.6000		0.2862		-1.3017
1.6000		1.6160		0.2652		-1.3159
1.6160		1.6320		0.2440		-1.3291
1.6320		1.6480		0.2227		-1.3412
1.6480		1.6640		0.2011		-1.3522
1.6640		1.6800		0.1794		-1.3620
1.6800		1.6960		0.1575		-1.3708
1.6960		1.7120		0.1355		-1.3784
1.7120		1.7280		0.1134		-1.3849
1.7280		1.7440		0.0912		-1.3903
1.7440		1.7600		0.0689		-1.3945
1.7600		1.7760		0.0466		-1.3975
1.7760		1.7920		0.0242		-1.3993
1.7920		1.8080		0.0018		-1.4000
1.8080		1.8240		-0.0206		-1.3995
1.8240		1.8400		-0.0430		-1.3979
1.8400		1.8560		-0.0653		-1.3950
1.8560		1.8720		-0.0876		-1.3910
1.8720		1.8880		-0.1098		-1.3859
1.8880		1.9040		-0.1320		-1.3796
1.9040		1.9200		-0.1540		-1.3721
1.9200		1.9360		-0.1759		-1.3635
1.9360		1.9520		-0.1976		-1.3538
1.9520		1.9680		-0.2192		-1.3430
1.9680		1.9840		-0.2406		-1.3311
1.9840		2.0000		-0.2618		-1.3181
2.0000		2.0160		-0.2828		-1.3041
2.0160		2.0320		-0.3035		-1.2890
2.0320		2.0480		-0.3240		-1.2728
2.0480		2.0640		-0.3442		-1.2557
2.0640		2.0800		-0.3642		-1.2375
2.0800		2.0960		-0.3838		-1.2185
2.0960		2.1120		-0.4032		-1.1984
2.1120		2.1280		-0.4222		-1.1774
2.1280		2.1440		-0.4409		-1.1556
2.1440		2.1600		-0.4592		-1.1328
2.1600		2.1760		-0.4771		-1.1092
2.1760		2.1920		-0.4947		-1.0848
2.1920		2.2080		-0.5118		-1.0596
2.2080		2.2240		-0.5286		-1.0336
2.2240		2.2400		-0.5449		-1.0069
2.2400		2.2560		-0.5608		-0.9795
2.2560		2.2720		-0.5762		-0.9513
2.2720		2.2880		-0.5912		-0.9225
2.2880		2.3040		-0.6058		-0.8931
2.3040		2.3200		-0.6198		-0.8630
2.3200		2.3360		-0.6334		-0.8323
2.3360		2.3520		-0.6464		-0.8011
2.3520		2.3680		-0.6590		-0.7694
2.3680		2.3840		-0.6711		-0.7371
2.3840		2.4000		-0.6826		-0.7044
2.4000		2.4160		-0.6936		-0.6712
2.4160		2.4320		-0.7041		-0.6375
2.4320		2.4480		-0.7140		-0.6035
2.4480		2.4640		-0.7234		-0.5691
2.4640		2.4800		-0.7322		-0.5343
2.4800		2.4960		-0.7405		-0.4992
2.4960		2.5120		-0.7482		-0.4638
2.5120		2.5280		-0.7553		-0.4281
2.5280		2.5440		-0.7619		-0.3921
2.5440		2.5600		-0.7679		-0.3559
2.5600		2.5760		-0.7733		-0.3195
2.5760		2.5920		-0.7781		-0.2829
2.5920		2.6080		-0.7823		-0.2461
2.6080		2.6240		-0.7860		-0.2092
2.6240		2.6400		-0.7890		-0.1722
2.6400		2.6560		-0.7915		-0.1350
2.6560		2.6720		-0.7934		-0.0978
2.6720		2.6880		-0.7946		-0.0605
2.6880		2.7040		-0.7953		-0.0232
2.7040		2.7200		-0.7954		0.0141
2.7200		2.7360		-0.7948		0.0514
2.7360		2.7520		-0.7937		0.0887
2.7520		2.7680		-0.7920		0.1260
2.7680		2.7840		-0.7897		0.1631
2.7840		2.8000		-0.7868		0.2002
2.8000		2.8160		-0.7833		0.2371
2.8160		2.8320		-0.7792		0.2739
2.8320		2.8480		-0.7745		0.3106
2.8480		2.8640		-0.7693		0.3471
2.8640		2.8800		-0.7634		0.3833
2.8800		2.8960		-0.7570		0.4193
2.8960		2.9120		-0.7500		0.4551
2.9120		2.9280		-0.7424		0.4906
2.9280		2.9440		-0.7343		0.5258
2.9440		2.9600		-0.7256		0.5606
2.9600		2.9760		-0.7164		0.5951
2.9760		2.9920		-0.7066		0.6293
2.9920		3.0080		-0.6962		0.6630
3.0080		3.0240		-0.6853		0.6963
3.0240		3.0400		-0.6739		0.7292
3.0400		3.0560		-0.6620		0.7615
3.0560		3.0720		-0.6496		0.7934
3.0720		3.0880		-0.6366		0.8248
3.0880		3.1040		-0.6232		0.8556
3.1040		3.1200		-0.6092		0.8858
 
  • #4
Ok basically the output above appears to be oscillatory the pendulum swinging between 1.4rad and -1.4rad. My problem comes when i attempt to apply an initial velocity (y[0] = 2) in this case the velocity only increases during the output (as in the first example output), would anyone be able to help with my implementation of the runge-kutta method or the derivatives method to try and find the root of this problem.
 
  • #5
Suggestions:
1) Is the total energy (= sum of potential and kinetic energy) conserved at each point? You can easily calculate this from the value of theta, the mass of the pendulum and the velocity at each point. Print this out and check that it is conserved. You might get a clue as to what is happening.
2) What happens if you take the 15th point, say, and start there, integrating backwards.
Do you retrace your steps, as you should?
 
  • #6
i have a problem in mathematical equation about the initial condition, what methode should used if there is an addition force, such as damping force. This equation will be applied in oscillatory system in fortran language. considering that i still learn about the fortran language , i need the example of how make the programme about my problem, thanks before.
 
Last edited:

1. What is a Runge Kutta Pendulum?

A Runge Kutta Pendulum is a mathematical model used to simulate the motion of a pendulum. It is a numerical method that approximates the solution of a differential equation, allowing for the prediction of the pendulum's position at any given time.

2. How does a Runge Kutta Pendulum work?

A Runge Kutta Pendulum works by dividing the pendulum's motion into small time intervals, calculating the position and velocity of the pendulum at each interval, and then using those values to update the position and velocity for the next interval. This process is repeated until the desired time is reached, providing an accurate approximation of the pendulum's motion.

3. What are the advantages of using a Runge Kutta Pendulum model?

One advantage of using a Runge Kutta Pendulum model is that it is a more accurate and efficient method compared to traditional analytical solutions. It also allows for the simulation of complex pendulum systems with multiple variables and parameters, providing a more comprehensive understanding of the system's behavior.

4. How is a Runge Kutta Pendulum used in scientific research?

A Runge Kutta Pendulum is commonly used in scientific research to study the behavior of pendulum systems in various fields such as physics, engineering, and mathematics. It is also used to validate theoretical models and to make predictions about the behavior of pendulum systems in real-world situations.

5. Are there any limitations to using a Runge Kutta Pendulum model?

One limitation of using a Runge Kutta Pendulum model is that it is a numerical approximation, so it may not provide an exact solution to the pendulum's motion. It also requires specific initial conditions and may not accurately predict the behavior of chaotic or highly non-linear systems. Additionally, the accuracy of the model may be affected by the chosen time interval and step size.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
4
Views
6K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Programming and Computer Science
Replies
10
Views
12K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top