r[0] = y[0]
    r[m] = y[m] - 1.0
    for i in range(1, m):
        r[i] = y[i-1] - 2.0*y[i] + y[i+1]                      \
             - h*h*F(x[i],y[i],(y[i+1] - y[i-1])/(2.0*h))
    return r


def F(x, y, yPrime):  # Differential eqn. y" = F(x,y,y')
    F = -3.0 * y * yPrime
    return F


def startSoln(x):  # Starting solution y(x)
    y = numpy.zeros((m + 1), dtype=float)
    for i in range(m + 1):
        y[i] = 0.5 * x[i]
    return y


xStart = 0.0  # x at left end
xStop = 2.0  # x at right end
m = 10  # Number of mesh intevals
h = (xStop - xStart) / m
x = numpy.arange(xStart, xStop + h, h)
y = newtonRaphson2(residual, startSoln(x), 1.0e-5)
print "\n        x              y"
for i in range(m + 1):
    print "%14.5e %14.5e" % (x[i], y[i])
raw_input("\nPress return to exit")
Exemple #2
0

def r(u):  # Boundary condition residuals-- see Eq. (8.7)
    r = zeros(len(u), type=Float64)
    X, Y = integrate(F, x, initCond(u), xStop, h)
    y = Y[len(Y) - 1]
    r[0] = y[2]
    r[1] = y[3] - 1.0
    return r


def F(x, y):  # First-order differential equations
    F = zeros((4), type=Float64)
    F[0] = y[1]
    F[1] = y[2]
    F[2] = y[3]
    if x == 0.0: F[3] = -12.0 * y[1] * y[0]**2
    else: F[3] = -4.0 * (y[0]**3) / x
    return F


x = 0.0  # Start of integration
xStop = 1.0  # End of integration
u = array([-1.0, 1.0])  # Initial guess for u
h = 0.1  # Initial step size
freq = 1  # Printout frequency
u = newtonRaphson2(r, u, 1.0e-5)
X, Y = integrate(F, x, initCond(u), xStop, h)
printSoln(X, Y, freq)
raw_input("\nPress return to exit")
Exemple #3
0
def initCond(u):  # Initial values of [y,y',y",y"'];
                  # use 'u' if unknown
    return array([0.0, 0.0, u[0], u[1]])

def r(u):  # Boundary condition residuals-- see Eq. (8.7)
    r = zeros(len(u),type=Float64)
    X,Y = integrate(F,x,initCond(u),xStop,h)
    y = Y[len(Y) - 1]
    r[0] = y[2]             
    r[1] = y[3] - 1.0       
    return r
              
def F(x,y):  # First-order differential equations                   
    F = zeros((4),type=Float64)
    F[0] = y[1]
    F[1] = y[2]
    F[2] = y[3]
    if x == 0.0: F[3] = -12.0*y[1]*y[0]**2
    else:        F[3] = -4.0*(y[0]**3)/x
    return F

x = 0.0                    # Start of integration        
xStop = 1.0                # End of integration
u = array([-1.0, 1.0])     # Initial guess for u
h = 0.1                    # Initial step size
freq = 1                   # Printout frequency
u = newtonRaphson2(r,u,1.0e-5)
X,Y = integrate(F,x,initCond(u),xStop,h)
printSoln(X,Y,freq)
raw_input("\nPress return to exit")
def residual(y):  # Residuals of finite diff. Eqs. (8.11)
    r = zeros(m + 1)
    r[0] = y[0]
    r[m] = y[m] - 1.0
    for i in range(1,m):
        r[i] = y[i-1] - 2.0*y[i] + y[i+1]                      \
             - h*h*F(x[i],y[i],(y[i+1] - y[i-1])/(2.0*h))
    return r                 
        
def F(x,y,yPrime):  # Differential eqn. y" = F(x,y,y')
    F = -3.0*y*yPrime
    return F

def startSoln(x): # Starting solution y(x)
    y = zeros(m + 1)
    for i in range(m + 1): y[i] = 0.5*x[i]
    return y
 
xStart = 0.0            # x at left end
xStop = 2.0             # x at right end
m = 10                  # Number of mesh intevals
h = (xStop - xStart)/m
x = arange(xStart,xStop + h,h)
y = newtonRaphson2(residual,startSoln(x),1.0e-5)
print "\n        x              y"
for i in range(m + 1):
    print "%14.5e %14.5e" %(x[i],y[i]) 
raw_input("\nPress return to exit")


#!/usr/bin/python
## example10_4_check

import numpy as np
from newtonRaphson2 import *


def F(x):
    return np.array([
        2.0 * (x[0] - 5.0) + x[2] * x[1], 2.0 * (x[1] - 8.0) + x[2] * x[0],
        x[0] * x[1] - 5.0
    ])


xStart = np.array([1.0, 5.0, 1.0])
print("x = ", newtonRaphson2(F, xStart))
input("Press return to exit")
#
def F(x):
    F = zeros((len(x)), dtype=float64)
    F[0] = x[0] / 1. + x[1] * sin(-30. + x[2]) - 6870.
    F[1] = x[0] / 1. + x[1] * sin(0. + x[2]) - 6728.
    F[2] = x[0] / 1. + x[1] * sin(30. + x[2]) - 6615.
    return F


# Initial guess
x = np.array([
    6800, 0.5, 0
])  # What list needs to be used to initialze the Newton-Raphson (N-R) method?

