All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM - To incorporate multivariate newton raphson solver for a time marching problem OBJECTIVE - To create the python code that solves for transient multivariate newton raphson method and see if the implicit euler method works for varirying time step values. THEORY - In transient multivariate newton…
Amol Patel
updated on 21 Sep 2021
AIM - To incorporate multivariate newton raphson solver for a time marching problem
OBJECTIVE - To create the python code that solves for transient multivariate newton raphson method and see if the implicit euler method works for varirying time step values.
THEORY - In transient multivariate newton raphson solver we will have to create a code that will use implicit euler method or what we call as backward differencing scheme where the values of current timestep are calculated using the values of current and past time step.
For this problem we have the following set of equations
dy1dt=(-0.04y1+104y2y3)dy1dt=(−0.04y1+104y2y3)
dy2dt=(0.04y1-104y2y3-3⋅107y22dy2dt=(0.04y1−104y2y3−3⋅107y22
dy3dt=3â‹…107y22dy3dt=3â‹…107y22
to solve this set of nonlinear equation using implicit euler method we will have to convert this equation into the form that represents the values at both the current and past timesteps.
this can be done by using the annotations like yniyni denotes the values for at the current time step and yn-1iyn−1i denoted the past time step values.
from the numerical definition of the derivative using backward differencing scheme
dyidt=yni-yn-1iΔtdyidt=yni−yn−1iΔt
so our set of equation will become
yn1-yn-11Δt=-0.04yn1+104yn2yn3yn1−yn−11Δt=−0.04yn1+104yn2yn3
yn2-yn-12Δt=0.04yn1-104yn2yn3-3⋅107(yn2)2yn2−yn−12Δt=0.04yn1−104yn2yn3−3⋅107(yn2)2
yn3-yn-13Δt=3⋅107(yn2)2yn3−yn−13Δt=3⋅107(yn2)2
now we will be arranging the equations by taking the terms on the LHS to the RHS
yn-11+Δt⋅(-0.04yn1+104yn2yn3)-yn1=0yn−11+Δt⋅(−0.04yn1+104yn2yn3)−yn1=0
yn-12+Δt⋅(0.04yn1-104yn2yn3-3.107(yn2)2)-yn2=0yn−12+Δt⋅(0.04yn1−104yn2yn3−3.107(yn2)2)−yn2=0
yn-13+Δt⋅(3⋅107(yn2)2)-yn3=0yn−13+Δt⋅(3⋅107(yn2)2)−yn3=0
now we will use this equation in our code to solve using the multivariate newton raphson method
also here we have to incorporate the numerical derivative technique to calculate the jacobian in our calculations for that case we will assume that we have 2 node that are very close to each other at a distance h , keeping h very small about 1e-6 we will find the numerical derivative for the functions.
the numerical derivaitve for the first jacobian term will be given with respect to the y1y1 as shown below
fi′(y1,y2,y3,other variables)=fi(y1+h,y2,y3,other variables)-fi(y1,y2,y3,other variables)h
similarly we can find the other derivatives terms in the jacobian using this logic
Now to write the code first we need to create funtions for each equation that we have also we will create a function to solver for the jacobian .Then we will be having our outer time loop inside which we will have our newton raphson loop(while loop)that will solve iterativesly untill a desired convergence is reached. finally we will be plotting the value for each variables at the end of each time step.
CODE -
Following is the code that is used to solve as set of nonlinear coupled equations using a multivariate newton raphson method.
"""
For this project the followin set of equations will be
solved using multivariate newton raphson method.
dy1/dt = -0.04y1 +10^4y2y3
dy2/dt = 0.04y1 - 10^4y2y3 - 3.10^7y2^2
dy3/dt = 3.10^7 y2^2
modules to be imported
1. numpy module for matrix calculations
2. inv module from the numpy.linalg for inv of matrix
"""
import numpy as np
import numpy.linalg as npla
import matplotlib.pyplot as plt
def f1(y1,y2,y3,y1_old,dt):
"""
first non-linear equation
"""
return y1_old + dt*(-0.04*y1 + pow(10,4)*y2*y3) - y1
def f2(y1,y2,y3,y2_old,dt):
"""
second non-linear equation
"""
return y2_old + dt*(0.04*y1 - pow(10,4)*y2*y3 - 3*pow(10,7)*pow(y2,2)) - y2
def f3(y1,y2,y3,y3_old,dt):
"""
third non-linear equation
"""
return y3_old + dt*(3*pow(10,7)*pow(y2,2)) - y3
def jacobian(y1,y2,y3,y1_old,y2_old,y3_old,dt,h):
# creating the jacobian matrix
J = np.ones((3,3))
# to calculate jacobian numerically we will consider two nodes at distance h apart
# row 1
J[0,0] = (f1(y1+h,y2,y3,y1_old,dt) - f1(y1,y2,y3,y1_old,dt))/h
J[0,1] = (f1(y1,y2+h,y3,y1_old,dt) - f1(y1,y2,y3,y1_old,dt))/h
J[0,2] = (f1(y1,y2,y3+h,y1_old,dt) - f1(y1,y2,y3,y1_old,dt))/h
# row 2
J[1,0] = (f2(y1+h,y2,y3,y2_old,dt) - f2(y1,y2,y3,y2_old,dt))/h
J[1,1] = (f2(y1,y2+h,y3,y2_old,dt) - f2(y1,y2,y3,y2_old,dt))/h
J[1,2] = (f2(y1,y2,y3+h,y2_old,dt) - f2(y1,y2,y3,y2_old,dt))/h
# row 3
J[2,0] = (f3(y1+h,y2,y3,y3_old,dt) - f3(y1,y2,y3,y3_old,dt))/h
J[2,1] = (f3(y1,y2+h,y3,y3_old,dt) - f3(y1,y2,y3,y3_old,dt))/h
J[2,2] = (f3(y1,y2,y3+h,y3_old,dt) - f3(y1,y2,y3,y3_old,dt))/h
return J
# intial guess values
y1=1
y2=0
y3=0
y1_old = 1
y2_old = 0
y3_old = 0
# numpy inital guess array Y_old
Y_old = np.ones((3,1))
Y_old[0] = y1_old
Y_old[1] = y2_old
Y_old[2] = y3_old
#numpy inital array Y
Y = np.ones((3,1))
Y[0] = y1
Y[1] = y2
Y[2] = y3
# nodal distance h
h= 1e-6
# relaxation factor
alpha = 1
# tolerance
tol = 1e-9
# total time in seconds
total_t = 600
dt = 1e-1 # timestep value
n_t = total_t/dt # total number of timesteps
# initial array F
F = np.copy(Y_old)
# array to store the value of the variables
Y1_final = []
Y2_final = []
Y3_final = []
# time-step counter
t_s = 1
#time loop
for i in range (1,(int(n_t)+1)):
# iteration counter
iter = 1
# initial error
error = 9e9
# newton raphson loop
while error > tol:
J = jacobian(Y[0],Y[1],Y[2],Y_old[0],Y_old[1],Y_old[2],dt,h)
# computing the F vector
F[0] = f1(Y[0],Y[1],Y[2],Y_old[0],dt)
F[1] = f2(Y[0],Y[1],Y[2],Y_old[1],dt)
F[2] = f3(Y[0],Y[1],Y[2],Y_old[2],dt)
#computing new values
Y_new = Y - alpha*np.matmul(npla.inv(J),F)
# computing the error
error = np.max(np.abs(Y-Y_new))
#updating the values of Y
Y = Y_new
# log message for each iteration
log_message1 = "iteration # = {0} ,y1 = {1} ,y2 = {2} ,y3 = {3}".format(iter,Y_new[0],Y_new[1],Y_new[2])
print(log_message1)
# update iteration number
iter = iter +1
# updating the Y_old values
Y_old = Y_new
# storing the value of the new values in the array at the end of each time step
Y1_final.append(Y_new[0])
Y2_final.append(Y_new[1])
Y3_final.append(Y_new[2])
# log message for each time-step
log_message = "timestep # = {0} ,y1 = {1} ,y2 = {2} ,y3 = {3}".format(t_s,Y[0],Y[1],Y[2])
print(log_message)
# update the timestep number
t_s = t_s + 1
# plotting the trend of the variables
plt.plot(Y1_final)
plt.plot(Y2_final)
plt.plot(Y3_final)
plt.xlabel('number of time-steps')
plt.ylabel('variables')
plt.legend(["Y1","Y2","Y3"])
plt.title("For dt = "+str(dt)+" sec with total time = "+str(total_t)+" seconds ")
plt.grid("on")
plt.show()
the output that we get form running this code is shown below
in the code we can vary the value of time-step 'dt' and still we will get the same result.
In our case we have used 'dt = 0.1' as this value is good enough to get good results and is also quick , we can further increase the value of dt and get result more quicker but the values will be a little approximate and not that accurate though the solution will be always stable.
Further decreasing the timestep will resulting more computation cost as well as time.
Below are some output results with varying the value of dt in the code at line number 91.
For dt = 1
For dt = 0.5
For dt = 0.01
for dt = 0.01 it takes a lot of time to solve and not recomended as it is costlier.
CONCLUSION -
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 6: Conjugate Heat Transfer Simulation
AIM- To simulate a Conjugate Heat Transfer flow through a pipe, while the inlet Reynolds number should be 7000. To run the grid independance test on 3 grids and show that the outlet temperature converges to a particular value To observe the effect of various supercycle stage interval on the total simulation time.…
09 Nov 2022 06:55 AM IST
Week 7: Shock tube simulation project
AIM - To set up a transient shock tube simulation Plot the pressure and temperature history in the entire domain Plot the cell count as a function of time SHOCK TUBE- The shock tube is an instrument used to replicate and direct blast waves at a sensor or a model in order to simulate actual explosions…
07 Nov 2022 09:18 PM IST
Week 5: Prandtl Meyer Shock problem
AIM - 1. To understand what is a shock wave. 2. To understand the what are the different boundary conditions used for a shock flow problems. 3. To understand the effect of SGS parameter on shock location. 4. To simulate Prandalt Meyer Shcok Wave. OBJECTIVE - Que 1. What is Shock Wave? A shock wave or shock,…
01 Nov 2022 06:36 PM IST
Week 4.2: Project - Transient simulation of flow over a throttle body
AIM - Transient simulation of flow over a throttle body. OBJECTIVE - Setup and run transient state simulation for flow over a throttle body. Post process the results and show pressure and velocity contours. Show the mesh (i.e surface with edges) Show the plots for pressure, velocity, mass flow rate and total…
12 Feb 2022 07:08 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.