# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import matplotlib.pyplot as plt
import numpy as np
from numpy import cosh
from math import log
from math import exp


#part (c)
def function(t):
    return np.log(cosh(t))
def order1(x):
    return (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))
def order2(x):
    return 4/(np.exp(x)+np.exp(-x))**2
def Q(x,y):
    return function(y)+order1(y)*(x-y)+0.5*(x-y)**2


t = np.arange(-5,5.1,0.5)
plt.plot(t,function(t).astype(np),'r',label='f(x)')
plt.plot(t,Q(t,-2).astype(np),'g',label='Q(x,-2)')
plt.plot(t,Q(t,3).astype(np),'b',label='Q(x,3)')
plt.legend(loc='upper right')
plt.xlabel('value of x')
plt.ylabel('corresponding value of functions')
plt.show()


# +
#part (e)
x_begin = -2  #generate a random x0 to begin
def update(x):
    x_new = x-order1(x)
    return x_new
iteration_time = 10
xval=[x_begin]
for i in range(iteration_time):
    x_begin = update(x_begin)
    xval.append(x_begin)
n=list(range(iteration_time+1))
plt.plot(n,xval)
plt.xlabel('iteration times   n')
plt.ylabel('value of x(n)')
plt.title('x(n) vs n with initial guess x0 = -2')
plt.show()
    

                       
                       
# -

x_begin = 3  #generate a random x0 to begin
def update(x):
    x_new = x-order1(x)
    return x_new
iteration_time = 10
xval=[x_begin]
for i in range(iteration_time):
    x_begin = update(x_begin)
    xval.append(x_begin)
n=list(range(iteration_time+1))
plt.plot(n,xval)
plt.xlabel('iteration times   n')
plt.ylabel('value of x(n)')
plt.title('x(n) vs n with initial guess x0 = 3')
plt.show()

#(g)
t = np.arange(1.0875,1.09,0.0001)
plt.plot(t,(abs(order1(t)/order2(t))/abs(t)).astype(np),'r',label='f(x)')
plt.show()


x_begin = 1.0886 #generate a random x0 to begin
def update(x):
    x_new = x-order1(x)/order2(x)
    return x_new
iteration_time = 15
xval=[x_begin]
for i in range(iteration_time):
    x_begin = update(x_begin)
    xval.append(x_begin)
n=list(range(iteration_time+1))
plt.plot(n,xval)
plt.xlabel('iteration times   n')
plt.ylabel('value of x(n)')
plt.title('x(n) vs n with initial guess x0 = 1.0886')
plt.show()

x_begin = 1.0887 #generate a random x0 to begin
def update(x):
    x_new = x-order1(x)/order2(x)
    return x_new
iteration_time = 15
xval=[x_begin]
for i in range(iteration_time):
    x_begin = update(x_begin)
    xval.append(x_begin)
n=list(range(iteration_time+1))
plt.plot(n,xval)
plt.xlabel('iteration times   n')
plt.ylabel('value of x(n)')
plt.title('x(n) vs n with initial guess x0 = 1.0887')
plt.show()


#(h)
def g(x):
    result=0
    for i in range(10):
        k=i+1
        result += np.log(cosh(x+2/k**(1/2.0)))
    result = result/10
    return result
t = np.arange(-6,5.1,0.1)
plt.plot(t,g(t).astype(np),'r')
plt.xlabel('value of x')
plt.ylabel('value of g(x)')
plt.show()


# +
#(j)
def g1(x):
    result = 0
    for i in range(10):
        k=i+1
        result += (np.exp(x+2/k**(1/2.0))-np.exp(-x-2/k**(1/2.0)))/(np.exp(x+2/k**(1/2.0))+np.exp(-x-2/k**(1/2.0)))
    result = result/10
    return result
x_begin = -5  #generate a random x0 to begin
def update(x):
    x_new = x-g1(x)
    return x_new
iteration_time = 14
xval=[x_begin]
for i in range(iteration_time):
    x_begin = update(x_begin)
    print(x_begin)
    xval.append(x_begin)
n=list(range(iteration_time+1))
plt.plot(n,xval)
plt.xlabel('iteration times   n')
plt.ylabel('value of x(n)')
plt.title('x(n) vs n with the x0 = -5')
plt.show()
    

# -



StarcoderdataJupyterScriptsDedupFiltered