0% found this document useful (0 votes)
20 views5 pages

Newton Rapson Method

The document describes the implementation of the Newton-Raphson and Secant methods for solving a system of equations related to temperature calculations. It includes definitions for the functions and their Jacobians, as well as the iterative processes for both methods. The results of the temperature calculations are printed along with checks to verify the accuracy of the solutions.

Uploaded by

isratjahannipa29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Newton Rapson Method

The document describes the implementation of the Newton-Raphson and Secant methods for solving a system of equations related to temperature calculations. It includes definitions for the functions and their Jacobians, as well as the iterative processes for both methods. The results of the temperature calculations are printed along with checks to verify the accuracy of the solutions.

Uploaded by

isratjahannipa29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Newton Rapson Method

import numpy as np

# Define the system of equations

def f(T):

T0, Ti = T

f1 = (7.48e-8) * (2.22e25 - T0**4) - 3600 * (T0 - Ti)

f2 = 916.36 * (Ti - 1073) - 3600 * (T0 - Ti)

return np.array([f1, f2])

# Define the Jacobian matrix

def jacobian(T):

T0, Ti = T

df1_dT0 = -4 * 7.48e-8 * T0**3 - 3600

df1_dTi = 3600

df2_dT0 = -3600

df2_dTi = 916.36 + 3600

return np.array([[df1_dT0, df1_dTi], [df2_dT0, df2_dTi]])

# Newton-Raphson method

def newton_raphson(T0_guess, Ti_guess, tol=1e-6, max_iter=100):

T = np.array([T0_guess, Ti_guess])

for i in range(max_iter):

F = f(T)

J = jacobian(T)

delta_T = np.linalg.solve(J, -F) # Solve J * delta_T = -F


T = T + delta_T # Update T0 and Ti

# Check for convergence

if np.linalg.norm(delta_T) < tol:

print(f"Converged in {i+1} iterations.")

return T

raise RuntimeError("Newton-Raphson method did not converge.")

# Initial guesses for T0 and Ti

T0_guess = 1300 # Initial guess for outside temperature (T0)

Ti_guess = 1200 # Initial guess for inside temperature (Ti)

# Solve the system using Newton-Raphson method

T0_solution, Ti_solution = newton_raphson(T0_guess, Ti_guess)

# Print the results

print(f"Outside temperature of the tube, T0 = {T0_solution:.2f} K")

print(f"Inside temperature of the tube, Ti = {Ti_solution:.2f} K")

# Final check to verify the results with the original equations

f1_check = (7.48e-8) * (2.22e25 - T0_solution**4) - 3600 * (T0_solution - Ti_solution)

f2_check = 916.36 * (Ti_solution - 1073) - 3600 * (T0_solution - Ti_solution)

print(f"f1(T0, Ti) = {f1_check:.6f} (should be close to 0)")

print(f"f2(T0, Ti) = {f2_check:.6f} (should be close to 0)")


Secant Method

import numpy as np

# Function definitions for f1 and f2

def f1(T0, Ti):

return (7.48e-8) * (2.22e25 - T0**4) - 3600 * (T0 - Ti)

def f2(T0, Ti):

return 916.36 * (Ti - 1073) - 3600 * (T0 - Ti)

# Secant method for root finding

def secant_method(f, x0, x1, y0, y1, max_iterations=100, tolerance=1e-6):

for iteration in range(max_iterations):

# Calculate the new approximation for T0 using secant method

f_x0 = f(x0, y0)

f_x1 = f(x1, y1)

if abs(f_x1 - f_x0) < tolerance:

print(f"Converged after {iteration + 1} iterations.")

return x1

x_new = x1 - f_x1 * (x1 - x0) / (f_x1 - f_x0)

# Update values for the next iteration

x0, x1 = x1, x_new


# Check for convergence

if abs(x1 - x0) < tolerance:

print(f"Converged after {iteration + 1} iterations.")

return x1

print("Did not converge.")

return x1

# Iteratively solve for T0 and Ti using the secant method

def solve_system_secant():

# Initial guesses

T0_0 = 1300.0

T0_1 = 1400.0

Ti_0 = 1200.0

Ti_1 = 1250.0

# Secant method iterations

for iteration in range(100):

# Solve for T0 using f1 (with latest Ti)

T0 = secant_method(f1, T0_0, T0_1, Ti_0, Ti_1)

# Solve for Ti using f2 (with latest T0)

Ti = secant_method(f2, Ti_0, Ti_1, T0, T0)

# Update guesses for the next iteration


T0_0, T0_1 = T0_1, T0

Ti_0, Ti_1 = Ti_1, Ti

# Check convergence between T0 and Ti

if abs(T0 - T0_1) < 1e-6 and abs(Ti - Ti_1) < 1e-6:

print(f"Converged after {iteration + 1} iterations.")

return T0, Ti

print("Did not converge.")

return T0, Ti

# Run the solver

T0_solution, Ti_solution = solve_system_secant()

# Output the results

print(f"Outside temperature of the tube, T0 = {T0_solution:.2f} K")

print(f"Inside temperature of the tube, Ti = {Ti_solution:.2f} K")

# Final check to verify the results with the original equations

f1_check = f1(T0_solution, Ti_solution)

f2_check = f2(T0_solution, Ti_solution)

print(f"f1(T0, Ti) = {f1_check:.6f} (should be close to 0)")

print(f"f2(T0, Ti) = {f2_check:.6f} (should be close to 0)")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy