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

TPmatlab2 En

The document outlines a MATLAB assignment for the Lebanese University Faculty of Sciences, detailing five exercises focused on root-finding methods including Lagrange's method, bisection method, Newton's method, and secant method. Each exercise requires the creation of a MATLAB function to approximate the roots of a given function and includes a script to visualize the results. The document specifies input parameters such as function, interval, tolerance, and maximum iterations for each method.

Uploaded by

Houssien Alarab
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)
2 views

TPmatlab2 En

The document outlines a MATLAB assignment for the Lebanese University Faculty of Sciences, detailing five exercises focused on root-finding methods including Lagrange's method, bisection method, Newton's method, and secant method. Each exercise requires the creation of a MATLAB function to approximate the roots of a given function and includes a script to visualize the results. The document specifies input parameters such as function, interval, tolerance, and maximum iterations for each method.

Uploaded by

Houssien Alarab
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

Lebanese University M2207 2017-2018

Faculty of Sciences (I) MATLAB Sheet 2

Exercise 1 Write a MATLAB function


function [r]=lagrange(f, a, b, tol, N max)
which takes as input a function f , two reals a and b where f is defined over the interval [a, b], a
tolerance value tol and a maximum number of iterations N max, and returns the approximation
of the root r of f using Lagrange’s method.

Exercise 2 Write a MATLAB function


function [c]=bisectionall(f, a, b, tol, N max)
which takes as input a function f , two reals a and b where f is defined over the interval [a, b], a
tolerance value tol and a maximum number of iterations N max, and returns a vector c containing
all of the values (ck ) obtained by the bisection method. The approximation of the roor r of f
corresponds to the last component of c.

Write also a script that calls the function “bisectionall” and sketches on the same figure the
curve of f and the points (ck , f (ck )) for all k.

Exercise 3 Write a MATLAB function


function [r]=newton(f, df, x0, tol, N max)
which takes as input a function f and its derivative function df , a real x0, a tolerance value tol
and a maximum number of iterations N max, and returns the approximation of the root r of f
using Newton’s method, i.e.
f (xn )
xn+1 = xn − , n≥0
f 0 (xn )

Exercise 4 Write a MATLAB function


function [r]=secant(f, x0, x1, tol, N max)
which takes as input a function f , two reals x0 and x1, a tolerance value tol and a maximum
number of iterations N max, and returns the approximation of the root r of f using the secant
method, i.e.
xn − xn−1
xn+1 = xn − f (xn ), n≥1
f (xn ) − f (xn−1 )
Exercise 5 Run the following MATLAB script that calls the above MATLAB functions.

clear all;
clc;
f = @(x) x.^2+x.*exp(-x);
df = @(x) 2*x+exp(-x)-x.*exp(-x);

a = -2;
b = 2;
fplot(f,[a b]);

Nmax = 20;
tol = 1e-5;

r1 = lagrange(f,a,b,tol,Nmax);
r2 = bisectionall(f,a,b,tol,Nmax);
r3 = newton(f,df,x0,tol,Nmax);
r4 = secant(f,x0,x1,tol,Nmax);

disp([r1,r2(end),r3,r4]);

You might also like