Skip to content

Commit 02ab7b4

Browse files
committed
Created using Colab
1 parent defff43 commit 02ab7b4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

CentralDifferenceMethod.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"private_outputs": true,
7+
"provenance": [],
8+
"authorship_tag": "ABX9TyNbGIbKsIccowCM/6sLGwXo",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/GEORMC/Nnumerical_Methods_Course/blob/main/CentralDifferenceMethod.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {
34+
"id": "iyHe3OQBAUlA"
35+
},
36+
"outputs": [],
37+
"source": [
38+
"import numpy as np\n",
39+
"\n",
40+
"# System parameters\n",
41+
"m = 1.0 # mass in kg\n",
42+
"c = 0.1 # damping coefficient in Ns/m\n",
43+
"k = 2.0 # stiffness in N/m\n",
44+
"F = 1.0 # external force in N (constant)\n",
45+
"dt = 0.1 # time step in seconds\n",
46+
"t_max = 1.0 # maximum time in seconds\n",
47+
"\n",
48+
"# Time steps\n",
49+
"n_steps = int(t_max / dt) + 1\n",
50+
"\n",
51+
"# Initial conditions\n",
52+
"u = np.zeros(n_steps) # displacement array\n",
53+
"u[1] = 0.5 * F / m * dt**2 # initial displacement using approximation\n",
54+
"\n",
55+
"# Central Difference Method\n",
56+
"for n in range(1, n_steps - 1):\n",
57+
" # Apply the central difference formula\n",
58+
" u[n + 1] = (F - c * (u[n] - u[n - 1]) / (2 * dt) - k * u[n]) / m * dt**2 + 2 * u[n] - u[n - 1]\n",
59+
"\n",
60+
"# Display results\n",
61+
"time = np.arange(0, t_max + dt, dt)\n",
62+
"result = np.column_stack((time, u))\n",
63+
"\n"
64+
]
65+
}
66+
]
67+
}

0 commit comments

Comments
 (0)