Find the root for three of the following equations with an error tolerance value 0.0001 with maximum number of iterations being 50 using fixed point iteration and the secant method. Give the fixed point equation and starting values (p0 and p1 if appropriate) you choose for each approximation. Save the new value of the approximation (p) after each iteration in a table and compare the results with number of iterations and methods.
(a) f(x) = 3x3−2x2+ 5x−2ex+ 1 in the interval [-1,1]
(b) f(x) =x4+ 3x2−2 in the interval [0,2]
(c) f(x) = 3x−x2+ex−2 in the interval [-2, 2]
The solution is a simple code in python I'll take the program as the problem and all you need to solve any problem to solve equation is to change the limit and the Equation and we will be done.
def f(x):
# we are taking equation here I'll take the first example so I'll #take f(x) = x4+ 3x2−2
f = pow(x, 4) + 3*pow(x,2) - 2
return f;
def secant(x1, x2, E): # we need these parameters as for limits
n = 0; xm = 0; x0 = 0; c = 0;
if (f(x1) * f(x2) < 0):
while True:
x0 = ((x1 * f(x2) - x2 * f(x1)) /
(f(x2) - f(x1)))
c = f(x1) * f(x0);
x1 = x2; #reseting the limits for next iteration
x2 = x0; # if you need reference think of binary search
# update number of iteration
n += 1; # ideally we should not have any limits on iteration to reach a end point and we have been given a tolerance level so I'll keep a count and you can exit() if n=>50
if (c == 0): #if the root is found so we can exit
break;
xm = ((x1 * f(x2) - x2 * f(x1)) /
(f(x2) - f(x1)));
if(abs(xm - x0) < E): # also in case we are too close to root we can close it up as per the Estimate value given by us
break;
print("Root of the given equation =", round(x0, 6));
print("No. of iterations = ", n);
# here if you want add if(n==50) exit() but I'll not suggest that
else:
print("Can not find a root in ",
"the given inteval");
# initializing the values for the different problems here
x1 = 0;
x2 = 1;
E = 0.0001;
secant(x1, x2, E);
Hope this solution is helpful, for a better understanding just check with the secant iteration mathematically or draw a graph you'll understand the solution and how we reached there.
It's basically check-in which side our solution lies and based on that we are changing the limits and after the difference between the 0 and the f(z) where z is the solution is smaller then the tolerance value we can exit as well.
In cases where there is no solution, we need to have a iteration terminal so you can put one for that just remove the comment and I'll suggest take more then 50 as limit
Hope it helps
Get Answers For Free
Most questions answered within 1 hours.