You are on page 1of 3

Lab 6 | reConfigurable@IITD 20/12/22, 7:57 PM

reConfigurable@IITD

Home

Lab 6
Bisection Method

Write a program to find the roots of a given function using the Bisection method.

The inputs to the program will be:


 

End Points [a, b]


Tolerance c
The function whose root is to be found.

Your program should output the final approximate root AND the number of iterations taken to reach there on separate lines.

The functions f1, f2, f3 have already been defined in your code file and will be referred as strings.

Use MATLAB function feval to evaluate a named function on some parameter. Read its documentation to understand.

In case of any error condition, your program should print -1

NOTES

Do not print unnecessary output. example- input('Input the function')

Use fprintf/printf for displaying data rather than disp.

Output the root upto decimal 4 decimals.

Newton Method 

Write a program to find the roots of a given function using Newton's method (involving derivatives.)

The inputs to the program will be:


 

Initial Guess
Tolerance c
The function whose root is to be found and its derivative.

Your program should output the final approximate root AND the number of iterations taken to reach there on separate lines.

The functions f1, f2, f3 and their derivatives f1p, f2p, f3p have already been defined in your code file and will be referred as strings.

Use Octave

function feval to evaluate a named function on some parameter. Read its documentation to understand.

We are considering relative error i.e. ((new_value - old_value) / old_value)

http://karnali.cse.iitd.ac.in/drupal/?q=node/45 Page 1 of 3
Lab 6 | reConfigurable@IITD 20/12/22, 7:57 PM

In case of any error condition, your program should print -1

NOTES

Do not print unnecessary output. example- input('Input the function')

Use fprintf/printf for displaying data rather than disp.

Output the root upto decimal 4 decimals.

Input format:

"function_name"

"function_derivative_name"

#initial_guess

#tolerance

Here input('') would give a function handle

Here output can be printf("%.4f\n%d\n",ans, i)

Examples:

Input: 

f1

f1p

1.4

0.0001

Output:

1.1656

Explanation:

In the range [1, 1.4] the root of function f1, (with a tolerance of 0.0001) is 1.1656 and it takes 5 iterations for Newton's method to reach there.

Notice that for the same problem it took bisection method to reach 12 iterations.

Input format:

"function_name"

#a

#b

#tolerance

Examples:

http://karnali.cse.iitd.ac.in/drupal/?q=node/45 Page 2 of 3
Lab 6 | reConfigurable@IITD 20/12/22, 7:57 PM

Input: 

f1

1.4

0.0001

Output:

1.1655

12

Explanation:

In the range [1, 1.4] the root of function f1, (with a tolerance of 0.0001) is 1.1655 and it takes 12 iterations for bisection method to reach there.

http://karnali.cse.iitd.ac.in/drupal/?q=node/45 Page 3 of 3

You might also like