import numpy as np;
import matplotlib.pyplot as plt;
plt.rcParams.update({"text.usetex":True});
%config InlineBackend.figure_format = "svg"
fsolve
import scipy.optimize as sco
def sysfunc(x):
return [x[0]*np.exp(x[0] + x[1])-2, x[0]*x[1] - 0.1*np.exp(-x[1])];
sol = sco.fsolve(sysfunc, [1, 1]); #provide the system of equations and guess value
print(sol)
re1, re2 = sysfunc(sol);
print(re1, re2)
def g(x): # define the system of equations as function
return np.array([2*np.exp(-(x[0] + x[1])), 0.1/x[0]*np.exp(-x[1])]); #convert list to numpy array
# let's first plot the two implicit functions
x = np.linspace(0, 1);
y = np.linspace(0, 1);
[X, Y] = np.meshgrid(x, y);
F1 = X*np.exp(X+Y) - 2;
F2 = X*Y - 0.1*np.exp(-Y);
plt.contour(X, Y, F1, [0]);# plot the first function
plt.contour(X, Y, F2, [0]);# plot the second function
Niter = 100;
x = np.zeros((Niter, 2));
x[0,:] = np.array([1, 1]); # guess value 1,1
count = 1;
for i in np.arange(1, Niter):
x[count,:] = 0.6*x[count-1,:]+ 0.4*g(x[count-1,:]);
count = count + 1;
# plot all the iterated values
plt.plot(x[:,0], x[:,1], '-ok', linewidth=1);
def f(x): # function
return 1-x**2;
def dfdx(x): # derivative of function
return -2*x;
xa = np.linspace(-2, 2);
ya = f(xa);
plt.plot(xa, ya);
plt.plot(xa, 0*xa); # plot the y=0 line
Niter = 10;
x = -0.3; # guess value
for i in np.arange(0, Niter):
plt.plot([x, x], [0, f(x)], '-k');
x0 = x;
x = x - f(x)/dfdx(x);
plt.plot([x0, x], [f(x0), 0], '-r');
def f(z):
return 1-z**4;
def dfdz(z):
return -4*z**3;
def iterf(z):
Nitermax = 100;
count = 1;
err = 1;
err_threshold = 1e-6;
while err>err_threshold and count < Nitermax:
zn = z - f(z)/dfdz(z);
err = np.abs(1-zn/z);
z = zn;
count = count +1;
return z, count
x = np.linspace(-1.50, 1.5, 200);
y = np.linspace(-1.5, 1.5, 200);
[X, Y] = np.meshgrid(x, y);
Z = X+1j*Y; # The complex plane
C = np.zeros(np.shape(X)); # stores the number of iteration required to reach the defined convergence criteria of each point in the argand plane
for i in np.arange(0, np.shape(Z)[0]):
for j in np.arange(0, np.shape(Z)[1]):
z, C[j,i] = iterf(Z[j,i]);
#print(Z[j, i], np.round(z, 1), C[j,i] );
plt.imshow(C); plt.colorbar();
ax = plt.gca();
ax.set_aspect(1);
Till now we have observed the regions of convergence. Now let's look at which root does the iteration converge to depending upon the initial guess value.
NaN
value. NaN means not a number. def f(z):
return 1-z**4;
def dfdz(z):
return -4*z**3;
def iterf(z, Nitermax):
count = 1;
err = 1;
err_threshold = 1e-6;
while err>err_threshold and count < Nitermax:
zn = z - f(z)/dfdz(z);
err = np.abs(1-zn/z);
z = zn;
count = count +1;
return z, count
x = np.linspace(0.25, 0.28, 500);
y = np.linspace(0.25, 0.28, 500);
[X, Y] = np.meshgrid(x, y);
Z = X+1j*Y;
Nitermax = 200;
total_roots = 4;
root_array = np.zeros(total_roots, dtype=complex);
root_count = 0;
C = np.zeros(np.shape(X));
for i in np.arange(0, np.shape(Z)[0]):
for j in np.arange(0, np.shape(Z)[1]):
z, count = iterf(Z[j,i], Nitermax);
z = np.round(z, 1);
if count < Nitermax:
if z not in root_array:
root_array[root_count] = z;
root_count = root_count + 1;
if z in root_array:
C[j,i] = np.where(root_array == z)[0];
else:
C[j,i] = np.nan;
plt.imshow(C); plt.colorbar();
print(root_array)