0% found this document useful (0 votes)
1 views

Lab3 (2)

The document is a laboratory manual for a Computational Methods course, outlining various algorithms for solving nonlinear equations using MATLAB, including fixed point iteration, bisection method, secant method, and Newton Raphson method. It aims to teach students the efficiency and convergence characteristics of these algorithms through practical programming assignments. Additionally, it includes MATLAB and C++ code examples for implementing these methods.

Uploaded by

kalkidanasale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab3 (2)

The document is a laboratory manual for a Computational Methods course, outlining various algorithms for solving nonlinear equations using MATLAB, including fixed point iteration, bisection method, secant method, and Newton Raphson method. It aims to teach students the efficiency and convergence characteristics of these algorithms through practical programming assignments. Additionally, it includes MATLAB and C++ code examples for implementing these methods.

Uploaded by

kalkidanasale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Computational Methods Laboratory Manual Semester II, 2015GC

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

2. Bisection Method Matlab function

function root = bisect(f,x1,x2,tol,n) return


f1 = feval(f,x1); % feval is a matlab builtin end
function that evaluates the function f at x1 i.e. it if f3 == 0.0
computes f(x1) root = x3;
if f1 == f0.0; disp(x3);
root = x1; return
return; end
end if f2*f3 < 0.0
f2 = feval(f,x2); x1 = x3;
if f2 == 0.0; f1 = f3;
root = x2; else
return; x2 = x3;
end f2 = f3;
if f1*f2 > 0; end
error('Root is not bracketed in end
(x1,x2)') root = (x1 + x2)/2;
end disp(root);
for i = 1:n end
x3 = 0.5*(x1 + x2); % the maximum number of iteration need to attain
f3 = feval(f,x3); the maximum
if(abs(f3) > abs(f1))&& (abs(f3) > % error tolerance tol can be evaluated by using
abs(f2)) % n = ceil(log(abs(x2 - x1)/tol)/log(2.0));
root = NaN;
3. Simplified Newton Raphson Matlab function

function root = newtonRaphson( f,df,xo,n)


x=xo;
for i=0:n
x=x-f(x)/df(x);
end
root=x;
disp(root);
disp(f(root));
end
4. Simplified Secant Method Matlab Function
function x = secant(f,x0,x1,n)
y0 = f(x0);
y1 = f(x1);
for i = 1:n
x = x1 - (x1-x0)*y1/((y1-y0)+eps);
%eps is included to prevent division by zero
y=f(x)
x0=x1;
y0=y1;
x1=x;
y1=y;
end
root=x;
disp(root);
end

__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC

You might also like