Lab3 (2)
Lab3 (2)
Laboratory: 03
Contents:
Fixed point iteration Secant method
Bisection method Newton Raphson method
Objective:
Understand the basic algorithms for solving nonlinear equations through practical programing using MATLAB
Analyze the efficiency of different algorithms for solving nonlinear equations
Understand the order of convergence of the above five methods
Comparing the advantage and disadvantage of different algorithms through practical programming
Preparations:
All students who attend the lab are expected to read and understand all the algorithms which are dealt in the lecture class
before attending the lab.
3.1. Write MATLAB script that solves the roots of cos(x) - x𝒆𝒙 =0 using fixed point iteration.
a. By taking the appropriate interval try to estimate the order of convergence of the algorithm by plotting the relative error
generate at each iteration? By what parameters the order of convergence of this algorithm depends?
3.2. Write MATLAB script that solves the roots of the function that is taken from the user (from the command window as inline function)
using bisection method.
a. By selecting appropriate interval plot the relative error.
b. What is the advantage of bisection method over the fixed point iteration method?
3.3. Write a MATLAB code to solve the following equation using secant method.
𝑒 𝑥 + 2−𝑥 + 2 cos(𝑥) − 6=0
(use x0=0.5 and x1=1.0)
3.4. Write a MATLAB code to solve the following equations using Newton Raphson method.
a. f (x) = 2cosh(x/4)−x
b. f (x)= sin(x)+x*cos(x)
3.5. After implementing the algorithms using Matlab, try to plot the relative error generated at each iteration and predict the convergence
characteristics of all the methods described above. In addition, by selecting one equation among those given above try to compare each
methods.
Assignment 1:
1. Write a c++ program that solves the equation 𝑒 𝑥 + 2−𝑥 + 2 cos(𝑥) − 6 using bisection method.
2. Write a c++ program that solves the equation x𝑒 𝑥 -x=0 using Newton Raphson method.
__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC
Introduction to Computational Methods Laboratory Manual Semester II, 2015GC
1. Fixed Point Iteration Simplified Matlab Function
function y = fixedpoint(iter_fun,p0,tol,max1)
for k=1:max1
p = iter_fun(p0);
abserr = abs(p-p0);
relerr = abserr/( abs(p)+eps );
if (abserr<tol) && (relerr<tol)
break
end
p0 = p;
end
if (k==max1)
disp('The algorithm did not converge to the specified error level')
end
y = p;
disp(y);
end
__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC