├── FVM ├── 2D_Heat_Conduction │ ├── P1.m │ └── P2.m ├── 1D_Heat_Conduction │ ├── 1D_Heat_Conduction_without_Source.m │ └── 1D_Heat_Conduction_with_Source.m └── 1D_Heat_Convection │ └── 1D_Heat_Convection_and_Diffusion.m ├── FDM ├── Hyperbolic │ ├── readme.md │ ├── Example_5 │ │ ├── readme.md │ │ ├── Lax Method │ │ │ ├── readme.md │ │ │ └── Lax_Method.m │ │ ├── Lax-Wendroff Method │ │ │ ├── readme.md │ │ │ └── Lax_Wendroff_method.m │ │ └── MacCormak Method │ │ │ ├── readme.md │ │ │ └── MacCormak_Method.m │ ├── Example_4 │ │ ├── readme.md │ │ └── Hyperbolic_2nd_order_Wave.m │ ├── Example_1 │ │ ├── readme.md │ │ └── Unsteady_Convection_1D.m │ ├── Example_3 │ │ ├── readme.md │ │ └── Unsteady_Convection_1D.m │ └── Example_2 │ │ ├── readme.md │ │ └── Unsteady_Convection_1D.m ├── Elliptic │ ├── Temperature_Variation_Examples │ │ ├── 2D_Examples │ │ │ ├── readme.md │ │ │ ├── Example_4 │ │ │ │ ├── readme.md │ │ │ │ └── PSOR_method.m │ │ │ ├── Example_2 │ │ │ │ ├── readme.md │ │ │ │ └── Heat_Conduction_2D.m │ │ │ ├── Example_1 │ │ │ │ ├── readme.md │ │ │ │ └── Heat_Conduction_Convection.m │ │ │ ├── Example_3 │ │ │ │ ├── readme.md │ │ │ │ └── PSOR_2D_Heat_Conduction.m │ │ │ ├── Example_7 │ │ │ │ ├── readme.md │ │ │ │ ├── Heat_Conduction_2D.m │ │ │ │ └── Finding_Optimum_Relaxation_Factor.m │ │ │ ├── Example_6 │ │ │ │ ├── readme.md │ │ │ │ └── Heat_Conduction_2D.m │ │ │ └── Example_5 │ │ │ │ ├── readme.md │ │ │ │ └── Jacobi_VS_Gauss_Seidel_VS_PSOR.m │ │ └── 2D_Unsteady_Examples │ │ │ ├── readme.md │ │ │ ├── Example_1 │ │ │ ├── readme.md │ │ │ └── Unsteady_Heat_Conduction_2D.m │ │ │ ├── Example_3 │ │ │ ├── readme.md │ │ │ └── Unsteady_Heat_Conduction_2D.m │ │ │ └── Example_2 │ │ │ ├── readme.md │ │ │ └── Unsteady_Heat_Conduction_2D.m │ └── Stream_Function_Examples │ │ ├── Example_6 │ │ ├── readme.md │ │ └── Stream_Function.m │ │ ├── Example_4 │ │ ├── readme.md │ │ └── Stream_Function.m │ │ ├── Example_5 │ │ ├── readme.md │ │ └── Stream_Function.m │ │ ├── Example_2 │ │ ├── readme.md │ │ └── Gauss_Seidel_Method.m │ │ ├── Example_1 │ │ ├── readme.md │ │ ├── Gauss_Seidel_Method.m │ │ ├── Jacobi_Method.m │ │ └── Jacobi_VS_Gauss_Seidel.m │ │ └── Example_3 │ │ ├── readme.md │ │ └── Stream_Function.m └── Parabolic │ ├── Unsteady_Heat_Conduction_Examples │ └── 1D_Examples │ │ ├── Example_1 │ │ ├── readme.md │ │ └── Heat_Conduction_1D.m │ │ ├── Example_2 │ │ ├── readme.md │ │ └── 1D_Heat_Conduction_with_Fin.m │ │ └── Example_3 │ │ ├── readme.md │ │ └── Heat_Conduction_with_Heat_Generation_1D.m │ └── Couette_Flow_Examples │ ├── Example_3 │ ├── readme.md │ └── Couette_Flow.m │ ├── Example_4 │ ├── readme.md │ └── Couette_Flow.m │ ├── Example_5 │ ├── readme.md │ └── Couette_Flow.m │ ├── Example_1 │ ├── readme.md │ └── Couette_Flow_1D.m │ └── Example_2 │ ├── readme.md │ └── Couette_Flow_1D.m └── README.md /FVM/2D_Heat_Conduction/P1.m: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /FVM/2D_Heat_Conduction/P2.m: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/readme.md: -------------------------------------------------------------------------------- 1 | FDM Hyperbolic Equations Examples 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CFD : Numerical-Techniques for Fluid Flow and Heat Transfer-MATLAB 2 | 3 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/readme.md: -------------------------------------------------------------------------------- 1 | # 2D Steady State Heat Conduction (Elliptic PDE - Temperature variation) examples 2 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/readme.md: -------------------------------------------------------------------------------- 1 | # 2D Unsteady Heat Conduction (Elliptic PDE - Temperature variation) Examples 2 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : Non Linear Wave Form 1D using - 2 | 1. Lax method 3 | 2. Lax-Wendroff method 4 | 3. MacCormak method 5 | 6 | Problem Statement : 7 | 8 | ![Screenshot (1223)](https://user-images.githubusercontent.com/68963724/122454203-c0346480-cfc8-11eb-98b3-12bc730573ba.png) 9 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Example-1 : 1D Steady Heat Conduction 2 | 3 | Problem Statement : 4 | 5 | ![Q1](https://user-images.githubusercontent.com/68963724/120059760-11f46980-c071-11eb-83f1-f8970a152a7b.png) 6 | 7 | 8 | Pic : 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120059765-1caefe80-c071-11eb-98c8-1f05f703e500.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_4/readme.md: -------------------------------------------------------------------------------- 1 | # Temperature Variation Example-4 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1181)](https://user-images.githubusercontent.com/68963724/120073427-df209480-c0b5-11eb-8607-0187552c1362.png) 6 | 7 | 8 | Pic : 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120073175-e5624100-c0b4-11eb-882b-1edd9e876e61.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_6/readme.md: -------------------------------------------------------------------------------- 1 | # Example-6 : Stream Function Variation 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1237)](https://user-images.githubusercontent.com/68963724/123503714-43615480-d672-11eb-982a-ecef25e3a872.png) 6 | 7 | 8 | Pic : Psi Contour 9 | 10 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/123503691-20cf3b80-d672-11eb-88ae-042c5917880f.png) 11 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # 1D Steady Heat Conduction from a Fin 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1159)](https://user-images.githubusercontent.com/68963724/120059996-ba56fd80-c072-11eb-8c0e-058df32f2f54.png) 6 | 7 | 8 | Pic : 9 | 10 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/120059799-56800500-c071-11eb-9206-133c76caf7a1.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_4/readme.md: -------------------------------------------------------------------------------- 1 | # Example-4 : Stream Function Variation 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1236)](https://user-images.githubusercontent.com/68963724/123503543-32fcaa00-d671-11eb-8e77-0abc47b0779e.png) 6 | 7 | 8 | Pic : Stream Function (Psi) Contour 9 | 10 | ![Figure_2 (1)](https://user-images.githubusercontent.com/68963724/123503656-fb423200-d671-11eb-8e70-54e999e3e8b2.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_5/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : Stream Function Variation 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1239)](https://user-images.githubusercontent.com/68963724/123503768-9d621a00-d672-11eb-9808-2c02e650ca37.png) 6 | 7 | 8 | Pic : Stream Function (Psi) Contour Plot 9 | 10 | ![Figure_3 (1)](https://user-images.githubusercontent.com/68963724/123503661-01d0a980-d672-11eb-8ad2-1b5af2f9029c.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # Example-2 : 2D Heat Conduction 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1164)](https://user-images.githubusercontent.com/68963724/120060723-fee49800-c076-11eb-8bd9-6e92fabac216.png) 6 | 7 | 8 | Pic : Temperature variation 9 | 10 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/119830026-7997a100-bf19-11eb-98d8-11941fcd1f62.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # Stream_Function_Example-2 2 | 3 | Problem Statement: 4 | 5 | ![Screenshot (1153)](https://user-images.githubusercontent.com/68963724/119997102-fe9fba80-bfec-11eb-99ac-4d3d18ea6267.png) 6 | 7 | 8 | Solution : 9 | 10 | Pic : Stream_Function (PSI) Contour Plot 11 | 12 | ![Figure_1](https://user-images.githubusercontent.com/68963724/119825904-1ad02880-bf15-11eb-8d3a-8cda5811cbc2.png) 13 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_4/readme.md: -------------------------------------------------------------------------------- 1 | # Example-4 : 2nd Order Wave Equation - 1D 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1212)](https://user-images.githubusercontent.com/68963724/122415646-b8af9400-cfa5-11eb-8ebd-4de67b356f3b.png) 6 | 7 | 8 | Pic : Velocity Profile Comparison for different Courant Numbers (C) 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122411882-bf88d780-cfa2-11eb-87df-0d89017b7114.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Example-1 : 2D Heat Conduction & Convection 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1162)](https://user-images.githubusercontent.com/68963724/120060526-f8095580-c075-11eb-91fb-3dbc1bc8c257.png) 6 | 7 | 8 | Pic : Temperature variation Plot 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/119829636-1148bf80-bf19-11eb-84ac-c9104f8a3f90.png) 11 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # Example-3 : 2D Heat Conduction Example with PSOR method 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1166)](https://user-images.githubusercontent.com/68963724/120060820-a1048000-c077-11eb-950a-f5e277dc7fa7.png) 6 | 7 | 8 | Pic : Temperature Variation Method 9 | 10 | ![Figure_1 (2)](https://user-images.githubusercontent.com/68963724/119830482-ee6adb00-bf19-11eb-9f0e-bb2b6ef36372.png) 11 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/Lax Method/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : Non Linear Wave - 1D : Lax Method 2 | 3 | Pic_1 : Velocity Profile for (dt/dx) = 1 or dt = 0.05 4 | 5 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122453440-ff15ea80-cfc7-11eb-822c-120105dec004.png) 6 | 7 | 8 | Pic_2 : Velocity Profile for (dt/dx) = 0.5 or dt = 0.025 9 | 10 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122453471-05a46200-cfc8-11eb-95ac-2328b6b7c993.png) 11 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # 1D Steady Heat Conduction with Heat generation and Convection 2 | 3 | Problem Statament : 4 | 5 | ![Screenshot (1172)](https://user-images.githubusercontent.com/68963724/120062076-ce542c80-c07d-11eb-9802-3cbb97041b02.png) 6 | 7 | 8 | Pic : Temperature Variation Plot 9 | 10 | ![Figure_1 (2)](https://user-images.githubusercontent.com/68963724/120061749-3e61b300-c07c-11eb-9144-36429755fdb5.png) 11 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/Lax-Wendroff Method/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : Non Linear Wave 1D : Lax-Wendroff Method 2 | 3 | Pic_1 : Velocity profile for (dt/dx) = 1 or dt = 0.05 4 | 5 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122452845-4fd91380-cfc7-11eb-86c7-83e12f440f06.png) 6 | 7 | 8 | Pic_2 : Velocity profile for (dt/dx) = 0.5 or dt = 0.025 9 | 10 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122452857-549dc780-cfc7-11eb-95d1-d7101b31a75a.png) 11 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/MacCormak Method/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : Non Linear Wave - 1D : MacCormak Method 2 | 3 | 4 | Pic_1 : Velocity Profile for (dt/dx) = 1 or dt = 0.05 5 | 6 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122451775-266bb800-cfc6-11eb-9d4f-46093ae7a38b.png) 7 | 8 | 9 | Pic_2 : Velocity profile for (dt/dx) = 0.5 or dt = 0.025 10 | 11 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122451807-2cfa2f80-cfc6-11eb-858c-05152acb9160.png) 12 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # Example-3 : Couette Flow 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1229)](https://user-images.githubusercontent.com/68963724/123502977-a8667b80-d66d-11eb-9936-310372632f90.png) 6 | 7 | 8 | Pic : Velocity variation with height 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/123502776-7e608980-d66c-11eb-8c85-c40af5017a3a.png) 11 | 12 | 13 | Animation : Couette Flow Animation 14 | 15 | ![CouetteFlow](https://user-images.githubusercontent.com/68963724/123502660-b2877a80-d66b-11eb-8f10-a893901e46ab.gif) 16 | 17 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Example-1 : 1D Linear Convection (1st order Wave Equation) 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1218)](https://user-images.githubusercontent.com/68963724/122418132-a20a3c80-cfa7-11eb-8d02-44a0705869c4.png) 6 | 7 | 8 | Pic-1 : Velocity Profile Comparison 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122400891-c0693b80-cf99-11eb-99bc-da0128aa6587.png) 11 | 12 | 13 | Pic-2 : Velocity Profile for dt = 0.1 case 14 | 15 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122400971-d0811b00-cf99-11eb-8506-903ab728df3f.png) 16 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Stream_Function_Example-1 2 | 3 | Problem Statement: 4 | 5 | ![Screenshot (1150)](https://user-images.githubusercontent.com/68963724/119996000-d82d4f80-bfeb-11eb-8120-850fff4ff467.png) 6 | 7 | 8 | Solution Output: 9 | 10 | Pic_1 : Stream Function (PSI) Contour Plot 11 | 12 | ![PSI_Contour_Plot](https://user-images.githubusercontent.com/68963724/119825297-6e8e4200-bf14-11eb-8434-8b94a5f5fb09.png) 13 | 14 | Pic_2 : Error VS Iterations Plot 15 | 16 | ![Error_VS_Iteration_Plot](https://user-images.githubusercontent.com/68963724/119825316-72ba5f80-bf14-11eb-8a23-9b632a474a8e.png) 17 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_4/readme.md: -------------------------------------------------------------------------------- 1 | # Example-4 : 1D Flow Variation : Couette Flow with both plates stationary 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1231)](https://user-images.githubusercontent.com/68963724/123503096-5540f880-d66e-11eb-8757-b3fefd3af443.png) 6 | 7 | 8 | Pic : Velocity Variation with Height 9 | 10 | ![Figure_2](https://user-images.githubusercontent.com/68963724/123502799-a64fed00-d66c-11eb-9898-d3cd2e009501.png) 11 | 12 | 13 | Animation : Velocity VS Height 14 | 15 | ![CouetteFlow (1)](https://user-images.githubusercontent.com/68963724/123502743-43f6ec80-d66c-11eb-8f29-077bc2a06bdd.gif) 16 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_5/readme.md: -------------------------------------------------------------------------------- 1 | # Example-5 : 1D Flow variation : Couette Flow with both plates stationary 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1233)](https://user-images.githubusercontent.com/68963724/123503177-e9ab5b00-d66e-11eb-92ef-2bfccac3f72a.png) 6 | 7 | 8 | Pic : Velocity variation with Height 9 | 10 | ![Figure_3](https://user-images.githubusercontent.com/68963724/123502809-afd95500-d66c-11eb-8e75-e57685ee4f85.png) 11 | 12 | 13 | Animation : Velocity VS Height 14 | 15 | ![CouetteFlow (2)](https://user-images.githubusercontent.com/68963724/123502836-d9927c00-d66c-11eb-8baf-e98e2303f827.gif) 16 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Couette Flow Example : Flow between 2 infinitely parallel plates 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1174)](https://user-images.githubusercontent.com/68963724/120067533-d15d1600-c099-11eb-9e39-b2d22630c47f.png) 6 | 7 | 8 | Pic : Fluid Velocity Variation at different time steps 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120067201-01a3b500-c098-11eb-83ec-77a4f44fa24b.png) 11 | 12 | 13 | Gif : Couette Flow Animation 14 | 15 | ![CouetteFlow](https://user-images.githubusercontent.com/68963724/120922277-cfbccf00-c6e5-11eb-84cb-5ac0dc91d99d.gif) 16 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_7/readme.md: -------------------------------------------------------------------------------- 1 | # Example-7 : 2D Heat Conduction & Convection of gases 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1204)](https://user-images.githubusercontent.com/68963724/121521245-74564e00-ca11-11eb-8633-5719aa17e6be.png) 6 | 7 | 8 | Pic_1 : Temperature Variation Plot 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/121520657-c9de2b00-ca10-11eb-98d8-abbcb1f30280.png) 11 | 12 | 13 | Pic_2 : Number of Iterations VS Relaxation Factor 14 | 15 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/121520992-2e00ef00-ca11-11eb-84aa-8fac39479fe5.png) 16 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # Couette Flow : Flow between a top stationary plate and a bottom moving plate 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1178)](https://user-images.githubusercontent.com/68963724/120071406-d8415400-c0ac-11eb-877b-acc2ac7560fb.png) 6 | 7 | 8 | Pic : Velocity against Height at different time steps 9 | 10 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/120071279-2a35aa00-c0ac-11eb-85d2-f579e4c40d9a.png) 11 | 12 | 13 | Animation : Velocity Variation w.r.t time 14 | 15 | ![CouetteFlow (1)](https://user-images.githubusercontent.com/68963724/120925209-2ed61000-c6f5-11eb-824d-4c22ca4c51cb.gif) 16 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_1/readme.md: -------------------------------------------------------------------------------- 1 | # Example-1 : Unsteady Heat Conduction in 2D 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1194)](https://user-images.githubusercontent.com/68963724/120982311-36012a80-c796-11eb-8224-10802b3784e3.png) 6 | 7 | 8 | Pic : Temperature variation Plot at the end of simulation time 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120974561-e6b6fc00-c78d-11eb-8265-68b7e219331f.png) 11 | 12 | 13 | Animation : Temperature Variation with time 14 | 15 | ![Unsteady_Heat_Conduction](https://user-images.githubusercontent.com/68963724/120974708-1403aa00-c78e-11eb-9184-9fe2c1bd576d.gif) 16 | 17 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # Example-3 : Unsteady Heat Conduction in 2D 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1195)](https://user-images.githubusercontent.com/68963724/120982697-985a2b00-c796-11eb-8338-4f6ce72aad67.png) 6 | 7 | 8 | Pic : Temperature Variation at Steady State (i.e. at the end of simulation time) 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120976432-f6cfdb00-c78f-11eb-8883-d9a121f30ce9.png) 11 | 12 | 13 | Animation : Temperature variation with time 14 | 15 | ![Unsteady Heat COnduction](https://user-images.githubusercontent.com/68963724/120980724-85def200-c794-11eb-84ef-646179296f26.gif) 16 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # Example-2 : Unsteady Heat Conduction & COnvection in 2D 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1197)](https://user-images.githubusercontent.com/68963724/120983329-377f2280-c797-11eb-93c3-1c4d92cbeb42.png) 6 | 7 | 8 | Pic : Temperature Variation at Steady State (i.e. at the end of simulation time) 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120976028-90e35380-c78f-11eb-93a0-601d7d741d3c.png) 11 | 12 | 13 | Animation : Temperature Variation with time 14 | 15 | ![Unsteady Heat Conduction](https://user-images.githubusercontent.com/68963724/120978599-47483800-c792-11eb-999c-05603e3fe06f.gif) 16 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # Example-3 : 1D Linear Convection (1st Order Wave Equation) - Comparison between FTBS, Lax & Lax-Wendroff Schemes 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1207)](https://user-images.githubusercontent.com/68963724/122413570-1ba02b80-cfa4-11eb-87fa-940b2ba5e0a5.png) 6 | 7 | 8 | Pic_1 : Velocity Profiles for different Courant Numbers (C) - FTBS Scheme 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/122403286-e263bd80-cf9b-11eb-9abe-0fc70b12b967.png) 11 | 12 | 13 | Pic_2 : Velocity Profiles for different Courant Numbers (C) - Lax_Wendroff Scheme 14 | 15 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122403319-e8f23500-cf9b-11eb-941c-f466478149e7.png) 16 | 17 | 18 | Pic_3 : Velocity Profiles for different Courant Numbers (C) - Lax Scheme 19 | 20 | ![Figure_3](https://user-images.githubusercontent.com/68963724/122403349-f0194300-cf9b-11eb-96e6-edbdf9a585b3.png) 21 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_2/readme.md: -------------------------------------------------------------------------------- 1 | # Example-2 : 1D Linear Convection (1st order Wave Equation) 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1219)](https://user-images.githubusercontent.com/68963724/122418205-ab93a480-cfa7-11eb-8833-9869cddea786.png) 6 | 7 | 8 | Pic_1 : Velocity Profile with 21 nodes 9 | 10 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/122402250-ee9b4b00-cf9a-11eb-801f-3dfa5ff6c1e9.png) 11 | 12 | 13 | Pic_2 : Velocity Profile with 41 nodes 14 | 15 | ![Figure_2](https://user-images.githubusercontent.com/68963724/122402270-f3f89580-cf9a-11eb-9027-73f5c6be9b3f.png) 16 | 17 | 18 | Pic_3 : Velocity Profile with 81 nodes 19 | 20 | ![Figure_3](https://user-images.githubusercontent.com/68963724/122402296-fa870d00-cf9a-11eb-826f-93c21e1c5b6e.png) 21 | 22 | 23 | Pic_4 : Velocity Profile with 161 nodes 24 | 25 | ![Figure_4](https://user-images.githubusercontent.com/68963724/122402330-01ae1b00-cf9b-11eb-85af-df9138e073d0.png) 26 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_6/readme.md: -------------------------------------------------------------------------------- 1 | # Example-6 : 2D Heat Conduction & Convection 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1202)](https://user-images.githubusercontent.com/68963724/121504225-78797000-c9ff-11eb-94f1-7213060e6010.png) 6 | 7 | 8 | Pic-1 : Temperature Variation plot from Jacobi Method 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/121503499-bde96d80-c9fe-11eb-899d-3378795c2ef1.png) 11 | 12 | 13 | Pic-2 : Temperature Variation plot from Gauss-Seidel Method 14 | 15 | ![Figure_1 (1)](https://user-images.githubusercontent.com/68963724/121503563-ce99e380-c9fe-11eb-9210-4756603b71ad.png) 16 | 17 | 18 | Pic-3 : Temperature variation plot from PSOR Method 19 | 20 | ![Figure_2](https://user-images.githubusercontent.com/68963724/121503599-d5c0f180-c9fe-11eb-88b0-6084d9853fef.png) 21 | 22 | 23 | Pic-4 : Error VS iterations plot 24 | 25 | ![Figure_3](https://user-images.githubusercontent.com/68963724/121503620-dbb6d280-c9fe-11eb-91ed-2dd713d33e9d.png) 26 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_1/Heat_Conduction_1D.m: -------------------------------------------------------------------------------- 1 | % Steady 1D Heat Conduction Example-1 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | M = 21; % Number of Points 7 | 8 | % Calculated values 9 | dx = Lx/(M-1); % Step Size 10 | 11 | % Initilizing Temperature Matrix 12 | T = zeros(1,M); 13 | 14 | % Boundary Conditions 15 | T(1,1) = 100; % Left Boundary T = 100 C 16 | T(1,M) = 0; % Right Boundary T = 0 C 17 | 18 | % Computation 19 | epsilon = 1e-8; % Error Sensivity 20 | err = 1; % Error 21 | n = 0; % Iterator 22 | 23 | while err>epsilon 24 | T_old = T; 25 | for i=2:M-1 26 | T(1,i) = (1/2)*(T(1,i+1)+T(1,i-1)); 27 | end 28 | err = 0; % Error 29 | for i=1:M 30 | err = err + power(T(1,i)-T_old(1,i),2); 31 | end 32 | err = sqrt(err/M); 33 | n = n+1; % iterator 34 | end 35 | 36 | T 37 | n 38 | err 39 | 40 | % Plotting 41 | x = linspace(0,Lx,M); 42 | plot(x,T,'r-o') 43 | xlabel('Domain Length (x)'),ylabel('Temperature') 44 | title('Temperature Variation') 45 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_5/readme.md: -------------------------------------------------------------------------------- 1 | # Temperature variation Example-5 2 | 3 | Problem Statement : 4 | 5 | ![Screenshot (1182)](https://user-images.githubusercontent.com/68963724/120100425-590c5880-c15e-11eb-82c0-841bb55dbc73.png) 6 | 7 | 8 | Pic_1 : Temperature Contour Plot based on Jacobi Method 9 | 10 | ![Figure_1](https://user-images.githubusercontent.com/68963724/120100283-9d4b2900-c15d-11eb-85be-0f03a338a11f.png) 11 | 12 | 13 | 14 | Pic_2 : Temperature Contour Plot based on Gauss-Seidel Method 15 | 16 | ![Figure_2](https://user-images.githubusercontent.com/68963724/120100279-97edde80-c15d-11eb-92b5-b017f88b3c22.png) 17 | 18 | 19 | Pic_3: Temperature Contour Plot based on PSOR Method 20 | 21 | ![Figure_3](https://user-images.githubusercontent.com/68963724/120100263-7b51a680-c15d-11eb-8681-f21867dab6a7.png) 22 | 23 | 24 | Pic_4: Errors VS Iterations Comparison between Jacobi, Gauss-Seidel & PSOR methods 25 | 26 | ![Figure_4](https://user-images.githubusercontent.com/68963724/120100248-5d844180-c15d-11eb-9739-2efd9376c542.png) 27 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_3/readme.md: -------------------------------------------------------------------------------- 1 | # Example-3 : Stream Function Variation : Jacobi VS Gauss-Seidel VS PSOR methods 2 | 3 | Problem Statement : f = Stream Function (Psi) 4 | 5 | ![Screenshot (1226)](https://user-images.githubusercontent.com/68963724/123247244-4e967200-d504-11eb-9959-dabdc17f3c45.png) 6 | 7 | 8 | 9 | Pic_1 : Stream Function (Psi) Contour Plot (from Jacobi Method) 10 | 11 | ![Figure_1](https://user-images.githubusercontent.com/68963724/123247050-15f69880-d504-11eb-9a26-96a78c3a13cd.png) 12 | 13 | 14 | Pic_2 : Stream Function (Psi) Contour Plot (from Gauss-Seidel Method) 15 | 16 | ![Figure_2](https://user-images.githubusercontent.com/68963724/123247069-1c851000-d504-11eb-9a01-47d0557f1010.png) 17 | 18 | 19 | Pic_3 : Stream Function (Psi) Contour Plot (from PSOR Method) 20 | 21 | ![Figure_3](https://user-images.githubusercontent.com/68963724/123247092-227af100-d504-11eb-9573-f5311cf95163.png) 22 | 23 | 24 | Pic_4 : Error VS Iterations 25 | 26 | ![Figure_4](https://user-images.githubusercontent.com/68963724/123247115-273fa500-d504-11eb-947f-bcc8398f1569.png) 27 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_3/Heat_Conduction_with_Heat_Generation_1D.m: -------------------------------------------------------------------------------- 1 | % 1D Steady Heat conduction with Heat Generation & Convection 2 | clear all; clc 3 | 4 | % Known Parameters 5 | Lx = 0.04; % Length of x-domian (0.04 m) 6 | M = 21; % Number of grid points chosen along x-domain 7 | T_inf = 30; % Free air temperature (30 C) 8 | h = 45; % Convection Heat transfer coefficient 9 | k = 28; % Thermal conductivity 10 | q = 5e6; % Heat Generation (W/m^3) 11 | 12 | dx = Lx/(M-1); % grid size 13 | 14 | % Initializing Temperature Matrix 15 | T = zeros(1,M); 16 | 17 | % Boundary Conditions 18 | T(1,1) = 0; % Left Boundary Condition 19 | 20 | % Computation 21 | err = 1; % Error 22 | epsilon = 1e-8; % Tolerance 23 | n = 0; % Iterator 24 | 25 | while err>epsilon 26 | T_old = T; % T_old = T from kth iteration 27 | for i=2:M-1 28 | T(1,i) = (1/2)*(T(1,i+1)+T(1,i-1)+(q/k)*(dx^2)); 29 | end 30 | % Right Boundary Condition 31 | T(1,M) = (4*T(1,M-1)-T(1,M-2)+T_inf*(2*h*dx/k))/(3+(2*h*dx/k)); 32 | err = 0; % Error 33 | for i=1:M 34 | err = err + power(T(1,i)-T_old(1,i),2); 35 | end 36 | err = sqrt(err/M); 37 | n = n+1; % iterator 38 | end 39 | T 40 | n 41 | err 42 | 43 | % Plotting 44 | x = linspace(0,Lx,M); 45 | plot(x,T,'ro-') 46 | xlabel('x-Domain Length'),ylabel('Temperature') 47 | -------------------------------------------------------------------------------- /FDM/Parabolic/Unsteady_Heat_Conduction_Examples/1D_Examples/Example_2/1D_Heat_Conduction_with_Fin.m: -------------------------------------------------------------------------------- 1 | % 1D heat Conduction Example-1 : Fin connected to a wall 2 | % Tip of fin is insulated 3 | clear all; clc 4 | 5 | % Known Parameters 6 | Lx = 1; % Length of x-domain (1 metre) 7 | k = 1.2; % Thermal Conductivity of wall 8 | h = 8; % Convection heat Transfer Coefficient 9 | d = 0.7; % Diameter of Fin 10 | M = 21; % Number of Points 11 | 12 | % Calculations 13 | dx = Lx/(M-1); 14 | P = (22/7)*d; % Perimeter 15 | A = (22/28)*(d^2); % Cross sectional area of Fin 16 | m = sqrt(h*P/(k*A)); 17 | T_inf = 300; % Free Air Temperature; T_infinity = 300K 18 | 19 | % Initializing temperature Matrix 20 | T = zeros(1,M); 21 | 22 | % Boundary Conditions 23 | T(1,1) = 500; % Left Boundary; T = 500K 24 | 25 | err = 1; 26 | epsilon = 1e-8; 27 | n = 0; % Iterator 28 | 29 | while err>epsilon 30 | T_old = T; 31 | for i=2:M-1 32 | T(1,i) = (1/(2+(m^2)*(dx^2)))*(T(1,i+1)+T(1,i-1)+(m^2)*(dx^2)*T_inf); 33 | end 34 | T(1,M) = (4*T(1,M-1)-T(1,M-2))/3; % Right Adiabatic Boundary Condition using 2nd order approximation 35 | err = 0; % Error 36 | for i=1:M 37 | err = err + power(T(1,i)-T_old(1,i),2); 38 | end 39 | err = sqrt(err/M); 40 | n = n+1; % iterator 41 | end 42 | T 43 | n 44 | err 45 | 46 | % Plotting 47 | x = linspace(0,Lx,M); 48 | plot(x,T,'r-o'); 49 | xlabel('x-Domain Length'),ylabel('Temperature') 50 | title('Temperature Variation') 51 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_1/Couette_Flow_1D.m: -------------------------------------------------------------------------------- 1 | % Example-1 2 | % Couette Flow : Flow between 2 infinitely parallel plates using FTCS 3 | clear all; clc 4 | 5 | % Known Parameters 6 | N = 101; % No. of grid points 7 | h = 1; % Distance between the plates (Length of y-domain) 8 | dy = h/(N-1); % Grid Size 9 | u_m = 1; % Velocity of Moving plate 10 | Re = 100; % Reynold's Number (Aribitrary) 11 | 12 | % Calculations 13 | % Since nu = gamma = U*h/Re 14 | gamma = (u_m*h/Re); % Diffusion Coefficient 15 | 16 | % Condition for stability: gamma_y <= (1/2) 17 | % gamma_y = nu*dt/(dy^2) 18 | dt = (dy^2)/(2*gamma); % Time Step 19 | gamma_y = gamma*dt/(dy*dy); 20 | 21 | % Initialization 22 | u = zeros(N,1); % Velocity Matrix 23 | U = []; % Velocity variation array with time step 24 | t = 0; % Time 25 | T = []; % Time array 26 | 27 | % Initial & Boundary Conditions 28 | u(1,1) = u_m; % Moving plate 29 | u(N,1) = 0; % Stationary plate 30 | 31 | % Computation - Explicit Method 32 | % Forward in Time Central in Space (FTCS) 33 | n = 0; % Iterator 34 | while t<=50 35 | u_old = u; 36 | for j=2:N-1 % For Internal Nodes 37 | u(j,1) = u_old(j,1) + gamma_y*(u_old(j+1,1)-2*u_old(j,1)+u_old(j-1,1)); 38 | end 39 | n = n+1; 40 | t = t + dt; 41 | U = [U;u']; 42 | T(end+1) = t; 43 | end 44 | u 45 | n 46 | t 47 | 48 | % Plotting 49 | figure; 50 | hold on; 51 | grid on; 52 | for i=1:(2/dt):n 53 | plot(U(i,:),h:-dy:0,'b-') 54 | end 55 | xlabel('Velocity (u)'),ylabel('Height (Y)') 56 | title('Flow between 2 parallel plates') 57 | hold off; 58 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_2/Couette_Flow_1D.m: -------------------------------------------------------------------------------- 1 | % 1D Couette Flow Example-2 using Crank-Nicolson Scheme 2 | % Fixed Plate -> Top Plate 3 | % Moving Plate -> Bottom Plate 4 | clear all; clc 5 | 6 | % Known parameters 7 | h = 0.04; % Distance between the plates (0.04 m) 8 | N = 41; % No. of grid points considered 9 | dy = h/(N-1); % Grid Size 10 | U_m = 40; % Velocity of Moving Plate (40 m/s) 11 | v = 0.000217; % v = Uh/Re; Re = reynolds number (m^2/s) 12 | dt = 2e-3; % Time Step 13 | gamma_y = v*dt/(dy^2); % Diffusion Factor Coefficient 14 | 15 | % Initialization 16 | t = 0; % Time 17 | u = zeros(N,1); % Velocity Matrix 18 | U = []; % Velocities at particular time steps 19 | T = []; % Particular Time Steps 20 | n = 0; % Iterator 21 | 22 | % Initial & Boundary Conditions 23 | u(N,1) = U_m; % Moving Plate (Bottom Plate) 24 | u(1,1) = 0; % Stationary Plate (Top Plate) 25 | 26 | % Computation - Implicit Method 27 | % Crank Nicolson Method 28 | while t<=10 29 | u_old = u; % u_old at nth time step & u at (n+1)th time step 30 | for i=2:N-1 31 | u(i,1) = ((gamma_y/(2+2*gamma_y))*(u(i+1,1)+u(i-1,1)+u_old(i+1,1)+u_old(i-1,1)))+((1-gamma_y)/(1+gamma_y))*u_old(i,1); 32 | end 33 | U = [U;u']; 34 | T(end+1) = t; 35 | t = t+dt; % Time 36 | n = n+1; % Iterator 37 | end 38 | u 39 | n 40 | t 41 | 42 | % Plotting 43 | figure; 44 | hold on; 45 | grid on; 46 | %plot(u,h:-dy:0) 47 | for i=1:150:n 48 | plot(U(i,:),h:-dy:0,'b-') 49 | end 50 | xlabel('Velocity (u)'),ylabel('Height (Y)') 51 | title('Flow between 2 parallel plates') 52 | hold off; 53 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_3/Unsteady_Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % Unsteady Heat Conduction 2D 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | Ly = 1; % Length of y-domain 7 | alpha = 1.4; % Thermal Diffusivity 8 | M = 41; % Number of grid points in x-direction 9 | N = 41; % Number of grid points in y-direction 10 | 11 | % Calculated Values 12 | dx = Lx/(M-1); % Grid size in x-direction 13 | dy = Ly/(N-1); % Grid size in y-direction 14 | dt = 1e-3; % Time Step 15 | gamma_x = alpha*dx/(dt^2); 16 | gamma_y = alpha*dy/(dt^2); 17 | 18 | % Initializing Temperature matrix 19 | T = zeros(N,M); 20 | 21 | % Boundary Conditions 22 | T(:,1) = 500; % Left Wall Boundary Condition 23 | T(:,M) = 1000; % Right Wall Boundary Condition 24 | 25 | % Computation 26 | n = 0; % Iterator 27 | t = 0; 28 | while t<50 29 | for i=1:M 30 | T(1,i) = T(2,i); % Top Neumann Boundary Condition 31 | T(N,i) = T(N-1,i); % Bottom Neumann Boundary Condition 32 | end 33 | err = 0; % Error 34 | for j=2:N-1 35 | for i=2:M-1 36 | T_temp = T(j,i); 37 | T(j,i) = (1/(1+2*gamma_x+2*gamma_y))*(gamma_x*(T(j+1,i)+T(j-1,i))+gamma_y*(T(j,i+1)+T(j,i-1))+T_temp); 38 | err = err + power(T(j,i)-T_temp,2); 39 | end 40 | end 41 | err = sqrt(err/(M*N)); 42 | n = n+1; 43 | t = t+dt; 44 | end 45 | T 46 | n 47 | err 48 | 49 | % Plotting 50 | x = linspace(0,Lx,M); 51 | y = linspace(Ly,0,N); 52 | [X,Y] = meshgrid(x,y); 53 | contourf(X,Y,T,':','ShowText','on'),colorbar,colormap(jet) 54 | set(gca, 'XTick',0:1:Lx) 55 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 56 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_5/Stream_Function.m: -------------------------------------------------------------------------------- 1 | % Example-5 : Stream Function Variation 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | Ly = 1; % Length of y-domain 7 | M = 81; % Number of grid points in x-direction 8 | N = 81; % Number of grid points in y-direction 9 | 10 | % Calculated Values 11 | dx = Lx/(M-1); 12 | dy = Ly/(N-1); 13 | x = linspace(0,Lx,M); 14 | y = linspace(0,Ly,N); 15 | 16 | % Coefficients 17 | ap = 2*((1/(dx^2))+(1/(dy^2))); 18 | ae = 1/(dx^2); 19 | aw = 1/(dx^2); 20 | an = 1/(dy^2); 21 | as = 1/(dy^2); 22 | 23 | % Initializing Stream Function Matrix 24 | psi = zeros(N,M); 25 | 26 | % Boundary Conditions 27 | psi(N,:) = 0; % Bottom Boundary 28 | psi(1,:) = 100; % Top Boundary 29 | psi(:,1) = 0; % Left Boundary 30 | psi(:,M) = 100; % Right Boundary 31 | 32 | % Computation 33 | epsilon = 1e-8; % Error Sensivity 34 | err = 1; % Error 35 | n = 0; % Iterator 36 | 37 | while err>epsilon 38 | err = 0; % Error 39 | for j=2:N-1 % For Internal Grid points 40 | for i=2:M-1 % For Internal Grid points 41 | psi_temp = psi(j,i); % Value of Psi from previous iteration 42 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1) + 81*x(i)*y(j)); % Updated Psi value 43 | err = err + power(psi(j,i)-psi_temp,2); 44 | end 45 | end 46 | err = sqrt(err/(M*N)); 47 | n = n+1; % Iterator 48 | end 49 | %psi 50 | n 51 | err 52 | 53 | % Plotting 54 | x = linspace(0,Lx,M); 55 | y = linspace(Ly,0,N); 56 | [X,Y] = meshgrid(x,y); 57 | contourf(X,Y,psi,'ShowText','on'),colorbar,colormap(jet) 58 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 59 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_2/Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % 2D Heat Conduction Example-2 2 | clear all; clc 3 | 4 | % Known Parameters 5 | Lx = 1; % Length of X-Domain 6 | Ly = 1; % Length of Y-Domain 7 | M = 21; % Number of grid points starting from 1 in x-direction 8 | N = 21; % Number of grid points starting from 1 in y-direction 9 | 10 | % Calculated constants 11 | dx = Lx/(M-1); % Grid size along x-direction 12 | dy = Ly/(N-1); % Grid size along y-direction 13 | 14 | % Coefficients 15 | ap = 2*((1/(dx^2))+(1/(dy^2))); 16 | ae = 1/(dx^2); 17 | aw = 1/(dx^2); 18 | an = 1/(dy^2); 19 | as = 1/(dy^2); 20 | 21 | % Initializing Temperature matrix 22 | T = zeros(N,M); 23 | 24 | % Boundary Conditions 25 | T(:,1) = 500; % Left Boundary T=500K 26 | T(:,M) = 1000; % Right Boundary T=500K 27 | 28 | % Computation 29 | epsilon = 1e-8; % Error Sensivity 30 | err = 1; % Error 31 | n = 0; % Iterator 32 | 33 | while err>epsilon 34 | err = 0; % Error 35 | for j=2:N-1 % For Internal Grid points 36 | for i=2:M-1 % For Internal Grid points 37 | T_old = T(j,i); 38 | T(j,i) = (1/ap)*(ae*T(j+1,i) + aw*T(j-1,i) + an*T(j,i+1) + as*T(j,i-1)); 39 | err = err + power(T(j,i)-T_old,2); 40 | end 41 | end 42 | for i=1:M 43 | T(1,i) = T(2,i); % Top Neumann Boundary Condition 44 | T(N,i) = T(N-1,i); % Bottom Neumann Boundary Condition 45 | end 46 | err = sqrt(err/(M*N)); 47 | n = n+1; % Iterator 48 | end 49 | T 50 | n 51 | err 52 | 53 | % Plotting 54 | x = linspace(0,Lx,M); 55 | y = linspace(0,Ly,N); 56 | [X,Y] = meshgrid(x,y); 57 | contourf(X,Y,T,'ShowText','on'),colorbar 58 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 59 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_2/Gauss_Seidel_Method.m: -------------------------------------------------------------------------------- 1 | % Stream Function Example-2 using Gauss-Seidel Method 2 | 3 | % Known Parameters 4 | Lx = 4; % Length of domain along x-direction 5 | Ly = 4; % Length of domain along y-direction 6 | M = 21; % Number of grid points starting from 1 in x-direction 7 | N = 21; % Number of grid points starting from 1 in y-direction 8 | 9 | % Calculated constants 10 | dx = Lx/(M-1); % Grid size along x-direction 11 | dy = Ly/(N-1); % Grid size along y-direction 12 | % Coefficients 13 | ap = 2*((1/(dx^2))+(1/(dy^2))); 14 | ae = 1/(dx^2); 15 | aw = 1/(dx^2); 16 | an = 1/(dy^2); 17 | as = 1/(dy^2); 18 | 19 | % Initializing Stream Function Matrix 20 | psi = zeros(N,M); 21 | 22 | % Boundary Conditions 23 | psi(N,:) = 0; % Bottom Boundary 24 | psi(1,:) = 1; % Top Boundary 25 | psi(:,1) = 0; % Left Boundary 26 | psi(:,M) = 0; % Right Boundary 27 | 28 | % Computation 29 | epsilon = 1e-8; % Error Sensivity 30 | err = 1; % Error 31 | n = 0; % Iterator 32 | 33 | while err>epsilon 34 | err = 0; % Error 35 | for j=2:N-1 % For Internal Grid points 36 | for i=2:M-1 % For Internal Grid points 37 | psi_temp = psi(j,i); 38 | % Psi(j,i)_(k+1)th_iteration Calculation 39 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1)); 40 | err = err + power(psi(j,i)-psi_temp,2); 41 | end 42 | end 43 | err = sqrt(err/(M*N)); 44 | n = n+1; % Iterator 45 | end 46 | psi 47 | n 48 | err 49 | 50 | % Plotting 51 | x = linspace(0,Lx,M); 52 | y = linspace(Ly,0,N); 53 | [X,Y] = meshgrid(x,y); 54 | contourf(X,Y,psi,'ShowText','on'),colorbar 55 | set(gca, 'XTick',0:1:Lx) 56 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 57 | -------------------------------------------------------------------------------- /FVM/1D_Heat_Conduction/1D_Heat_Conduction_without_Source.m: -------------------------------------------------------------------------------- 1 | %Steady State 1D Heat Conduction without Source -> using Finite Volume Method (FVM) 2 | 3 | clear all; clc; 4 | 5 | %Following SI Units 6 | %Known Values 7 | L = 1; %Length of the domain (or rod in this example) 8 | n = 5; %Number of control volumes considered 9 | dx = L/n; %Grid spacing; %Assumed Uniform Grid Spacing 10 | 11 | %Thermal Conductivity - Assumed constant throught the domain 12 | ke = 1000; 13 | kw = 1000; 14 | 15 | %Boundary Conditions 16 | %Temperature in Degree Celsius 17 | TA = 100; 18 | TB = 500; 19 | 20 | %Calculated Constants 21 | aw = kw/dx; 22 | ae = ke/dx; 23 | 24 | %Mesh Creation 25 | x = dx/2:dx:L; 26 | N = length(x); 27 | 28 | %Matrices creation 29 | A = zeros(N,N); %Coefficient Matrix 30 | B = zeros(N,1); %Constants' Matrix 31 | 32 | %Algorithm 33 | for i=1:N 34 | if x(i) == min(x) %For Boundary Node-1 (Left most Node) 35 | A(i,i) = ae + 2*(kw/dx); 36 | A(i,i+1) = -ae; 37 | b(i,1) = 2*(kw/dx)*TA; 38 | elseif x(i) == max(x) %For Boundary Node-2 (Right most Node) 39 | A(i,i) = aw + 2*(ke/dx); 40 | A(i,i-1) = -aw; 41 | b(i,1) = 2*(ke/dx)*TB; 42 | else %For all Internal Nodes 43 | A(i,i) = ae + aw; 44 | A(i,i+1) = -ae; 45 | A(i,i-1) = -aw; 46 | end 47 | end 48 | 49 | %Final results 50 | A %Coefficient Tri-Diagonal Matrix 51 | b %Constant Matrix 52 | T = A\b %Displays the Temperature matrix after solving 53 | 54 | %Plots and Comparison between Analytical/Exact and Numerical solution curves 55 | y = linspace(0,L,500); 56 | Te = ((TB-TA)/L)*y + TA; %Exact solution (Analytical) for Temperature 57 | plot(x,T,'o-',y,Te,'r') 58 | xlabel('Length') 59 | ylabel('Temperature') 60 | legend('Numerical solution','Exact/Analytical solution') 61 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_4/Stream_Function.m: -------------------------------------------------------------------------------- 1 | % Example-4 : Stream Function Variation 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | Ly = 1; % Length of y-domain 7 | M = 81; % Number of grid points in x-direction 8 | N = 81; % Number of grid points in y-direction 9 | 10 | % Calculated Values 11 | dx = Lx/(M-1); 12 | dy = Ly/(N-1); 13 | x = linspace(0,Lx,M); 14 | y = linspace(Ly,0,N); 15 | 16 | % Coefficients 17 | ap = 2*((1/(dx^2))+(1/(dy^2))); 18 | ae = 1/(dx^2); 19 | aw = 1/(dx^2); 20 | an = 1/(dy^2); 21 | as = 1/(dy^2); 22 | 23 | % Initializing Stream Function Matrix 24 | psi = zeros(N,M); 25 | 26 | % Boundary Conditions 27 | psi(N,:) = 0; % Bottom Boundary 28 | psi(:,1) = 0; % Left Boundary 29 | psi(:,M) = 0; % Right Boundary 30 | % Top Boundary 31 | for i=1:M 32 | if(0<=x(i) && x(i)<=(2/3)) 33 | psi(1,i) = 75*x(i); 34 | elseif((2/3)<=x(i) && x(i)<=1) 35 | psi(1,i) = 150*(1-x(i)); 36 | end 37 | end 38 | 39 | % Computation - Gauss-Seidel Method 40 | epsilon = 1e-8; % Error Sensivity 41 | err = 1; % Error 42 | n = 0; % Iterator 43 | 44 | while err>epsilon 45 | err = 0; % Error 46 | for j=2:N-1 % For Internal Grid points 47 | for i=2:M-1 % For Internal Grid points 48 | psi_temp = psi(j,i); 49 | % Psi(j,i)_(k+1)th_iteration Calculation 50 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1)); 51 | err = err + power(psi(j,i)-psi_temp,2); 52 | end 53 | end 54 | err = sqrt(err/(M*N)); 55 | n = n+1; % Iterator 56 | end 57 | %psi 58 | n 59 | err 60 | 61 | % Plotting 62 | [X,Y] = meshgrid(x,y); 63 | contourf(X,Y,psi,'ShowText','on'),colorbar,colormap(jet) 64 | grid on; 65 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 66 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_6/Stream_Function.m: -------------------------------------------------------------------------------- 1 | % Example-6 : Stream Function Variation 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 4; % Length of x-domain 6 | Ly = 4; % Length of y-domain 7 | M = 81; % Number of grid points in x-direction 8 | N = 81; % Number of grid points in y-direction 9 | 10 | % Calculated Values 11 | dx = Lx/(M-1); 12 | dy = Ly/(N-1); 13 | x = linspace(0,Lx,M); 14 | y = linspace(0,Ly,N); 15 | 16 | % Coefficients 17 | ap = 2*((1/(dx^2))+(1/(dy^2))); 18 | ae = 1/(dx^2); 19 | aw = 1/(dx^2); 20 | an = 1/(dy^2); 21 | as = 1/(dy^2); 22 | 23 | % Initializing Stream Function Matrix 24 | psi = zeros(N,M); 25 | 26 | % Boundary Conditions 27 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 28 | psi(:,1) = 0; % Left Boundary 29 | % Right Boundary 30 | for j=1:N 31 | psi(j,M) = 12 + y(j); 32 | end 33 | % Top & Bottom Boundaries 34 | for i=1:M 35 | psi(1,i) = x(i)^2; % Top Boundary 36 | psi(N,i) = 3*x(i); % Bottom Boundary 37 | end 38 | 39 | % Computation - Gauss-Seidel Method 40 | epsilon = 1e-8; % Error Sensivity 41 | err = 1; % Error 42 | n = 0; % Iterator 43 | 44 | while err>epsilon 45 | err = 0; % Error 46 | for j=2:N-1 % For Internal Grid points 47 | for i=2:M-1 % For Internal Grid points 48 | psi_temp = psi(j,i); % Value of Psi from previous iteration 49 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1)); % Updated Psi value 50 | err = err + power(psi(j,i)-psi_temp,2); 51 | end 52 | end 53 | err = sqrt(err/(M*N)); 54 | n = n+1; % Iterator 55 | end 56 | %psi 57 | n 58 | err 59 | 60 | % Plotting 61 | x = linspace(0,Lx,M); 62 | y = linspace(Ly,0,N); 63 | [X,Y] = meshgrid(x,y); 64 | contourf(X,Y,psi,'ShowText','on'),colorbar,colormap(jet) 65 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 66 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_1/Gauss_Seidel_Method.m: -------------------------------------------------------------------------------- 1 | % Stream Function Example-1 using Gauss-Seidel Method 2 | 3 | % Known Parameters 4 | Lx = 6; % Length of domain along x-direction 5 | Ly = 4; % Length of domain along y-direction 6 | M = 31; % Number of grid points starting from 1 in x-direction 7 | N = 21; % Number of grid points starting from 1 in y-direction 8 | 9 | % Calculated constants 10 | dx = Lx/(M-1); % Grid size along x-direction 11 | dy = Ly/(N-1); % Grid size along y-direction 12 | % Coefficients 13 | ap = 2*((1/(dx^2))+(1/(dy^2))); 14 | ae = 1/(dx^2); 15 | aw = 1/(dx^2); 16 | an = 1/(dy^2); 17 | as = 1/(dy^2); 18 | 19 | % Initializing Stream Function Matrix 20 | psi = zeros(N,M); 21 | 22 | % Boundary Conditions 23 | psi(N,1:6) = 0; % Bottom Boundary 24 | psi(N,7:M) = 100; % Bottom Boundary 25 | psi(:,1) = 0; % Left Boundary 26 | psi(1,:) = 0; % Top Boundary 27 | 28 | % Computation 29 | epsilon = 1e-8; % Error Sensivity 30 | err = 1; % Error 31 | n = 0; % Iterator 32 | 33 | while err>epsilon 34 | err = 0; % Error 35 | for j=2:N-1 % For Internal Grid points 36 | for i=2:M-1 % For Internal Grid points 37 | psi_temp = psi(j,i); 38 | % Psi(j,i)_(k+1)th_iteration Calculation 39 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1)); 40 | err = err + power(psi(j,i)-psi_temp,2); 41 | end 42 | end 43 | for j=1:N % Right Neumann Boundary Condition 44 | psi(j,M) = psi(j,M-1); 45 | end 46 | err = sqrt(err/(M*N)); 47 | n = n+1; % Iterator 48 | end 49 | psi 50 | n 51 | err 52 | 53 | % Plotting 54 | x = linspace(0,Lx,M); 55 | y = linspace(Ly,0,N); 56 | [X,Y] = meshgrid(x,y); 57 | contourf(X,Y,psi,'ShowText','on'),colorbar 58 | set(gca, 'XTick',0:1:Lx) 59 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 60 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_1/Unsteady_Convection_1D.m: -------------------------------------------------------------------------------- 1 | % Example-1 : 1D Linear Convection 2 | clear all; clc 3 | 4 | % Velocity is 2m/s in the range x=0.1 to x=0.3 and is 5 | % 1m/s everywhere else. 6 | 7 | % Simulating based on different size of time steps 8 | 9 | % Known Values 10 | Lx = 1; % Length of x-domain 11 | C = 1; % Velocity (1 m/s) 12 | M = 41; % Number of Grid Points 13 | x = linspace(0,Lx,M); 14 | 15 | % Calculated Values 16 | dx = Lx/(M-1); % Grid Size in x-direction 17 | Dt = [1e-1,1e-2,1e-3,1e-4]; % Time Steps 18 | NI = zeros(size(Dt)); % Number of iterations taken for each time step 19 | Err = zeros(size(Dt)); % Error from each time step; 20 | 21 | for j=1:length(Dt) 22 | dt = Dt(j); % Time Step 23 | lambda = C*dt/dx; % Courant Number 24 | 25 | % Initializing Velocity Matrix 26 | U = ones(1,M); 27 | 28 | % Applying given conditions 29 | U(1,5:13) = 2; 30 | U(1,1) = 1; 31 | 32 | % Computation - Explicit Method (First Upwind Difference Method) 33 | U_initial = U; 34 | 35 | t = 0; % Time 36 | n = 0; % Iterator 37 | while t<=0.4 38 | err = 0; % Error 39 | U_old = U; 40 | for i=2:M 41 | U(1,i) = (1-lambda)*U_old(1,i)+lambda*U_old(1,i-1); 42 | err = err + power(U(1,i)-U_old(1,i),2); % Error 43 | end 44 | err = sqrt(err/M); % Error calculation 45 | t = t+dt; 46 | n = n+1; % Iterator 47 | end 48 | NI(j) = n; 49 | Err(j) = err; 50 | 51 | % Plotting 52 | plot(x,U,'LineWidth',1) 53 | hold on; 54 | axis([0 1 1 2]) 55 | end 56 | 57 | % Plotting 58 | plot(x,U_initial,'black','LineWidth',2) 59 | xlabel('Length (x)'),ylabel('Velocity (U)') 60 | title('Velocity Profile Comparison') 61 | legend('Time Step : 0.1','Time Step : 0.01','Time Step : 0.001','Time Step : 0.0001','Original Profile','Location',"northeastoutside") 62 | hold off; 63 | -------------------------------------------------------------------------------- /FVM/1D_Heat_Conduction/1D_Heat_Conduction_with_Source.m: -------------------------------------------------------------------------------- 1 | %Steady State 1D Heat Conduction with Source -> using Finite Volume Method (FVM) 2 | 3 | clear all; clc; 4 | 5 | %Following SI Units 6 | 7 | %Known Values 8 | L = 0.02; %Length of the domain (or rod in this case) 9 | n = 5; %Number of Control Volumes(CVs) considered 10 | dx = L/n; %Grid Spacing %Assumed Uniform Grid spacing 11 | 12 | %Thermal Conductivity - Assumed constant throught the domain 13 | k = 0.5; 14 | ke = 0.5; 15 | kw = 0.5; 16 | q = 1000*1000; %Uniform Heat Generation 17 | 18 | %Boundary Conditions 19 | %Temperature - in degree celsius 20 | TA = 100; 21 | TB = 200; 22 | 23 | %Calculated Constants 24 | aw = kw/dx; 25 | ae = ke/dx; 26 | 27 | %Mesh Creation 28 | x = dx/2:dx:L; 29 | N = length(x); 30 | 31 | %Matrices Creation 32 | A = zeros(N,N); %Coefficient Matrix 33 | b = zeros(N,1); %Constants' Matrix 34 | 35 | %Algorithm 36 | for i=1:N 37 | if x(i) == min(x) %For Boundary Point-1 (Left most Node) 38 | A(i,i) = ae + 2*(kw/dx); 39 | A(i,i+1) = -ae; 40 | b(i,1) = q*dx + 2*(kw/dx)*TA; 41 | elseif x(i) == max(x) %For Boundary Point-2 (Right most Node) 42 | A(i,i) = aw + 2*(ke/dx); 43 | A(i,i-1) = -aw; 44 | b(i,1) = q*dx + 2*(ke/dx)*TB; 45 | else %For all Internal Nodes 46 | A(i,i) = ae + aw; 47 | A(i,i+1) = -ae; 48 | A(i,i-1) = -aw; 49 | b(i,1) = q*dx; 50 | end 51 | end 52 | 53 | %Final results 54 | A %Coefficient Tri-Diagonal Matrix 55 | b %Constant Matrix 56 | T = A\b %Displays the Temperature matrix after solving 57 | 58 | %Plots and comparison between Analytical/Exact & Numerical solution curves 59 | y = linspace(0,L,500); 60 | Te = TA + y.*(((TB-TA)/L)+(q/(2*k)).*(L-y)); %Exact solution (Analytical) for Temperature 61 | plot(x,T,'o-',y,Te,'r') 62 | xlabel('Length') 63 | ylabel('Variation in T') 64 | legend('Numerical solution','Exact/Analytical Solution') 65 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_1/Heat_Conduction_Convection.m: -------------------------------------------------------------------------------- 1 | % 2D Heat Conduction & Convection Example-1 2 | clear all; clc 3 | 4 | % Known Parameters 5 | Lx = 1; % Length of X-Domain 6 | Ly = 1; % Length of Y-Domain 7 | M = 21; % Number of grid points starting from 1 in x-direction 8 | N = 21; % Number of grid points starting from 1 in y-direction 9 | h = 10; % Convective Heat Transfer Coefficient (W/K/m^2) 10 | T_inf = 300; % Free air Temperature T_infi=300K 11 | k = 0.8; % Thermal Conductivity of the wall (W/K/m) 12 | 13 | % Calculated constants 14 | dx = Lx/(M-1); % Grid size along x-direction 15 | dy = Ly/(N-1); % Grid size along y-direction 16 | B = dx/dy; 17 | 18 | % Coefficients 19 | ap = 2*((1/(dx^2))+(1/(dy^2))); 20 | ae = 1/(dx^2); 21 | aw = 1/(dx^2); 22 | an = 1/(dy^2); 23 | as = 1/(dy^2); 24 | 25 | % Initializing Temperature matrix 26 | T = zeros(N,M); 27 | 28 | % Boundary Conditions 29 | T(:,1) = 500; % Left Boundary T=500K 30 | T(N,:) = 500; % Top Boundary T=500K 31 | T(:,M) = 500; % Right Boundary T=500K 32 | 33 | % Computation 34 | epsilon = 1e-8; % Error Sensivity 35 | err = 1; % Error 36 | n = 0; % Iterator 37 | 38 | while err>epsilon 39 | err = 0; % Error 40 | for j=2:N-1 % For Internal Grid points 41 | for i=2:M-1 % For Internal Grid points 42 | T_old = T(j,i); 43 | T(j,i) = (1/ap)*(ae*T(j+1,i) + aw*T(j-1,i) + an*T(j,i+1) + as*T(j,i-1)); 44 | err = err + power(T(j,i)-T_old,2); 45 | end 46 | end 47 | for i=2:M-1 % Bottom Neumann Boundary Condition 48 | T(1,i) = (4*T(2,i)-T(3,i)+(2*dy*(h/k))*T_inf)/(3+(2*dy*(h/k))); 49 | end 50 | err = sqrt(err/(M*N)); 51 | n = n+1; % Iterator 52 | end 53 | T 54 | n 55 | err 56 | 57 | % Plotting 58 | x = linspace(0,Lx,M); 59 | y = linspace(0,Ly,N); 60 | [X,Y] = meshgrid(x,y); 61 | contourf(X,Y,T,'ShowText','on'),colorbar 62 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 63 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_4/PSOR_method.m: -------------------------------------------------------------------------------- 1 | % Temperature Variation Example-4 using PSOR Method 2 | 3 | % Known Parameters 4 | Lx = 1; % Length of domain along x-direction 5 | Ly = 1; % Length of domain along y-direction 6 | M = 41; % Number of grid points starting from 1 in x-direction 7 | N = 41; % Number of grid points starting from 1 in y-direction 8 | 9 | % Calculated constants 10 | dx = Lx/(M-1); % Grid size along x-direction 11 | dy = Ly/(N-1); % Grid size along y-direction 12 | 13 | % Finding Optimum Relaxation Factor 14 | B = dx/dy; 15 | a = ((cos(pi/(M-1))+(B^2)*cos(pi/(N-1)))/(1+(B^2)))^2; 16 | w = (2-sqrt(1-a))/a; % Optimum Relaxation Factor 17 | 18 | % Initializing Temperature Matrix 19 | T = zeros(N,M); 20 | 21 | % Boundary Conditions 22 | T(N,:) = 100; % Bottom Boundary T = 100 C 23 | T(1,:) = 0; % Top Boundary T = 0 C 24 | T(:,1) = 100; % Left Boundary T = 100 C 25 | T(:,M) = 0; % Right Boundary T = 0 C 26 | 27 | % Computation 28 | epsilon = 1e-8; % Error Sensivity 29 | err = 1; % Error 30 | n = 0; % Iterator 31 | 32 | while err>epsilon 33 | for j=1:N 34 | for i=1:M 35 | T_old(j,i) = T(j,i); 36 | end 37 | end 38 | for j=2:N-1 % For Internal Grid points 39 | for i=2:M-1 % For Internal Grid points 40 | % T(j,i)_(k+1)th_iteration Calculation 41 | T(j,i) = (1-w)*T_old(j,i)+(w/(2*(1+B^2)))*((B^2)*T(j,i-1)+T(j-1,i)+T_old(j,i+1)+(B^2)*T_old(j+1,i)); 42 | end 43 | end 44 | err = 0; % Error 45 | for j=1:N 46 | for i=1:M 47 | err = err + power(T(j,i)-T_old(j,i),2); 48 | end 49 | end 50 | err = sqrt(err/(M*N)); 51 | n = n+1; % Iterator 52 | end 53 | w 54 | T 55 | n 56 | err 57 | 58 | % Plotting 59 | x = linspace(0,Lx,M); 60 | y = linspace(Ly,0,N); 61 | [X,Y] = meshgrid(x,y); 62 | contourf(X,Y,T,'ShowText','on'),colorbar 63 | set(gca, 'XTick',0:1:Lx) 64 | xlabel('X'),ylabel('Y'),title('Temperature Contour Plot') 65 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_3/PSOR_2D_Heat_Conduction.m: -------------------------------------------------------------------------------- 1 | % Temperature Variation Example-3 using PSOR Method 2 | 3 | % Known Parameters 4 | Lx = 2; % Length of domain along x-direction 5 | Ly = 1; % Length of domain along y-direction 6 | M = 41; % Number of grid points starting from 1 in x-direction 7 | N = 21; % Number of grid points starting from 1 in y-direction 8 | 9 | % Calculated constants 10 | dx = Lx/(M-1); % Grid size along x-direction 11 | dy = Ly/(N-1); % Grid size along y-direction 12 | 13 | % Finding Optimum Relaxation Factor 14 | B = dx/dy; 15 | a = ((cos(pi/(M-1))+(B^2)*cos(pi/(N-1)))/(1+(B^2)))^2; 16 | w = (2-sqrt(1-a))/a; % Optimum Relaxation Factor 17 | 18 | % Initializing Temperature Matrix 19 | T = zeros(N,M); 20 | 21 | % Boundary Conditions 22 | T(N,:) = 200; % Bottom Boundary T = 200R (R - Rankine) 23 | T(1,:) = 0; % Top Boundary T = 0R 24 | T(:,1) = 0; % Left Boundary T = 0R 25 | T(:,M) = 0; % Right Boundary T = 0R 26 | 27 | % Computation 28 | epsilon = 1e-8; % Error Sensivity 29 | err = 1; % Error 30 | n = 0; % Iterator 31 | 32 | while err>epsilon 33 | for j=1:N 34 | for i=1:M 35 | T_old(j,i) = T(j,i); 36 | end 37 | end 38 | for j=2:N-1 % For Internal Grid points 39 | for i=2:M-1 % For Internal Grid points 40 | % T(j,i)_(k+1)th_iteration Calculation 41 | T(j,i) = (1-w)*T_old(j,i)+(w/(2*(1+B^2)))*((B^2)*T(j,i-1)+T(j-1,i)+T_old(j,i+1)+(B^2)*T_old(j+1,i)); 42 | end 43 | end 44 | err = 0; % Error 45 | for j=1:N 46 | for i=1:M 47 | err = err + power(T(j,i)-T_old(j,i),2); 48 | end 49 | end 50 | err = sqrt(err/(M*N)); 51 | n = n+1; % Iterator 52 | end 53 | w 54 | T 55 | n 56 | err 57 | 58 | % Plotting 59 | x = linspace(0,Lx,M); 60 | y = linspace(Ly,0,N); 61 | [X,Y] = meshgrid(x,y); 62 | contourf(X,Y,T,'ShowText','on'),colorbar 63 | set(gca, 'XTick',0:1:Lx) 64 | xlabel('X'),ylabel('Y'),title('Temperature Contour Plot') 65 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_2/Unsteady_Convection_1D.m: -------------------------------------------------------------------------------- 1 | % Example-2 : 1D Linear Convection 2 | clear all; clc 3 | 4 | % Velocity is 2m/s in the range x=0.1 to x=0.3 and is 5 | % 1m/s everywhere else. 6 | Xs = 0.1; % Starting point of square wave 7 | Xe = 0.3; % Ending point of square wave 8 | 9 | % Simulating based on different grid sizes 10 | 11 | % Known Values 12 | Lx = 1; % Length of x-domain 13 | C = 1; % Velocity (1 m/s) 14 | M = [21,41,81,161]; % Number of Grid Points 15 | Err = zeros(size(M)); % Error from each grid size 16 | 17 | for j=1:length(M) 18 | % Calculated Values 19 | dx = Lx/(M(j)-1); % Grid Size for each "M" 20 | dt = 0.01; % Time Step 21 | x = linspace(0,Lx,M(j)); % X-Domain 22 | lambda = C*dt/dx; % Courant Number 23 | 24 | % Initializing Velocity matrix 25 | U = ones(1,M(j)); 26 | 27 | % Applying Given conditions 28 | Ms(j) = round((Xs/dx)+1); % Starting node for square wave 29 | Me(j) = round((Xe/dx)+1); % Ending node for square qave 30 | U(:,Ms(j):Me(j)) = 2; % Velocity of wave in x=0.1 to x=0.3 range 31 | 32 | U_initial = U; % Initial Velocity 33 | 34 | % Computation 35 | t = 0; % Time 36 | n = 0; % Iterator 37 | while t<=0.4 38 | err = 0; % Error 39 | U_old = U; 40 | for i=2:M(j) 41 | U(1,i) = (1-lambda)*U_old(1,i)+lambda*U_old(1,i-1); 42 | err = err + power(U(1,i)-U_old(1,i),2); % Error 43 | end 44 | err = sqrt(err/M(j)); % Error calculation 45 | t = t+dt; 46 | n = n+1; % Iterator 47 | 48 | % Plotting 49 | figure(j); 50 | plot(x,U_initial,'r-') 51 | %axis([0 1 0 3]) 52 | hold on 53 | plot(x,U,'b-') 54 | xlabel('Length (x)'),ylabel('Velocity (U)') 55 | title('Velocity Profile') 56 | legend('Exact Numerical Velocity Profile','Numerical Velocity Profile','Location','northeastoutside') 57 | end 58 | Err(j) = err; % Error for each gid size variation 59 | end 60 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_4/Couette_Flow.m: -------------------------------------------------------------------------------- 1 | % Example-4 : 1D Flow Variation using Crank-Nicolson Scheme 2 | clear all; clc 3 | 4 | % Known parameters 5 | h = 1; % Distance between the plates 6 | N = 101; % No. of grid points considered 7 | dy = h/(N-1); % Grid Size 8 | U_m = 0; % Velocity of Moving Plate 9 | v = 0.01; % v = Uh/Re; Re = reynolds number (m^2/s) 10 | dt = 5e-3; % Time Step 11 | gamma_y = v*dt/(dy^2); % Diffusion Factor Coefficient 12 | y = 0:dy:h; % Y-domain 13 | 14 | % Initialization 15 | t = 0; % Time 16 | u = ones(N,1); % Velocity Matrix 17 | U = []; % Velocities at particular time steps 18 | T = []; % Particular Time Steps 19 | n = 0; % Iterator 20 | 21 | % Initial Condition 22 | for j=1:N 23 | u(j,1) = sin(pi*y(j)); 24 | end 25 | 26 | % Computation - Implicit Method 27 | % Crank Nicolson Method 28 | while t<=50 29 | if t>0 30 | u(N,1) = 0; % Stationary Plate Boundary Condition 31 | u(1,1) = U_m; % Moving Plate Boundary Condition 32 | u_old = u; % u_old at nth time step & u at (n+1)th time step 33 | for i=2:N-1 34 | u(i,1) = ((gamma_y/(2+2*gamma_y))*(u(i+1,1)+u(i-1,1)+u_old(i+1,1)+u_old(i-1,1)))+((1-gamma_y)/(1+gamma_y))*u_old(i,1); 35 | end 36 | end 37 | U = [U;u']; 38 | T(end+1) = t; 39 | t = t+dt; % Time 40 | n = n+1; % Iterator 41 | end 42 | %u 43 | n 44 | t 45 | 46 | % Plotting 47 | figure; 48 | hold on; 49 | grid on; 50 | plot(u,h:-dy:0) 51 | for i=1:50:n 52 | plot(U(i,:),h:-dy:0,'b-') 53 | end 54 | xlabel('Velocity (u)'),ylabel('Height (Y)') 55 | title('Flow between 2 parallel plates') 56 | hold off; 57 | 58 | % Animation 59 | figure; 60 | myVideo = VideoWriter('CouetteFlow'); %open video file 61 | myVideo.FrameRate = 10; 62 | open(myVideo) 63 | 64 | y = h:-dy:0; 65 | for i=1:100:n 66 | plot(U(i,:),y,'b-') 67 | % hold on; 68 | axis([0 1 0 h]) 69 | pause(0.2) 70 | xlabel('U (in m/s)'),ylabel('Y (in m)') 71 | frame = getframe(gcf); 72 | writeVideo(myVideo,frame); 73 | end 74 | close(myVideo) 75 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_3/Couette_Flow.m: -------------------------------------------------------------------------------- 1 | % 1D Couette Flow Example-3 using Crank-Nicolson Scheme 2 | % Fixed Plate -> Bottom Plate 3 | % Moving Plate -> Top Plate 4 | clear all; clc 5 | 6 | % Known parameters 7 | h = 5; % Distance between the plates (5 m) 8 | N = 51; % No. of grid points considered 9 | dy = h/(N-1); % Grid Size 10 | U_m = 100; % Velocity of Moving Plate (100 m/s) 11 | v = 1; % v = Uh/Re; Re = reynolds number (m^2/s) 12 | dt = 5e-3; % Time Step 13 | gamma_y = v*dt/(dy^2); % Diffusion Factor Coefficient 14 | 15 | % Initialization 16 | t = 0; % Time 17 | u = ones(N,1); % Velocity Matrix 18 | U = []; % Velocities at particular time steps 19 | T = []; % Particular Time Steps 20 | n = 0; % Iterator 21 | 22 | % Initial & Boundary Conditions 23 | u = 20*u; % Initial Condition 24 | 25 | % Computation - Implicit Method 26 | % Crank Nicolson Method 27 | while t<=10 28 | if t>0 29 | u(N,1) = 0; % Stationary Plate Boundary Condition 30 | u(1,1) = U_m; % Moving Plate Boundary Condition 31 | u_old = u; % u_old at nth time step & u at (n+1)th time step 32 | for i=2:N-1 33 | u(i,1) = ((gamma_y/(2+2*gamma_y))*(u(i+1,1)+u(i-1,1)+u_old(i+1,1)+u_old(i-1,1)))+((1-gamma_y)/(1+gamma_y))*u_old(i,1); 34 | end 35 | end 36 | U = [U;u']; 37 | T(end+1) = t; 38 | t = t+dt; % Time 39 | n = n+1; % Iterator 40 | end 41 | %u 42 | n 43 | t 44 | 45 | % Plotting 46 | figure; 47 | hold on; 48 | grid on; 49 | plot(u,h:-dy:0) 50 | for i=1:50:n 51 | plot(U(i,:),h:-dy:0,'b-') 52 | end 53 | xlabel('Velocity (u)'),ylabel('Height (Y)') 54 | title('Flow between 2 parallel plates') 55 | hold off; 56 | 57 | % Animation 58 | figure; 59 | myVideo = VideoWriter('CouetteFlow'); %open video file 60 | myVideo.FrameRate = 10; 61 | open(myVideo) 62 | 63 | y = h:-dy:0; 64 | for i=1:25:n 65 | plot(U(i,:),y,'b-') 66 | % hold on; 67 | axis([0 U_m 0 h]) 68 | pause(0.2) 69 | xlabel('U (in m/s)'),ylabel('Y (in m)') 70 | frame = getframe(gcf); 71 | writeVideo(myVideo,frame); 72 | end 73 | close(myVideo) 74 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_1/Jacobi_Method.m: -------------------------------------------------------------------------------- 1 | % Stream Function Example-1 using Jacobi Method 2 | 3 | % Known Parameters 4 | Lx = 6; % Length of domain along x-direction 5 | Ly = 4; % Length of domain along y-direction 6 | M = 31; % Number of grid points starting from 1 in x-direction 7 | N = 21; % Number of grid points starting from 1 in y-direction 8 | 9 | % Calculated constants 10 | dx = Lx/(M-1); % Grid size along x-direction 11 | dy = Ly/(N-1); % Grid size along y-direction 12 | % Coefficients 13 | ap = 2*((1/(dx^2))+(1/(dy^2))); 14 | ae = 1/(dx^2); 15 | aw = 1/(dx^2); 16 | an = 1/(dy^2); 17 | as = 1/(dy^2); 18 | 19 | % Initializing Stream Function Matrix 20 | psi_new = zeros(N,M); 21 | psi_old = zeros(N,M); 22 | 23 | % Boundary Conditions 24 | psi_new(N,1:6) = 0; % Bottom Boundary 25 | psi_new(N,7:M) = 100; % Bottom Boundary 26 | psi_new(:,1) = 0; % Left Boundary 27 | psi_new(1,:) = 0; % Top Boundary 28 | 29 | % Computation 30 | epsilon = 1e-8; % Error Sensivity 31 | err = 1; % Error 32 | n = 0; % Iterator 33 | 34 | while err>epsilon 35 | for j=1:N 36 | for i=1:M 37 | psi_old(j,i) = psi_new(j,i); 38 | end 39 | end 40 | for j=2:N-1 % For Internal Grid points 41 | for i=2:M-1 % For Internal Grid points 42 | % Psi(j,i)_(k+1)th_iteration Calculation 43 | psi_new(j,i) = (1/ap)*(ae*psi_old(j+1,i) + aw*psi_old(j-1,i) + an*psi_old(j,i+1) + as*psi_old(j,i-1)); 44 | end 45 | end 46 | for j=1:N % Right Neumann Boundary Condition 47 | psi_new(j,M) = psi_new(j,M-1); 48 | end 49 | err = 0; % Error 50 | for j=1:N 51 | for i=1:M 52 | err = err + power(psi_new(j,i)-psi_old(j,i),2); 53 | end 54 | end 55 | err = sqrt(err/(M*N)); 56 | n = n+1; % Iterator 57 | end 58 | psi_new 59 | n 60 | err 61 | 62 | % Plotting 63 | x = linspace(0,Lx,M); 64 | y = linspace(Ly,0,N); 65 | [X,Y] = meshgrid(x,y); 66 | contourf(X,Y,psi_new,'ShowText','on'),colorbar 67 | set(gca, 'XTick',0:1:Lx) 68 | xlabel('X'),ylabel('Y'),title('Stream Function(PSI) Contour Plot') 69 | -------------------------------------------------------------------------------- /FVM/1D_Heat_Convection/1D_Heat_Convection_and_Diffusion.m: -------------------------------------------------------------------------------- 1 | % 1D Heat Convection & Diffusion -> using Finite Volume Method (FVM) 2 | 3 | clear all; clc; 4 | 5 | %Following SI Units 6 | 7 | %Known Values 8 | L = 1; %Length of the domain 9 | n = 5; %Number of Control Volumes 10 | dx = L/n; %Grid Spacing; %Assumed Uniform grid spacing 11 | rho = 1; %Density of the bulk 12 | gamma = 0.1; %Viscocity 13 | u = 0.1; %Velocity of the bulk fluid 14 | 15 | %Boundary Conditions 16 | %Phi => some parameter 17 | PhiA = 1; %At left boundary 18 | PhiB = 0; %At right boundary 19 | 20 | %Calculated constants 21 | Dw = Gamma/dx; De = Gamma/dx; 22 | Fw = rho * u; Fe = rho * u; 23 | Area = dx * dx; 24 | aw = Dw + (Fw/2); ae = De - (Fe/2); 25 | 26 | %Create Mesh 27 | x = dx/2:dx:L; 28 | N = length(x); 29 | 30 | %Matrix creation 31 | A = zeros(N,N); 32 | b = zeros(N,1); 33 | 34 | %Algorithm 35 | for i = 1:N 36 | if x(i) == min(x) %For Boundary Point-1 (left boundary) 37 | A(i,i) = ae + ((2*Dw)+Fw) + (Fe-Fw); 38 | A(i,i+1) = -ae; 39 | b(i,1) = ((2*Dw)+Fw)*PhiA; 40 | elseif x(i) == max(x) %For Boundary Point-2 (right boundary) 41 | A(i,i) = aw + ((2*De)-Fe) + (Fe-Fw); 42 | A(i,i-1) = -aw; 43 | b(i,1) = ((2*De)-Fe)*PhiB; 44 | else %For Internal Nodes 45 | A(i,i) = ae + aw + (Fe - Fw); 46 | A(i,i+1) = -ae; 47 | A(i,i-1) = -aw; 48 | end 49 | end 50 | 51 | %Final Results 52 | %Final results 53 | A %Coefficient Tri-Diagonal Matrix 54 | b %Constant Matrix 55 | Phi = A\b %Displays the Phi matrix after solving 56 | 57 | %Plots and comparison between Analytical/Exact & Numerical solution curve 58 | y=linspace(0,1,500); 59 | Phi_e=1-((exp(rho*u*y/Gamma)-1)/(exp(rho*u*L/Gamma)-1)); %Exact solution (Analytical) for Phi 60 | plot(x,Phi,'o-',y,Phi_e,'r') 61 | xlabel('length') 62 | ylabel('Phi') 63 | legend('Numerical value','Exact/Analytical value') 64 | 65 | %Below code gives only the plot of the curve obtained from Numerival solution 66 | % plot([0,x,L],[PhiA;Phi;PhiB],'o-b') 67 | % title('Property Transported by Convetion and Diffusion') 68 | % xlabel('meters (m)') 69 | % ylabel('Phi') 70 | -------------------------------------------------------------------------------- /FDM/Parabolic/Couette_Flow_Examples/Example_5/Couette_Flow.m: -------------------------------------------------------------------------------- 1 | % Example-5 : 1D Flow Variation using Crank-Nicolson Scheme 2 | clear all; clc 3 | 4 | % Known parameters 5 | h = 1; % Distance between the plates 6 | N = 101; % No. of grid points considered 7 | dy = h/(N-1); % Grid Size 8 | U_m = 0; % Velocity of Moving Plate 9 | v = 0.01; % v = Uh/Re; Re = reynolds number (m^2/s) 10 | dt = 5e-3; % Time Step 11 | gamma_y = v*dt/(dy^2); % Diffusion Factor Coefficient 12 | y = 0:dy:h; % Y-domain 13 | 14 | % Initialization 15 | t = 0; % Time 16 | u = ones(N,1); % Velocity Matrix 17 | U = []; % Velocities at particular time steps 18 | T = []; % Particular Time Steps 19 | n = 0; % Iterator 20 | 21 | % Initial Condition 22 | for j=1:N 23 | if(0<=y(j) && y(j)<=(1/2)) 24 | u(j,1) = 2*y(j); 25 | elseif((1/2)<=y(j) && y(j)<=1) 26 | u(j,1) = 2*(1-y(j)); 27 | end 28 | end 29 | 30 | % Computation - Implicit Method 31 | % Crank Nicolson Method 32 | while t<=20 33 | if t>0 34 | u(N,1) = 0; % Stationary Plate Boundary Condition 35 | u(1,1) = U_m; % Moving Plate Boundary Condition 36 | u_old = u; % u_old at nth time step & u at (n+1)th time step 37 | for i=2:N-1 38 | u(i,1) = ((gamma_y/(2+2*gamma_y))*(u(i+1,1)+u(i-1,1)+u_old(i+1,1)+u_old(i-1,1)))+((1-gamma_y)/(1+gamma_y))*u_old(i,1); 39 | end 40 | end 41 | U = [U;u']; 42 | T(end+1) = t; 43 | t = t+dt; % Time 44 | n = n+1; % Iterator 45 | end 46 | %u 47 | n 48 | t 49 | 50 | % Plotting 51 | %figure; 52 | %hold on; 53 | %grid on; 54 | %plot(u,h:-dy:0) 55 | %for i=1:50:n 56 | % plot(U(i,:),h:-dy:0,'b-') 57 | %end 58 | %xlabel('Velocity (u)'),ylabel('Height (Y)') 59 | %title('Flow between 2 parallel plates') 60 | %hold off; 61 | 62 | % Animation 63 | figure; 64 | myVideo = VideoWriter('CouetteFlow'); %open video file 65 | myVideo.FrameRate = 10; 66 | open(myVideo) 67 | 68 | y = h:-dy:0; 69 | for i=1:25:n 70 | plot(U(i,:),y,'b-') 71 | % hold on; 72 | axis([0 1 0 h]) 73 | pause(0.2) 74 | xlabel('U (in m/s)'),ylabel('Y (in m)') 75 | frame = getframe(gcf); 76 | writeVideo(myVideo,frame); 77 | end 78 | close(myVideo) 79 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/Lax Method/Lax_Method.m: -------------------------------------------------------------------------------- 1 | % Example-5 : Non-Linear Wave Equation - 1D 2 | % Lax Method 3 | clear all; clc 4 | 5 | % Known Values 6 | Lx = 4; % Length of x-domain 7 | M = 81; % Number of grid points 8 | x = linspace(0,Lx,M); % x-Domain 9 | U_max = 1; % Max Velocity 10 | 11 | % Calculated Values 12 | dx = Lx/(M-1); % Grid Size in x-direction 13 | Dt = [dx,dx/2]; % Time step chosen so in order to obey |(dt/dx)*U_max| <= 1 for stability 14 | T = [0.5,1.0,1.5,2.0]; % Time Intervals chosen 15 | 16 | % Computation 17 | for j=1:length(Dt) 18 | dt = Dt(j); % Time Step 19 | N = T./dt; % Iteration Number at each time interval 20 | 21 | % Initializing Velocity Matrix 22 | U = zeros(1,M); 23 | % Velocity Matrices 24 | U_array = []; 25 | 26 | % Applying given initial & boundary conditions 27 | for p=1:M 28 | if((0<=x(p))&&(x(p)<=2)) 29 | U(1,p) = 1; 30 | end 31 | if((2<=x(p))&&(x(p)<=4)) 32 | U(1,p) = 0; 33 | end 34 | end 35 | 36 | % Initial Velocity Profile 37 | U_initial = U; 38 | 39 | % Computation using Lax Method (FTCS) 40 | t = 0; % Time 41 | n = 0; % Iterator 42 | while t<=2 43 | U_old = U; 44 | for i=2:M-1 45 | U(1,i) = ((U_old(1,i+1)+U_old(1,i-1))/2)-(dt/(4*dx))*((U_old(1,i+1)^2)-(U_old(1,i-1)^2)); 46 | end 47 | t = t+dt; 48 | n = n+1; % Iterator 49 | U_array = [U_array;U]; % Updating U_array 50 | end 51 | 52 | % Plotting 53 | figure; 54 | plot(x,U_initial,'k',"LineWidth",2) 55 | hold on; 56 | grid on; 57 | ylim([-0.5 1.5]) 58 | xlabel('Length (x)'),ylabel('Velocity (U)') 59 | title('Velocity Profile - Lax Method',strcat('Time Step :',num2str(dt))) 60 | set(gca,'xtick',[0:1:Lx]) 61 | set(gca,'ytick',[-0.5:0.5:1.5]) % U_max = 1 62 | 63 | for k=1:n 64 | for p=1:length(N) 65 | if k==N(p) 66 | plot(x,U_array(k,:),'color',[0.9100 0.4100 0.1700]) 67 | end 68 | end 69 | end 70 | legend('T = 0.0 sec',strcat('T =',num2str(T(1))),strcat('T =',num2str(T(2))),strcat('T =',num2str(T(3))),strcat('T =',num2str(T(4))),'Location','bestoutside') 71 | hold off; 72 | end 73 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_4/Hyperbolic_2nd_order_Wave.m: -------------------------------------------------------------------------------- 1 | % Example-4 : 2nd Order Wave Equation - 1D 2 | % Solving using Mid-Point Leap Frog Method 3 | clear all; clc 4 | 5 | % Known Values 6 | Lx = 300; % Length of x-domain 7 | a = 300; % Velocity (in m/s) 8 | M = 61; % Number of grid points 9 | x = linspace(0,Lx,M); 10 | 11 | % Calculated values 12 | dx = Lx/(M-1); % Grid size 13 | Dt = [0.01666,0.015,0.0075]; % Time Steps 14 | CL = zeros(size(Dt)); % Courant Number for each step 15 | 16 | % Initial Conditions (@ t=0) 17 | % U(x) = 0 for 0<=x<=20 18 | % U(x) = 100*sin(pi*(x-20)/120) for 20<=x<=140 19 | % U(x) = 0 for 140<=x<=180 20 | % U(x) = 100*sin(pi*(x-180)/120) for 180<=x<=300 21 | % dU/dt = 0 22 | 23 | % Boundary Conditions 24 | % U(x,t) = 0 for x = 0 25 | % U(x,t) = 0 for x = Lx 26 | 27 | % Velocity Matrices 28 | U_array = []; 29 | 30 | % Computation 31 | for j=1:length(Dt) 32 | dt = Dt(j); % Time Step 33 | lambda = a*dt/dx; % Courant Number 34 | CL(j) = lambda; % Courant Number for each time step 35 | 36 | % Initializing Velocity Matrix 37 | U = zeros(1,M); % U for 38 | 39 | % Applying given conditions 40 | for p=1:M 41 | if((20<=x(p))&&(x(p)<=140)) 42 | U(1,p) = 100*sin(pi*(x(p)-20)/120); 43 | end 44 | if((180<=x(p))&&(x(p)<=300)) 45 | U(1,p) = 100*sin(pi*(x(p)-180)/120); 46 | end 47 | end 48 | 49 | % Initial Velocity Profile 50 | U_initial = U; 51 | 52 | % Computation using Mid-Point Leap Frog Method 53 | t = 0; % Time 54 | while t<=0.28 55 | U_old = U; 56 | for i=2:M-1 57 | U(1,i) = U_old(1,i)+((lambda^2)/2)*(U_old(1,i-1)-2*U_old(1,i)+U_old(1,i+1)); 58 | end 59 | t = t+dt; 60 | end 61 | U_array = [U_array;U]; % Updating U_array for each time step chosen 62 | end 63 | 64 | % Plotting 65 | figure; 66 | plot(x,U_initial,'k') 67 | hold on; 68 | grid on; 69 | plot(x,U_array) 70 | ylim([-10 110]) 71 | xlabel('Length (x)'),ylabel('Velocity (U)') 72 | title('Velocity Profile','Time = 0.28') 73 | set(gca,'xtick',[0:50:Lx]) 74 | set(gca,'ytick',[0:20:100]) 75 | legend('Exact (C=1)',strcat('C =',num2str(CL(1))),strcat('C =',num2str(CL(2))),strcat('C =',num2str(CL(3))),'Location','bestoutside') 76 | hold off; 77 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_7/Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % Example-7 : 2D Heat Conduction & Convection 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 0.2; % Length of x-domain 6 | Ly = 0.1; % Length of y-domain 7 | M = 41; % Number of Grid Points along x-direction 8 | N = 21; % Number of Grid Points along y-direction 9 | hg = 1000; % Heat Convection Coefficient of gases 10 | Tg = 2000; % Temperature of gases 11 | hb = 8000; % Heat Convection Coefficient of coolant 12 | Tb = 60; % Temperature of coolant 13 | k = 20; % Thermal Conductivity 14 | 15 | % Calculated Values 16 | dx = Lx/(M-1); % Grid size in x-direction 17 | dy = Ly/(N-1); % Grid size in y-direction 18 | B = dx/dy; 19 | w = 1.9; % Relaxation Factor 20 | 21 | % Initializing Temperature Matrix 22 | T = zeros(N,M); 23 | 24 | % Computation 25 | epsilon = 1e-8; % Error Sensivity 26 | err = 1; % Error 27 | n = 0; % Iterator 28 | 29 | while err>epsilon 30 | % Applying Boundary Conditions 31 | for i=1:M % Top Boundary Condition (Convective) 32 | T(1,i) = (4*T(2,i)-T(3,i)+(2*dy*hg/k)*Tg)/(3+(2*dy*hg/k)); 33 | end 34 | for j=1:N 35 | T(j,1) = (4*T(j,2)-T(j,3))/3; % Left Boundary Condition (Adiabatic) 36 | T(j,M) = (4*T(j,M-1)-T(j,M-2))/3; % Right Boundary Condition (Adiabatic) 37 | end 38 | for i=1:21 39 | T(N,i) = (4*T(N-1,i)-T(N-2,i))/3; % Half Bottom Boundary Condition (Adiabatic) 40 | end 41 | for i=21:M 42 | T(N,i) = (4*T(N-1,i)-T(N-2,i)+(2*dy*hb/k)*Tb)/(3+(2*dy*hb/k)); 43 | end 44 | 45 | % Updating T_old i.e. T from previous iteration 46 | for j=1:N 47 | for i=1:M 48 | T_old(j,i) = T(j,i); 49 | end 50 | end 51 | % Updating T from current iteration 52 | for j=2:N-1 53 | for i=2:M-1 54 | T(j,i) = (1-w)*T_old(j,i)+(w/(2*(1+B^2)))*((B^2)*T(j,i-1)+T(j-1,i)+T_old(j,i+1)+(B^2)*T_old(j+1,i)); 55 | end 56 | end 57 | err = 0; % Error 58 | for j=1:N 59 | for i=1:M 60 | err = err + power(T(j,i)-T_old(j,i),2); 61 | end 62 | end 63 | err = sqrt(err/(M*N)); 64 | n = n+1; % Iterator 65 | end 66 | n 67 | err 68 | 69 | % Plotting 70 | x = linspace(0,Lx,M); 71 | y = linspace(Ly,0,N); 72 | [X,Y] = meshgrid(x,y); 73 | contourf(X,Y,T,'ShowText','on'),colorbar,colormap(jet) 74 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 75 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/Lax-Wendroff Method/Lax_Wendroff_method.m: -------------------------------------------------------------------------------- 1 | % Example-5 : Non-Linear Wave Equation - 1D 2 | % Lax-Wendroff Method 3 | clear all; clc 4 | 5 | % Known Values 6 | Lx = 4; % Length of x-domain 7 | M = 81; % Number of grid points 8 | x = linspace(0,Lx,M); % x-Domain 9 | U_max = 1; % Max Velocity 10 | 11 | % Calculated Values 12 | dx = Lx/(M-1); % Grid Size in x-direction 13 | Dt = [dx,dx/2]; % Time step chosen so in order to obey |(dt/dx)*U_max| <= 1 for stability 14 | T = [0.5,1.0,1.5,2.0]; % Time Intervals chosen 15 | 16 | % Computation 17 | for j=1:length(Dt) 18 | dt = Dt(j); % Time Step 19 | N = T./dt; % Iteration Number at each time interval 20 | 21 | % Initializing Velocity Matrix 22 | U = zeros(1,M); 23 | % Velocity Matrices 24 | U_array = []; 25 | 26 | % Applying given initial & boundary conditions 27 | for p=1:M 28 | if((0<=x(p))&&(x(p)<=2)) 29 | U(1,p) = 1; 30 | end 31 | if((2<=x(p))&&(x(p)<=4)) 32 | U(1,p) = 0; 33 | end 34 | end 35 | 36 | % Initial Velocity Profile 37 | U_initial = U; 38 | 39 | % Computation using Lax-Wendroff Method 40 | t = 0; % Time 41 | n = 0; % Iterator 42 | while t<=2 43 | U_old = U; 44 | F = (U.^2)/2; 45 | for i=2:M-1 46 | U(1,i) = U_old(1,i)-(dt/(2*dx))*(F(1,i+1)-F(1,i-1))+((dt^2)/(4*dx^2))*((U_old(1,i+1)+U_old(1,i))*(F(1,i+1)-F(1,i))-(U_old(1,i)+U_old(1,i-1))*(F(1,i)-F(1,i-1))); 47 | end 48 | t = t+dt; 49 | n = n+1; % Iterator 50 | U_array = [U_array;U]; % Updating U_array 51 | end 52 | 53 | % Plotting 54 | figure; 55 | plot(x,U_initial,'k',"LineWidth",2) 56 | hold on; 57 | grid on; 58 | ylim([-0.5 1.5]) 59 | xlabel('Length (x)'),ylabel('Velocity (U)') 60 | title('Velocity Profile - Lax-Wendroff Method',strcat('Time Step :',num2str(dt))) 61 | set(gca,'xtick',[0:1:Lx]) 62 | set(gca,'ytick',[-0.5:0.5:1.5]) % U_max = 1 63 | 64 | for k=1:n 65 | for p=1:length(N) 66 | if k==N(p) 67 | plot(x,U_array(k,:),'color',[0.9100 0.4100 0.1700]) 68 | end 69 | end 70 | end 71 | legend('T = 0.0 sec',strcat('T =',num2str(T(1))),strcat('T =',num2str(T(2))),strcat('T =',num2str(T(3))),strcat('T =',num2str(T(4))),'Location','bestoutside') 72 | hold off; 73 | end 74 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_5/MacCormak Method/MacCormak_Method.m: -------------------------------------------------------------------------------- 1 | % Example-5 : Non-Linear Wave Equation - 1D 2 | % MacCormack Method 3 | clear all; clc 4 | 5 | % Known Values 6 | Lx = 4; % Length of x-domain 7 | M = 81; % Number of grid points 8 | x = linspace(0,Lx,M); % x-Domain 9 | U_max = 1; % Max Velocity 10 | 11 | % Calculated Values 12 | dx = Lx/(M-1); % Grid Size in x-direction 13 | Dt = [dx,dx/2]; % Time step chosen so in order to obey |(dt/dx)*U_max| <= 1 for stability 14 | T = [0.5,1.0,1.5,2.0]; % Time Intervals chosen 15 | 16 | % Computation 17 | for j=1:length(Dt) 18 | dt = Dt(j); % Time Step 19 | N = T./dt; % Iteration Number at each time interval 20 | 21 | % Initializing Velocity Matrix 22 | U = zeros(1,M); 23 | % Velocity Matrices 24 | U_array = []; 25 | 26 | % Applying given initial & boundary conditions 27 | for p=1:M 28 | if((0<=x(p))&&(x(p)<=2)) 29 | U(1,p) = 1; 30 | end 31 | if((2<=x(p))&&(x(p)<=4)) 32 | U(1,p) = 0; 33 | end 34 | end 35 | 36 | % Initial Velocity Profile 37 | U_initial = U; 38 | 39 | Up = U; % Velocity for Predictor Step 40 | 41 | % Computation using MacCormak Method 42 | t = 0; % Time 43 | n = 0; % Iterator 44 | while t<=2 45 | U_old = U; 46 | for i=2:M-1 47 | % Predictor Step 48 | Up(1,i) = U_old(1,i)-(0.5*dt/dx)*(U_old(1,i+1).^2-U_old(1,i).^2); 49 | end 50 | for i=2:M-1 51 | % Corrector Step 52 | U(1,i) = (1/2)*(U_old(1,i)+Up(1,i)-(0.5*dt/dx)*(Up(1,i).^2-Up(1,i-1).^2)); 53 | end 54 | t = t+dt; 55 | n = n+1; % Iterator 56 | U_array = [U_array;U]; % Updating U_array 57 | end 58 | 59 | % Plotting 60 | figure; 61 | plot(x,U_initial,'k',"LineWidth",2) 62 | hold on; 63 | grid on; 64 | ylim([-0.5 1.5]) 65 | xlabel('Length (x)'),ylabel('Velocity (U)') 66 | title('Velocity Profile - MacCormak Method',strcat('Time Step :',num2str(dt))) 67 | set(gca,'xtick',[0:1:Lx]) 68 | set(gca,'ytick',[-0.5:0.5:1.5]) % U_max = 1 69 | 70 | for k=1:n 71 | for p=1:length(N) 72 | if k==N(p) 73 | plot(x,U_array(k,:),'color',[0.9100 0.4100 0.1700]) 74 | end 75 | end 76 | end 77 | legend('T = 0.0 sec',strcat('T =',num2str(T(1))),strcat('T =',num2str(T(2))),strcat('T =',num2str(T(3))),strcat('T =',num2str(T(4))),'Location','bestoutside') 78 | hold off; 79 | end 80 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_1/Unsteady_Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % 2D Unsteady Heat Conduction Example-1 2 | clear all; clc 3 | 4 | % Known Parameters 5 | Lx = 1; % Length of x-domain (1 m) 6 | Ly = 1; % Length of y-domain (1 m) 7 | alpha = 1.4; % Thermal Diffusivity 8 | M = 41; % Number of grid points along x-axis 9 | N = 41; % Number of grid points along y-axis 10 | 11 | % Calculated Constants 12 | dx = Lx/(M-1); % Grid Size in x-direction 13 | dy = Ly/(N-1); % Grid Size in y-direction 14 | dt = 1e-3; % Time Step 15 | gamma_x = alpha*dx/(dt^2); 16 | gamma_y = alpha*dy/(dt^2); 17 | 18 | % Initializing Temperature matrix 19 | T = zeros(N,M); 20 | T_array = {}; % Temperature Array to store T at each time step 21 | 22 | % Boundary Conditions 23 | T(:,1) = 400; % Left Wall Boundary Condition 24 | T(N,:) = 900; % Bottom Wall Boundary Condition 25 | T(:,M) = 800; % Right Wall Boundary Condition 26 | T(1,:) = 600; % Top Wall Boundary Condition 27 | 28 | % Computation 29 | n = 0; % Iterator 30 | t = 0; 31 | while t<=10 32 | err = 0; % Error 33 | for j=2:N-1 34 | for i=2:M-1 35 | T_temp = T(j,i); 36 | T(j,i) = (1/(1+2*gamma_x+2*gamma_y))*(gamma_x*(T(j+1,i)+T(j-1,i))+gamma_y*(T(j,i+1)+T(j,i-1))+T_temp); 37 | err = err + power(T(j,i)-T_temp,2); 38 | end 39 | end 40 | err = sqrt(err/(M*N)); 41 | %T_array = [T_array;T]; 42 | n = n+1; 43 | if n==10 || n==30 || n==50 || n==100 || n==150 || n==200 44 | T_array = [T_array;T]; 45 | elseif n==275 || n==350 || n==550 || n==750 || n==1000 || n==1250 46 | T_array = [T_array;T]; 47 | end 48 | t = t+dt; 49 | end 50 | %T 51 | n 52 | err 53 | 54 | % Plotting 55 | figure; 56 | x = linspace(0,Lx,M); 57 | y = linspace(Ly,0,N); 58 | [X,Y] = meshgrid(x,y); 59 | contourf(X,Y,T,':','ShowText','on'),colorbar,colormap(jet) 60 | set(gca, 'XTick',0:1:Lx) 61 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 62 | 63 | % Animation 64 | figure; 65 | myVideo = VideoWriter('Unsteady Heat COnduction'); %open video file 66 | myVideo.FrameRate = 5; 67 | open(myVideo) 68 | 69 | for k=1:12 70 | %figure; 71 | x = linspace(0,Lx,M); 72 | y = linspace(Ly,0,N); 73 | [X,Y] = meshgrid(x,y); 74 | TT = T_array(k); 75 | contourf(X,Y,TT{:,:},':','ShowText','on'),colorbar,colormap(jet) 76 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 77 | pause(0.02) 78 | frame = getframe(gcf); 79 | writeVideo(myVideo,frame); 80 | end 81 | close(myVideo) 82 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Unsteady_Examples/Example_2/Unsteady_Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % Unsteady Heat Conduction & Convection 2D 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | Ly = 1; % Length of y-domain 7 | alpha = 1.4; % Thermal Diffusivity 8 | k = 0.8; % Thermal Conductivity 9 | h = 10; % Convection Heat Transfer Coefficient 10 | T_inf = 300; % Free Air temperature 11 | M = 41; % Number of grid points in x-direction 12 | N = 41; % Number of grid points in y-direction 13 | 14 | % Calculated Values 15 | dx = Lx/(M-1); % Grid size in x-direction 16 | dy = Ly/(N-1); % Grid size in y-direction 17 | dt = 1e-3; % Time Step 18 | gamma_x = alpha*dx/(dt^2); 19 | gamma_y = alpha*dy/(dt^2); 20 | 21 | % Initializing Temperature matrix 22 | T = zeros(N,M); 23 | T_array = {}; % Temperature Array to store T at each time step 24 | 25 | % Boundary Conditions 26 | T(:,1) = 500; % Left Wall Boundary Condition 27 | T(:,M) = 500; % Right Wall Boundary Condition 28 | T(N,:) = 500; % Bottom Wall Boundary Condition 29 | 30 | % Computation 31 | n = 0; % Iterator 32 | t = 0; 33 | while t<10 34 | for i=2:M-1 % Top Boundary Condition 35 | T(1,i) = (4*T(2,i)-T(3,i)+(2*dy*(h/k))*T_inf)/(3+(2*dy*(h/k))); 36 | end 37 | err = 0; % Error 38 | for j=2:N-1 39 | for i=2:M-1 40 | T_temp = T(j,i); 41 | T(j,i) = (1/(1+2*gamma_x+2*gamma_y))*(gamma_x*(T(j+1,i)+T(j-1,i))+gamma_y*(T(j,i+1)+T(j,i-1))+T_temp); 42 | err = err + power(T(j,i)-T_temp,2); 43 | end 44 | end 45 | err = sqrt(err/(M*N)); 46 | n = n+1; 47 | if n==10 || n==30 || n==50 || n==100 || n==150 || n==200 48 | T_array = [T_array;T]; 49 | elseif n==275 || n==350 || n==550 || n==750 || n==1000 || n==1250 50 | T_array = [T_array;T]; 51 | end 52 | t = t+dt; 53 | end 54 | %T 55 | n 56 | err 57 | 58 | % Plotting 59 | x = linspace(0,Lx,M); 60 | y = linspace(Ly,0,N); 61 | [X,Y] = meshgrid(x,y); 62 | contourf(X,Y,T,':','ShowText','on'),colorbar,colormap(jet) 63 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 64 | 65 | % Animation 66 | figure; 67 | myVideo = VideoWriter('Unsteady Heat COnduction'); %open video file 68 | myVideo.FrameRate = 3; 69 | open(myVideo) 70 | 71 | for p=1:12 72 | %figure; 73 | x = linspace(0,Lx,M); 74 | y = linspace(Ly,0,N); 75 | [X,Y] = meshgrid(x,y); 76 | TT = T_array(p); 77 | contourf(X,Y,TT{:,:},':','ShowText','on'),colorbar,colormap(jet) 78 | xlabel('X'),ylabel('Y'),title('Temperature Variation (T)') 79 | pause(0.02) 80 | frame = getframe(gcf); 81 | writeVideo(myVideo,frame); 82 | end 83 | close(myVideo) 84 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_7/Finding_Optimum_Relaxation_Factor.m: -------------------------------------------------------------------------------- 1 | % Example-7 : 2D Heat Conduction & Convection using PSOR 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 0.2; % Length of x-domain 6 | Ly = 0.1; % Length of y-domain 7 | M = 41; % Number of Grid Points along x-direction 8 | N = 21; % Number of Grid Points along y-direction 9 | hg = 1000; % Heat Convection Coefficient of gases 10 | Tg = 2000; % Temperature of gases 11 | hb = 8000; % Heat Convection Coefficient of coolant 12 | Tb = 60; % Temperature of coolant 13 | k = 20; % Thermal Conductivity 14 | 15 | % Calculated Values 16 | dx = Lx/(M-1); % Grid size in x-direction 17 | dy = Ly/(N-1); % Grid size in y-direction 18 | B = dx/dy; 19 | %w = 1.9; % Relaxation Factor 20 | W = 1:0.001:2; 21 | NI = zeros(size(W)); 22 | 23 | for p=1:length(W) 24 | w = W(p); 25 | 26 | % Computation 27 | epsilon = 1e-8; % Error Sensivity 28 | err = 1; % Error 29 | n = 0; % Iterator 30 | 31 | % Initializing Temperature Matrix 32 | T = zeros(N,M); 33 | 34 | while err>epsilon 35 | % Applying Boundary Conditions 36 | for i=1:M % Top Boundary Condition (Convective) 37 | T(1,i) = (4*T(2,i)-T(3,i)+(2*dy*hg/k)*Tg)/(3+(2*dy*hg/k)); 38 | end 39 | for j=1:N 40 | T(j,1) = (4*T(j,2)-T(j,3))/3; % Left Boundary Condition (Adiabatic) 41 | T(j,M) = (4*T(j,M-1)-T(j,M-2))/3; % Right Boundary Condition (Adiabatic) 42 | end 43 | for i=1:21 44 | T(N,i) = (4*T(N-1,i)-T(N-2,i))/3; % Half Bottom Boundary Condition (Adiabatic) 45 | end 46 | for i=21:M 47 | T(N,i) = (4*T(N-1,i)-T(N-2,i)+(2*dy*hb/k)*Tb)/(3+(2*dy*hb/k)); 48 | end 49 | 50 | % Updating T_old i.e. T from previous iteration 51 | for j=1:N 52 | for i=1:M 53 | T_old(j,i) = T(j,i); 54 | end 55 | end 56 | % Updating T from current iteration 57 | for j=2:N-1 58 | for i=2:M-1 59 | T(j,i) = (1-w)*T_old(j,i)+(w/(2*(1+B^2)))*((B^2)*T(j,i-1)+T(j-1,i)+T_old(j,i+1)+(B^2)*T_old(j+1,i)); 60 | end 61 | end 62 | err = 0; % Error 63 | for j=1:N 64 | for i=1:M 65 | err = err + power(T(j,i)-T_old(j,i),2); 66 | end 67 | end 68 | err = sqrt(err/(M*N)); 69 | n = n+1; % Iterator 70 | end 71 | NI(p) = n; 72 | end 73 | 74 | % Plotting 75 | plot(W,NI,'b-'),xlim([0.8,2.2]) 76 | xlabel('Relaxation Factor'),ylabel('Number of Iterations') 77 | title('Number of Iterations taken VS Relaxation factor') 78 | hold on; 79 | plot(W(islocalmin(NI)),NI(islocalmin(NI)),'ro') 80 | hold off; 81 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_1/Jacobi_VS_Gauss_Seidel.m: -------------------------------------------------------------------------------- 1 | % Comparison of Error variation against Iteration w.r.t Jacobi 2 | % and Gauss-Seidel Methods 3 | 4 | % Known Parameters 5 | Lx = 6; % Length of domain along x-direction 6 | Ly = 4; % Length of domain along y-direction 7 | M = 31; % Number of grid points starting from 1 in x-direction 8 | N = 21; % Number of grid points starting from 1 in y-direction 9 | 10 | % Calculated constants 11 | dx = Lx/(M-1); % Grid size along x-direction 12 | dy = Ly/(N-1); % Grid size along y-direction 13 | % Coefficients 14 | ap = 2*((1/(dx^2))+(1/(dy^2))); 15 | ae = 1/(dx^2); 16 | aw = 1/(dx^2); 17 | an = 1/(dy^2); 18 | as = 1/(dy^2); 19 | 20 | % Jacobi Method 21 | % Initializing Stream Function Matrix 22 | psi_new = zeros(N,M); 23 | psi_old = zeros(N,M); 24 | 25 | % Boundary Conditions 26 | psi_new(N,1:6) = 0; % Bottom Boundary 27 | psi_new(N,7:M) = 100; % Bottom Boundary 28 | psi_new(:,1) = 0; % Left Boundary 29 | psi_new(1,:) = 0; % Top Boundary 30 | 31 | % Computation 32 | epsilon = 1e-8; % Error Sensivity 33 | err_1 = 1; % Error 34 | Err_1 = []; % Error Array 35 | n_1 = 0; % Iterator 36 | 37 | while err_1>epsilon 38 | for j=1:N 39 | for i=1:M 40 | psi_old(j,i) = psi_new(j,i); 41 | end 42 | end 43 | for j=2:N-1 % For Internal Grid points 44 | for i=2:M-1 % For Internal Grid points 45 | % Psi(j,i)_(k+1)th_iteration Calculation 46 | psi_new(j,i) = (1/ap)*(ae*psi_old(j+1,i) + aw*psi_old(j-1,i) + an*psi_old(j,i+1) + as*psi_old(j,i-1)); 47 | end 48 | end 49 | for j=1:N % Right Neumann Boundary Condition 50 | psi_new(j,M) = psi_new(j,M-1); 51 | end 52 | err_1 = 0; % Error 53 | for j=1:N 54 | for i=1:M 55 | err_1 = err_1 + power(psi_new(j,i)-psi_old(j,i),2); 56 | end 57 | end 58 | err_1 = sqrt(err_1/(M*N)); 59 | Err_1(end+1) = err_1; 60 | n_1 = n_1+1; % Iterator 61 | end 62 | 63 | % Gauss-Seidel Method 64 | % Initializing Stream Function Matrix 65 | psi = zeros(N,M); 66 | 67 | % Boundary Conditions 68 | psi(N,1:6) = 0; % Bottom Boundary 69 | psi(N,7:M) = 100; % Bottom Boundary 70 | psi(:,1) = 0; % Left Boundary 71 | psi(1,:) = 0; % Top Boundary 72 | 73 | err_2 = 1; % Error 74 | Err_2 = []; % Error Array 75 | n_2 = 0; % Iterator 76 | 77 | while err_2>epsilon 78 | err_2 = 0; % Error 79 | for j=2:N-1 % For Internal Grid points 80 | for i=2:M-1 % For Internal Grid points 81 | psi_temp = psi(j,i); 82 | % Psi(j,i)_(k+1)th_iteration Calculation 83 | psi(j,i) = (1/ap)*(ae*psi(j+1,i) + aw*psi(j-1,i) + an*psi(j,i+1) + as*psi(j,i-1)); 84 | err_2 = err_2 + power(psi(j,i)-psi_temp,2); 85 | end 86 | end 87 | for j=1:N % Right Neumann Boundary Condition 88 | psi(j,M) = psi(j,M-1); 89 | end 90 | err_2 = sqrt(err_2/(M*N)); 91 | Err_2(end+1) = err_2; 92 | n_2 = n_2+1; % Iterator 93 | end 94 | 95 | % Outputs 96 | display('Number of Iterations in Jacobi Method:',num2str(n_1)); 97 | display('Number of Iterations in Gauss-Seidel Method:',num2str(n_2)); 98 | %Err_1 99 | %Err_2 100 | 101 | % Comparison Plots 102 | x1 = 1:1:n_1; 103 | x2 = 1:1:n_2; 104 | loglog(x1,Err_1,'r-',x2,Err_2,'b-') 105 | xlabel('log(Iterations)'),ylabel('log(Error)') 106 | title('Error VS Iterations Comparison') 107 | legend('Jacobi','Gauss-Seidel') 108 | -------------------------------------------------------------------------------- /FDM/Hyperbolic/Example_3/Unsteady_Convection_1D.m: -------------------------------------------------------------------------------- 1 | % Example-3 : 1D Linear Convection 2 | % Comparison between FTBS (explicit), Lax-Wendroff (explicit) 3 | % and Lax (explicit) schemes 4 | clear all; clc 5 | 6 | % Known Values 7 | Lx = 300; % Length of x-domain 8 | a = 300; % Velocity (300 m/s) 9 | M = 61; % Number of grid points 10 | x = linspace(0,Lx,M); 11 | 12 | % Calculated values 13 | dx = Lx/(M-1); % Grid size 14 | Dt = [0.01666,0.015,0.0075]; % Time Steps 15 | CL = zeros(size(Dt)); % Courant Number for each step 16 | 17 | % Initial Conditions 18 | % U(x) = 0 for 0<=x<=50 19 | % U(x) = 100*sin(pi*(x-50)/60) for 50<=x<=110 20 | % U(x) = 0 for 110<=x<=300 21 | 22 | % Boundary Conditions 23 | % U(x,t) = 0 for x = 0 24 | % U(x,t) = 0 for x = Lx 25 | 26 | % Velocity Matrices 27 | U_ftbs = []; 28 | U_lw = []; 29 | U_lax = []; 30 | 31 | % Computation 32 | for j=1:length(Dt) 33 | dt = Dt(j); % Time Step 34 | lambda = a*dt/dx; % Courant Number 35 | CL(j) = lambda; % Courant Number for each time step 36 | 37 | % Initializing Velocity Matrix 38 | U_FTBS = zeros(1,M); % U for FTBS 39 | U_LW = zeros(1,M); % U for Lax-Wendroff 40 | U_LAX = zeros(1,M); % U for FTCS 41 | 42 | % Applying given conditions 43 | for p=1:M 44 | if((50<=x(p))&&(x(p)<=110)) 45 | U_FTBS(1,p) = 100*sin(pi*(x(p)-50)/60); 46 | U_LW(1,p) = 100*sin(pi*(x(p)-50)/60); 47 | U_LAX(1,p) = 100*sin(pi*(x(p)-50)/60); 48 | end 49 | end 50 | 51 | % Initial Velocity Profile (remains same for each case) 52 | U_initial = U_FTBS; 53 | 54 | % Computation using each method 55 | t = 0; % Time 56 | while t<=0.45 57 | 58 | U_FTBS_old = U_FTBS; % FTBS Scheme 59 | U_LW_old = U_LW; % Lax-Wendroff Scheme 60 | U_LAX_old = U_LAX; % FTCS Scheme 61 | 62 | for i=2:M-1 63 | U_FTBS(1,i) = (1-lambda)*U_FTBS_old(1,i)+lambda*U_FTBS_old(1,i-1); % FTBS Scheme 64 | U_LW(1,i) = (1-lambda^2)*U_LW_old(1,i)-(lambda/2)*(1-lambda)*U_LW_old(1,i+1)+(lambda/2)*(1+lambda)*U_LW_old(1,i-1); % Lax-Wendroff Scheme 65 | U_LAX(1,i) = ((1-lambda)/2)*U_LAX_old(1,i+1)+((1+lambda)/2)*U_LAX_old(1,i-1); % Lax Scheme 66 | end 67 | t = t+dt; 68 | end 69 | U_ftbs = [U_ftbs;U_FTBS]; % Updating U_ftbs array for each time step chosen 70 | U_lw = [U_lw;U_LW]; % Updating U_lw array for each time step chosen 71 | U_lax = [U_lax;U_LAX]; % Updating U_ftcs array for each time step chosen 72 | end 73 | 74 | % Plotting 75 | 76 | % Results using FTBS 77 | figure; 78 | plot(x,U_initial,'black') 79 | hold on; 80 | plot(x,U_ftbs) 81 | xlabel('Length (x)'),ylabel('Velocity') 82 | title('Velocity Profile') 83 | legend('Exact (C=1)',strcat('C =',num2str(CL(1))),strcat('C =',num2str(CL(2))),strcat('C =',num2str(CL(3))),'Location','bestoutside') 84 | hold off; 85 | 86 | % Results using Lax-Wendroff 87 | figure; 88 | plot(x,U_initial,'black') 89 | hold on; 90 | plot(x,U_lw) 91 | xlabel('Length (x)'),ylabel('Velocity') 92 | title('Velocity Profile') 93 | legend('Exact (C=1)',strcat('C =',num2str(CL(1))),strcat('C =',num2str(CL(2))),strcat('C =',num2str(CL(3))),'Location','bestoutside') 94 | hold off; 95 | 96 | % Results using FTCS 97 | figure; 98 | plot(x,U_initial,'black') 99 | hold on; 100 | plot(x,U_lax) 101 | xlabel('Length (x)'),ylabel('Velocity') 102 | title('Velocity Profile') 103 | legend('Exact (C=1)',strcat('C =',num2str(CL(1))),strcat('C =',num2str(CL(2))),strcat('C =',num2str(CL(3))),'Location','bestoutside') 104 | hold off; 105 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_5/Jacobi_VS_Gauss_Seidel_VS_PSOR.m: -------------------------------------------------------------------------------- 1 | % Temperature Variation Example-5 2 | % Comparing the Iterations & Results of Jacobi, Gauss-Seidel & PSOR methods 3 | 4 | % Known Parameters 5 | Lx = 1; % Length of domain along x-direction 6 | Ly = 1; % Length of domain along y-direction 7 | M = 41; % Number of grid points starting from 1 in x-direction 8 | N = 41; % Number of grid points starting from 1 in y-direction 9 | 10 | % Calculated constants 11 | dx = Lx/(M-1); % Grid size along x-direction 12 | dy = Ly/(N-1); % Grid size along y-direction 13 | 14 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | 16 | % 1. Jacobi Method 17 | % Coefficients 18 | ap = 2*((1/(dx^2))+(1/(dy^2))); 19 | ae = 1/(dx^2); 20 | aw = 1/(dx^2); 21 | an = 1/(dy^2); 22 | as = 1/(dy^2); 23 | 24 | % Initializing Temperature Matrix 25 | T_Jacobi = zeros(N,M); 26 | T_old_Jacobi = zeros(N,M); 27 | 28 | % Boundary Conditions 29 | T_Jacobi(N,:) = 900; % Bottom Boundary 30 | T_Jacobi(1,:) = 600; % Top Boundary 31 | T_Jacobi(:,1) = 400; % Left Boundary 32 | T_Jacobi(:,M) = 800; % Right Boundary 33 | 34 | % Computation - Jacobi Method 35 | epsilon = 1e-8; % Tolerance or Error Sensitivity 36 | err_Jacobi = 1; % Error 37 | Err_Jacobi = []; % Error Array 38 | n_Jacobi = 0; % Iterator 39 | 40 | while err_Jacobi>epsilon 41 | for j=1:N 42 | for i=1:M 43 | T_old_Jacobi(j,i) = T_Jacobi(j,i); 44 | end 45 | end 46 | for j=2:N-1 47 | for i=2:M-1 48 | T_Jacobi(j,i) = (1/ap)*(ae*T_old_Jacobi(j+1,i) + aw*T_old_Jacobi(j-1,i) + an*T_old_Jacobi(j,i+1) + as*T_old_Jacobi(j,i-1)); 49 | end 50 | end 51 | err_Jacobi = 0; % Error 52 | for j=1:N 53 | for i=1:M 54 | err_Jacobi = err_Jacobi + power(T_Jacobi(j,i)-T_old_Jacobi(j,i),2); 55 | end 56 | end 57 | err_Jacobi = sqrt(err_Jacobi/(M*N)); 58 | Err_Jacobi(end+1) = err_Jacobi; 59 | n_Jacobi = n_Jacobi+1; % Iterator 60 | end 61 | 62 | % Plotting using Jacobi Method Results 63 | figure; 64 | x = linspace(0,Lx,M); 65 | y = linspace(Ly,0,N); 66 | [X,Y] = meshgrid(x,y); 67 | contourf(X,Y,T_Jacobi,':','ShowText','on'),colorbar,colormap(jet) 68 | set(gca, 'XTick',0:1:Lx) 69 | xlabel('X'),ylabel('Y') 70 | title({['Temperature Contour Plot'];['Jacobi Method'];['Iterations taken : ',num2str(n_Jacobi)]}) 71 | 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | 74 | % 2. Gauss-Seidel Method 75 | % Initializing Temperature Matrix 76 | T_GS = zeros(N,M); 77 | 78 | % Boundary Conditions 79 | T_GS(N,:) = 900; % Bottom Boundary 80 | T_GS(1,:) = 600; % Top Boundary 81 | T_GS(:,1) = 400; % Left Boundary 82 | T_GS(:,M) = 800; % Right Boundary 83 | 84 | % Computation - Gauss Seidel Method 85 | epsilon = 1e-8; % Tolerance or Error Sensitivity 86 | err_GS = 1; % Error 87 | Err_GS = []; % Error Array 88 | n_GS = 0; % Iterator 89 | 90 | while err_GS>epsilon 91 | err_GS = 0; 92 | for j=2:N-1 93 | for i=2:M-1 94 | T_GS_temp = T_GS(j,i); 95 | T_GS(j,i) = (1/ap)*(ae*T_GS(j+1,i) + aw*T_GS(j-1,i) + an*T_GS(j,i+1) + as*T_GS(j,i-1)); 96 | err_GS = err_GS + power(T_GS(j,i)-T_GS_temp,2); 97 | end 98 | end 99 | err_GS = sqrt(err_GS/(M*N)); 100 | Err_GS(end+1) = err_GS; 101 | n_GS = n_GS+1; % Iterator 102 | end 103 | 104 | % Plotting using Gauss-Seidel Method Results 105 | figure; 106 | x = linspace(0,Lx,M); 107 | y = linspace(Ly,0,N); 108 | [X,Y] = meshgrid(x,y); 109 | contourf(X,Y,T_GS,':','ShowText','on'),colorbar,colormap(jet) 110 | set(gca, 'XTick',0:1:Lx) 111 | xlabel('X'),ylabel('Y') 112 | title({['Temperature Contour Plot'];['Gauss-Seidel Method'];['Iterations taken : ',num2str(n_GS)]}) 113 | 114 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 115 | 116 | % 3. PSOR Method 117 | % Initializing Temperature Matrix 118 | T_PSOR = zeros(N,M); 119 | T_old_PSOR = zeros(N,M); 120 | 121 | % Boundary Conditions 122 | T_PSOR(N,:) = 900; % Bottom Boundary 123 | T_PSOR(1,:) = 600; % Top Boundary 124 | T_PSOR(:,1) = 400; % Left Boundary 125 | T_PSOR(:,M) = 800; % Right Boundary 126 | 127 | % Finding Optimum Relaxation Factor 128 | B = dx/dy; 129 | a = ((cos(pi/(M-1))+(B^2)*cos(pi/(N-1)))/(1+(B^2)))^2; 130 | w = (2-sqrt(1-a))/a; % Optimum Relaxation Factor 131 | 132 | % Computation 133 | epsilon = 1e-8; % Error Sensivity 134 | err_PSOR = 1; % Error 135 | Err_PSOR = []; % Error Array 136 | n_PSOR = 0; % Iterator 137 | 138 | while err_PSOR>epsilon 139 | for j=1:N 140 | for i=1:M 141 | T_old_PSOR(j,i) = T_PSOR(j,i); 142 | end 143 | end 144 | for j=2:N-1 % For Internal Grid points 145 | for i=2:M-1 % For Internal Grid points 146 | % T(j,i)_(k+1)th_iteration Calculation 147 | T_PSOR(j,i) = (1-w)*T_old_PSOR(j,i)+(w/(2*(1+B^2)))*((B^2)*T_PSOR(j,i-1)+T_PSOR(j-1,i)+T_old_PSOR(j,i+1)+(B^2)*T_old_PSOR(j+1,i)); 148 | end 149 | end 150 | err_PSOR = 0; % Error 151 | for j=1:N 152 | for i=1:M 153 | err_PSOR = err_PSOR + power(T_PSOR(j,i)-T_old_PSOR(j,i),2); 154 | end 155 | end 156 | err_PSOR = sqrt(err_PSOR/(M*N)); 157 | Err_PSOR(end+1) = err_PSOR; 158 | n_PSOR = n_PSOR+1; % Iterator 159 | end 160 | 161 | % Plotting using PSOR Method Results 162 | figure; 163 | x = linspace(0,Lx,M); 164 | y = linspace(Ly,0,N); 165 | [X,Y] = meshgrid(x,y); 166 | contourf(X,Y,T_PSOR,':','ShowText','on'),colorbar,colormap(jet) 167 | set(gca, 'XTick',0:1:Lx) 168 | xlabel('X'),ylabel('Y') 169 | title({['Temperature Contour Plot'];['PSOR Method'];['Iterations taken : ',num2str(n_PSOR)]}) 170 | 171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 172 | 173 | % Errors VS Iterations Comparison Plot 174 | figure; 175 | x_Jacobi = 1:1:n_Jacobi; 176 | x_GS = 1:1:n_GS; 177 | x_PSOR = 1:1:n_PSOR; 178 | loglog(x_Jacobi,Err_Jacobi,'r-',x_GS,Err_GS,'b-',x_PSOR,Err_PSOR,'g-') 179 | hold on; 180 | x = 1:1:max([n_Jacobi,n_GS,n_PSOR]); 181 | Tolerance = (1e-8)*ones(1,max([n_Jacobi,n_GS,n_PSOR])); 182 | plot(x,Tolerance,'k-'); 183 | hold off; 184 | xlabel('log(Iterations)'),ylabel('log(Error)') 185 | title('Error VS Iterations Comparison') 186 | legend('Jacobi','Gauss-Seidel','PSOR','Tolerance') 187 | -------------------------------------------------------------------------------- /FDM/Elliptic/Stream_Function_Examples/Example_3/Stream_Function.m: -------------------------------------------------------------------------------- 1 | % Example-3 : Stream Function Variation 2 | % Jacobi VS Gauss-Seidel VS PSOR 3 | clear all; clc 4 | 5 | % Known Values 6 | Lx = 40; % Length of x-domain 7 | Ly = 40; % Length of y-domain 8 | M = 81; % Number of grid points in x-domain 9 | N = 81; % Number of grid points in y-domain 10 | x = linspace(0,Lx,M); 11 | y = linspace(Ly,0,N); 12 | 13 | % Calculated values 14 | dx = Lx/(M-1); 15 | dy = Ly/(N-1); 16 | 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 18 | 19 | % 1. Jacobi Method 20 | % Coefficients 21 | ap = 2*((1/(dx^2))+(1/(dy^2))); 22 | ae = 1/(dx^2); 23 | aw = 1/(dx^2); 24 | an = 1/(dy^2); 25 | as = 1/(dy^2); 26 | 27 | % Initializing Temperature Matrix 28 | Psi_Jacobi = zeros(N,M); 29 | Psi_old_Jacobi = zeros(N,M); 30 | 31 | % Boundary Conditions 32 | Psi_Jacobi(N,:) = 0; % Bottom Boundary 33 | Psi_Jacobi(1,:) = 0; % Top Boundary 34 | Psi_Jacobi(:,M) = 0; % Right Boundary 35 | 36 | % Computation - Jacobi Method 37 | epsilon = 1e-8; % Tolerance or Error Sensitivity 38 | err_Jacobi = 1; % Error 39 | Err_Jacobi = []; % Error Array 40 | n_Jacobi = 0; % Iterator 41 | 42 | while err_Jacobi>epsilon 43 | % Left Boundary Condition 44 | for j=1:N 45 | if(10<=y(j) && y(j)<=30) 46 | Psi_Jacobi(j,1) = 1; 47 | else 48 | Psi_Jacobi(j,1) = 0; 49 | end 50 | end 51 | 52 | for j=1:N 53 | for i=1:M 54 | Psi_old_Jacobi(j,i) = Psi_Jacobi(j,i); 55 | end 56 | end 57 | for j=2:N-1 58 | for i=2:M-1 59 | Psi_Jacobi(j,i) = (1/ap)*(ae*Psi_old_Jacobi(j+1,i) + aw*Psi_old_Jacobi(j-1,i) + an*Psi_old_Jacobi(j,i+1) + as*Psi_old_Jacobi(j,i-1)); 60 | end 61 | end 62 | err_Jacobi = 0; % Error 63 | for j=1:N 64 | for i=1:M 65 | err_Jacobi = err_Jacobi + power(Psi_Jacobi(j,i)-Psi_old_Jacobi(j,i),2); 66 | end 67 | end 68 | err_Jacobi = sqrt(err_Jacobi/(M*N)); 69 | Err_Jacobi(end+1) = err_Jacobi; 70 | n_Jacobi = n_Jacobi+1; % Iterator 71 | end 72 | 73 | % Plotting using Jacobi Method Results 74 | figure; 75 | [X,Y] = meshgrid(x,y); 76 | contourf(X,Y,Psi_Jacobi,':','ShowText','on'),colorbar,colormap(jet) 77 | xlabel('X'),ylabel('Y') 78 | title({['Stream Function Contour Plot'];['Jacobi Method'];['Iterations taken : ',num2str(n_Jacobi)]}) 79 | 80 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 81 | 82 | % 2. Gauss-Seidel Method 83 | % Initializing Temperature Matrix 84 | Psi_GS = zeros(N,M); 85 | 86 | % Boundary Conditions 87 | Psi_GS(N,:) = 0; % Bottom Boundary 88 | Psi_GS(1,:) = 0; % Top Boundary 89 | Psi_GS(:,M) = 0; % Right Boundary 90 | 91 | % Computation - Gauss Seidel Method 92 | epsilon = 1e-8; % Tolerance or Error Sensitivity 93 | err_GS = 1; % Error 94 | Err_GS = []; % Error Array 95 | n_GS = 0; % Iterator 96 | 97 | while err_GS>epsilon 98 | % Left Boundary Condition 99 | for j=1:N 100 | if(10<=y(j) && y(j)<=30) 101 | Psi_GS(j,1) = 1; 102 | else 103 | Psi_GS(j,1) = 0; 104 | end 105 | end 106 | 107 | err_GS = 0; 108 | for j=2:N-1 109 | for i=2:M-1 110 | Psi_GS_temp = Psi_GS(j,i); 111 | Psi_GS(j,i) = (1/ap)*(ae*Psi_GS(j+1,i) + aw*Psi_GS(j-1,i) + an*Psi_GS(j,i+1) + as*Psi_GS(j,i-1)); 112 | err_GS = err_GS + power(Psi_GS(j,i)-Psi_GS_temp,2); 113 | end 114 | end 115 | err_GS = sqrt(err_GS/(M*N)); 116 | Err_GS(end+1) = err_GS; 117 | n_GS = n_GS+1; % Iterator 118 | end 119 | 120 | % Plotting using Gauss-Seidel Method Results 121 | figure; 122 | [X,Y] = meshgrid(x,y); 123 | contourf(X,Y,Psi_GS,':','ShowText','on'),colorbar,colormap(jet) 124 | xlabel('X'),ylabel('Y') 125 | title({['Stream Function Contour Plot'];['Gauss-Seidel Method'];['Iterations taken : ',num2str(n_GS)]}) 126 | 127 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 128 | 129 | % 3. PSOR Method 130 | % Initializing Temperature Matrix 131 | Psi_PSOR = zeros(N,M); 132 | Psi_old_PSOR = zeros(N,M); 133 | 134 | % Boundary Conditions 135 | Psi_PSOR(N,:) = 0; % Bottom Boundary 136 | Psi_PSOR(1,:) = 0; % Top Boundary 137 | Psi_PSOR(:,M) = 0; % Right Boundary 138 | 139 | % Finding Optimum Relaxation Factor 140 | B = dx/dy; 141 | a = ((cos(pi/(M-1))+(B^2)*cos(pi/(N-1)))/(1+(B^2)))^2; 142 | w = (2-sqrt(1-a))/a; % Optimum Relaxation Factor 143 | 144 | % Computation 145 | epsilon = 1e-8; % Error Sensivity 146 | err_PSOR = 1; % Error 147 | Err_PSOR = []; % Error Array 148 | n_PSOR = 0; % Iterator 149 | 150 | while err_PSOR>epsilon 151 | % Left Boundary Condition 152 | for j=1:N 153 | if(10<=y(j) && y(j)<=30) 154 | Psi_PSOR(j,1) = 1; 155 | else 156 | Psi_PSOR(j,1) = 0; 157 | end 158 | end 159 | 160 | for j=1:N 161 | for i=1:M 162 | Psi_old_PSOR(j,i) = Psi_PSOR(j,i); 163 | end 164 | end 165 | for j=2:N-1 % For Internal Grid points 166 | for i=2:M-1 % For Internal Grid points 167 | % T(j,i)_(k+1)th_iteration Calculation 168 | Psi_PSOR(j,i) = (1-w)*Psi_old_PSOR(j,i)+(w/(2*(1+B^2)))*((B^2)*Psi_PSOR(j,i-1)+Psi_PSOR(j-1,i)+Psi_old_PSOR(j,i+1)+(B^2)*Psi_old_PSOR(j+1,i)); 169 | end 170 | end 171 | err_PSOR = 0; % Error 172 | for j=1:N 173 | for i=1:M 174 | err_PSOR = err_PSOR + power(Psi_PSOR(j,i)-Psi_old_PSOR(j,i),2); 175 | end 176 | end 177 | err_PSOR = sqrt(err_PSOR/(M*N)); 178 | Err_PSOR(end+1) = err_PSOR; 179 | n_PSOR = n_PSOR+1; % Iterator 180 | end 181 | 182 | % Plotting using PSOR Method Results 183 | figure; 184 | [X,Y] = meshgrid(x,y); 185 | contourf(X,Y,Psi_PSOR,':','ShowText','on'),colorbar,colormap(jet) 186 | xlabel('X'),ylabel('Y') 187 | title({['Stream Function Contour Plot'];['PSOR Method'];['Iterations taken : ',num2str(n_PSOR)]}) 188 | 189 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 190 | 191 | % Errors VS Iterations Comparison Plot 192 | figure; 193 | x_Jacobi = 1:1:n_Jacobi; 194 | x_GS = 1:1:n_GS; 195 | x_PSOR = 1:1:n_PSOR; 196 | loglog(x_Jacobi,Err_Jacobi,'r-',x_GS,Err_GS,'b-',x_PSOR,Err_PSOR,'g-') 197 | hold on; 198 | n = 1:1:max([n_Jacobi,n_GS,n_PSOR]); 199 | Tolerance = (1e-8)*ones(1,max([n_Jacobi,n_GS,n_PSOR])); 200 | plot(n,Tolerance,'k-','LineWidth',2); 201 | ylim([1e-10,1e0]) 202 | hold off; 203 | xlabel('log(Iterations)'),ylabel('log(Error)') 204 | title('Error VS Iterations Comparison between Schemes') 205 | legend('Jacobi','Gauss-Seidel','PSOR','Tolerance') 206 | -------------------------------------------------------------------------------- /FDM/Elliptic/Temperature_Variation_Examples/2D_Examples/Example_6/Heat_Conduction_2D.m: -------------------------------------------------------------------------------- 1 | % 2D Heat Conduction & Convection of a plate 2 | clear all; clc 3 | 4 | % Known Values 5 | Lx = 1; % Length of x-domain 6 | Ly = 2; % Length of y-domain 7 | M = 21; % Number of grid points in x-direction 8 | N = 41; % Number of grid points in y-direction 9 | T0 = 273; % Free Air temperature 10 | h = 25; % Heat transfer coefficient 11 | k = 0.25; % Thermal conductivity 12 | 13 | % Calculated values 14 | dx = Lx/(M-1); % Grid size in x-direction 15 | dy = Ly/(N-1); % Grid size in y-direction 16 | 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 18 | 19 | % 1. Jacobi Method 20 | % Coefficients 21 | ap = 2*((1/(dx^2))+(1/(dy^2))); 22 | ae = 1/(dx^2); 23 | aw = 1/(dx^2); 24 | an = 1/(dy^2); 25 | as = 1/(dy^2); 26 | 27 | % Initializing Temperature Matrix 28 | T_Jacobi = zeros(N,M); 29 | T_old_Jacobi = zeros(N,M); 30 | 31 | % Computation - Jacobi Method 32 | epsilon = 1e-8; % Tolerance or Error Sensitivity 33 | err_Jacobi = 1; % Error 34 | Err_Jacobi = []; % Error Array 35 | n_Jacobi = 0; % Iterator 36 | 37 | while err_Jacobi>epsilon 38 | % For Applying Boundary Conditions(BC) 39 | for i=1:M 40 | % Top BC 41 | T_Jacobi(1,i) = (4*T_Jacobi(2,i)-T_Jacobi(3,i)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 42 | 43 | % Bottom BC 44 | T_Jacobi(N,i) = (4*T_Jacobi(N-1,i)-T_Jacobi(N-2,i)-(2*h*dy/k)*T0)/(3-(2*h*dy/k)); 45 | end 46 | for j=1:N 47 | % Right BC 48 | T_Jacobi(j,M) = (4*T_Jacobi(j,M-1)-T_Jacobi(j,M-2)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 49 | 50 | %Left BC 51 | T_Jacobi(j,1) = (4*T_Jacobi(j,2)-T_Jacobi(j,3))/3; 52 | end 53 | 54 | for j=11:31 % Left BC (from node=11 to node=31) 55 | T_Jacobi(j,1) = 303; 56 | end 57 | 58 | % Finding T_Jacobi from previous iteration i.e. finding T_old_Jacobi 59 | for j=1:N 60 | for i=1:M 61 | T_old_Jacobi(j,i) = T_Jacobi(j,i); 62 | end 63 | end 64 | 65 | % Updating T_Jacobi 66 | for j=2:N-1 67 | for i=2:M-1 68 | T_Jacobi(j,i) = (1/ap)*(ae*T_old_Jacobi(j+1,i) + aw*T_old_Jacobi(j-1,i) + an*T_old_Jacobi(j,i+1) + as*T_old_Jacobi(j,i-1)); 69 | end 70 | end 71 | err_Jacobi = 0; % Error 72 | for j=1:N % Error calculation after each iteration 73 | for i=1:M 74 | err_Jacobi = err_Jacobi + power(T_Jacobi(j,i)-T_old_Jacobi(j,i),2); 75 | end 76 | end 77 | err_Jacobi = sqrt(err_Jacobi/(M*N)); 78 | Err_Jacobi(end+1) = err_Jacobi; 79 | n_Jacobi = n_Jacobi+1; % Iterator 80 | end 81 | 82 | % Plotting using Jacobi Method Results 83 | figure; 84 | x = linspace(0,Lx,M); 85 | y = linspace(Ly,0,N); 86 | [X,Y] = meshgrid(x,y); 87 | contourf(X,Y,T_Jacobi,':','ShowText','on'),colorbar,colormap(jet) 88 | xlabel('X'),ylabel('Y') 89 | title({['Temperature Contour Plot'];['Jacobi Method'];['Iterations taken : ',num2str(n_Jacobi)]}) 90 | 91 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 92 | 93 | % 2. Gauss-Seidel Method 94 | % Initializing Temperature Matrix 95 | T_GS = zeros(N,M); 96 | 97 | % Computation - Gauss Seidel Method 98 | epsilon = 1e-8; % Tolerance or Error Sensitivity 99 | err_GS = 1; % Error 100 | Err_GS = []; % Error Array 101 | n_GS = 0; % Iterator 102 | 103 | while err_GS>epsilon 104 | % For Applying Boundary Conditions(BC) 105 | for i=1:M 106 | % Top BC 107 | T_GS(1,i) = (4*T_GS(2,i)-T_GS(3,i)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 108 | 109 | % Bottom BC 110 | T_GS(N,i) = (4*T_GS(N-1,i)-T_GS(N-2,i)-(2*h*dy/k)*T0)/(3-(2*h*dy/k)); 111 | end 112 | for j=1:N 113 | % Right BC 114 | T_GS(j,M) = (4*T_GS(j,M-1)-T_GS(j,M-2)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 115 | 116 | %Left BC 117 | T_GS(j,1) = (4*T_GS(j,2)-T_GS(j,3))/3; 118 | end 119 | 120 | for j=11:31 % Left BC (from node=11 to node=31) 121 | T_GS(j,1) = 303; 122 | end 123 | 124 | err_GS = 0; 125 | for j=2:N-1 126 | for i=2:M-1 127 | T_GS_temp = T_GS(j,i); 128 | T_GS(j,i) = (1/ap)*(ae*T_GS(j+1,i) + aw*T_GS(j-1,i) + an*T_GS(j,i+1) + as*T_GS(j,i-1)); 129 | err_GS = err_GS + power(T_GS(j,i)-T_GS_temp,2); 130 | end 131 | end 132 | err_GS = sqrt(err_GS/(M*N)); 133 | Err_GS(end+1) = err_GS; 134 | n_GS = n_GS+1; % Iterator 135 | end 136 | 137 | % Plotting using Gauss-Seidel Method Results 138 | figure; 139 | x = linspace(0,Lx,M); 140 | y = linspace(Ly,0,N); 141 | [X,Y] = meshgrid(x,y); 142 | contourf(X,Y,T_GS,':','ShowText','on'),colorbar,colormap(jet) 143 | xlabel('X'),ylabel('Y') 144 | title({['Temperature Contour Plot'];['Gauss-Seidel Method'];['Iterations taken : ',num2str(n_GS)]}) 145 | 146 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 147 | 148 | % 3. PSOR Method 149 | % Initializing Temperature Matrix 150 | T_PSOR = zeros(N,M); 151 | T_old_PSOR = zeros(N,M); 152 | 153 | % Finding Optimum Relaxation Factor 154 | B = dx/dy; 155 | w = 1.9; % Optimum Relaxation Factor 156 | 157 | % Computation 158 | epsilon = 1e-8; % Error Sensivity 159 | err_PSOR = 1; % Error 160 | Err_PSOR = []; % Error Array 161 | n_PSOR = 0; % Iterator 162 | 163 | while err_PSOR>epsilon 164 | % For Applying Boundary Conditions(BC) 165 | for i=1:M 166 | % Top BC 167 | T_PSOR(1,i) = (4*T_PSOR(2,i)-T_PSOR(3,i)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 168 | 169 | % Bottom BC 170 | T_PSOR(N,i) = (4*T_PSOR(N-1,i)-T_PSOR(N-2,i)-(2*h*dy/k)*T0)/(3-(2*h*dy/k)); 171 | end 172 | for j=1:N 173 | % Right BC 174 | T_PSOR(j,M) = (4*T_PSOR(j,M-1)-T_PSOR(j,M-2)+(2*h*dy/k)*T0)/(3+(2*h*dy/k)); 175 | 176 | %Left BC 177 | T_PSOR(j,1) = (4*T_PSOR(j,2)-T_PSOR(j,3))/3; 178 | end 179 | 180 | for j=11:31 % Left BC (from node=11 to node=31) 181 | T_PSOR(j,1) = 303; 182 | end 183 | 184 | for j=1:N 185 | for i=1:M 186 | T_old_PSOR(j,i) = T_PSOR(j,i); 187 | end 188 | end 189 | for j=2:N-1 % For Internal Grid points 190 | for i=2:M-1 % For Internal Grid points 191 | % T(j,i)_(k+1)th_iteration Calculation 192 | T_PSOR(j,i) = (1-w)*T_old_PSOR(j,i)+(w/(2*(1+B^2)))*((B^2)*T_PSOR(j,i-1)+T_PSOR(j-1,i)+T_old_PSOR(j,i+1)+(B^2)*T_old_PSOR(j+1,i)); 193 | end 194 | end 195 | err_PSOR = 0; % Error 196 | for j=1:N 197 | for i=1:M 198 | err_PSOR = err_PSOR + power(T_PSOR(j,i)-T_old_PSOR(j,i),2); 199 | end 200 | end 201 | err_PSOR = sqrt(err_PSOR/(M*N)); 202 | Err_PSOR(end+1) = err_PSOR; 203 | n_PSOR = n_PSOR+1; % Iterator 204 | end 205 | 206 | % Plotting using PSOR Method Results 207 | figure; 208 | x = linspace(0,Lx,M); 209 | y = linspace(Ly,0,N); 210 | [X,Y] = meshgrid(x,y); 211 | contourf(X,Y,T_PSOR,':','ShowText','on'),colorbar,colormap(jet) 212 | xlabel('X'),ylabel('Y') 213 | title({['Temperature Contour Plot'];['PSOR Method'];['Iterations taken : ',num2str(n_PSOR)]}) 214 | 215 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 216 | 217 | % Errors VS Iterations Comparison Plot 218 | figure; 219 | x_Jacobi = 1:1:n_Jacobi; 220 | x_GS = 1:1:n_GS; 221 | x_PSOR = 1:1:n_PSOR; 222 | loglog(x_Jacobi,Err_Jacobi,'r-',x_GS,Err_GS,'b-',x_PSOR,Err_PSOR,'g-') 223 | hold on; 224 | x = 1:1:max([n_Jacobi,n_GS,n_PSOR]); 225 | Tolerance = (1e-8)*ones(1,max([n_Jacobi,n_GS,n_PSOR])); 226 | plot(x,Tolerance,'k-'); 227 | hold off; 228 | xlabel('log(Iterations)'),ylabel('log(Error)') 229 | title('Error VS Iterations Comparison') 230 | legend('Jacobi','Gauss-Seidel','PSOR','Tolerance') 231 | --------------------------------------------------------------------------------