# Complete the call to the N-R method to solve for unknowns
x = newtonRaphson2(F, x)

# Print the solution vector x from N-R
print()
np.set_printoptions(precision=3)
print('[ C  e  alpha] = ' + np.array_str(x))

# Calculate minimum trajectory and angle using components of x
minTheta = (pi / 2 - x[2]) % (2 * pi)
minR = (3 * pi / 2 - x[2]) % (2 * pi)

# Print minimum trajectory results
print('Minimum trajectory = %.3f km' % minR)
print('Angle at which minimum trajectory occurs = %.3f degrees' % minTheta)
print()
Exemple #7
0
#!/usr/bin/python
## example4_9
from numarray import zeros, array
from math import sin, log
from newtonRaphson2 import *


def f(x):
    f = zeros((len(x)), type=Float64)
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
    f[1] = 3.0 * x[0] + 2.0**x[1] - x[2]**3 + 1.0
    f[2] = x[0] + x[1] + x[2] - 5.0
    return f


x = array([1.0, 1.0, 1.0])
print newtonRaphson2(f, x)
raw_input("\nPress return to exit")
Exemple #8
0
# Dr. Berry told us to start with these values, presumably
# because they are close to the final values for these things

def f(x):
  f = zeros(len(x))
  f[0] = x[0] / (1 + (x[1]*sin(radians(-30) + radians(x[2])))) - 6870
  f[1] = x[0] / (1 + (x[1]*sin(0 + radians(x[2])))) - 6728
  f[2] = x[0] / (1 + (x[1]*sin(radians(30) + radians(x[2])))) - 6615
  return f
# So, really, I borrowed the structure of this function from the text book
# example 4.9/4.10 it just initializes an array 'f' and then, using the
# provided formula and data points, creates a value for each function to
# be used to create the Jacobian matrix used by newtonRaphson2 to solve for
# C, e, and alpha (x[0], x[1], and x[2] respectively.

x = (newtonRaphson2(f,x))
# Necessary call to newtonRaphson2 to solve the system of equations

theta = ((degrees(pi/2.0)) - x[2])  # Setting theta to pi/2 - alpha gives us the minimum value for R
R0 = x[0] / (1 + (x[1]*sin(radians(theta + x[2]))))
# R0 is the minimum value of R that I use my own numbers to solve for, it
# uses my own C, e, theta, and alpha.
  
R1 = x[0] / (1 + (x[1]*1))
# I used this to check and make sure that I was getting
# the right value for theta, R1 is the presumed correct

print (x)
print ("Theta = %f" % (theta))
print ("R0 = %f" % (R0)) # Again, this is my answer
print ("R1 = %f" % (R1)) # And this is presumably the correct answer
Exemple #9
0
#!/usr/bin/python
## example4_10
import numpy as np
import math
from newtonRaphson2 import *


def f(x):
    f = np.zeros(len(x))
    f[0] = math.sin(x[0]) + x[1]**2 + math.log(x[2]) - 7.0
    f[1] = 3.0 * x[0] + 2.0**x[1] - x[2]**3 + 1.0
    f[2] = x[0] + x[1] + x[2] - 5.0
    return f


x = np.array([1.0, 1.0, 1.0])
print(newtonRaphson2(f, x))
input("\nPress return to exit")
Exemple #10
0
#!/usr/bin/python
## example4_9
from numarray import zeros,array
from math import sin,log
from newtonRaphson2 import *

def f(x):
    f = zeros((len(x)),type=Float64)
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
    f[1] = 3.0*x[0] + 2.0**x[1] - x[2]**3 + 1.0
    f[2] = x[0] + x[1] + x[2] - 5.0
    return f

x = array([1.0, 1.0, 1.0])
print newtonRaphson2(f,x)
raw_input ("\nPress return to exit")
Exemple #11
0
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 25 14:22:25 2018

@author: 80504
"""
import numpy as np
import sys
sys.path.append('C:/Users/80504/study/git/kaggle/study/NM_Source') 
from newtonRaphson2 import *
import time
#override
 
if __name__ == '__main__':
    start_time = time.time()
    numbers = [];
    for elem in range(-1000,1000):
        numbers.append(np.array([1.0*elem]))
    for number in numbers:
        newtonRaphson2(lambda x:x*x-1,number)
    print("The DT is",time.time()-start_time)       
Exemple #12
0
from run_kut4 import *
## Use newtonRaphson2 module for finding v0, theta
from newtonRaphson2 import *

tStart = 0.0
tStop = 10.0
h = 0.001

## Initial value for newtonRaphson2
v0 = 50
theta = np.pi / 6
u = [v0 * np.cos(theta), v0 * np.sin(theta)]

## Consider initial value
u = newtonRaphson2(r, u)

ts, xs = integrate(F, tStart, initCond(u), tStop, h)

#from printSoln import *
#printSoln(ts,xs,freq)

## Write new txt file for save solution
f = open('problem8.1.19.txt', 'w')
f.write('u             ' + str(u))
f.write('\nv0            ' + str((u[0]**2 + u[1]**2)**0.5))
f.write('\ntheta         ' + str(np.arctan(u[1] / u[0])))
f.write('\nx(10)         ' + str(xs[len(ts) - 1, 0]))
f.write('\nr(u)          ' + str(r(u)))
f.close()