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
end
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:
bisection(f,-1,1,1e-6,300)
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
2
:
if x < 0
y = xˆ2;
end
2) If-else-statement. Use the following code for if x < 0 then y = x
2
else
y = cos(x):
if x < 0
1
y = xˆ2;
else
y = cos(x);
end
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
break
end
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
x
.
exp(x);
If x is an array, exp(x) returns an array with each component of x replaced by
its exponent.
6) Matlab command for | x | :
abs(x);
This is also component-wise.

Sample Solution

This question has been answered.

Get Answer