Calculating Variables

a) Write a MATLAB function that implements the bisection method in Algorithm 2.1. Make sure that you use the same order of input below:
function p = bisection(f,a,b,TOL,N0)
% f is the function f
% your implementation here
The ’p’ variable calculated in step 3 will be returned (function p = bisection).
Submit a single file named by your student ID (e.g. huamingy.m) that contains
the function. Your code will be tested on a few test cases. Your grade of this
assignment depends on the number of cases that your code calculates correctly.
Use the example below to test your code. Make sure your code runs without
any errors (which results in a zero grade!).
b) Find a root of the equation 6(e
x − x) = 7 + 3x
2 + 2x
3 between −1 and 1
within 10−6 accuracy, using 300 maximum iterations.
First, define an inline function:
f = @(x) 6(exp(x)-x) – (7+3xˆ2+2*xˆ3);
Then, run the following in the command window:
and your function should return −0.941764.
Some useful Matlab Commands :
1) If-statement. Use the following code for if x < 0 then y = x
if x < 0
y = xˆ2;
2) If-else-statement. Use the following code for if x < 0 then y = x
y = cos(x):
if x < 0
y = xˆ2;
y = cos(x);
3) Various STOP commands. Based on the context, STOP could mean to stop
the execution of the algorithm. In this case, use the return command.
Sometimes STOP could also mean to end a for-loop and continue executing
the remaining command after the for-loop. For example, use the following code
for if x < 0 then STOP FOR LOOP:
if x < 0
4) Use the following code to print a line in the command window:
disp(’blah blah blah’)
To include a variable in the output:
n = 3; disp([’blah repeated for’ num2str(n) ’ times.’])
5) Matlab command for e
If x is an array, exp(x) returns an array with each component of x replaced by
its exponent.
6) Matlab command for | x | :
This is also component-wise.

Sample Solution