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

Programming Assignment 1

Uploaded by

nothenric
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Assignment 1

Uploaded by

nothenric
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

UC Berkeley Math W128A: Programming Assignment 1

Prof. Per-Olof Persson ([email protected])

In this assignment, you will write and use two MATLAB functions for solving f (x) = 0.

1. Implement a MATLAB function findzero of the form


function p = findzero(f, a, b, tol)

which implements the following variant of the bisection and the secant method:
ˆ Initialize w = 1
ˆ Iterate for at most 100 times:
wf (a)(a − b)
1. Compute p = a +
f (b) − wf (a)
2. Output a, b, p, f (p) using fprintf
3. If f (p)f (b) > 0, set w = 1/2, otherwise set w = 1 and a = b
4. Set b = p
5. Terminate if |b − a| < tol or if |f (p)| < tol
You can use the function bisection_table on the course web page as a starting point.
2. Test your function findzero by solving f (x) = cos x − x with a = 0, b = 1, and tol = 10−10 .
Include the printed table in your report, and comment on the apparent order of convergence.
3. Implement a MATLAB function findmanyzeros of the form
function p = findmanyzeros(f, a, b, n, tol)

which nds zeros in the interval [a, b] using the following strategy:
1. Compute n + 1 equidistant points xk , k = 0, . . . , n, between a and b
2. For k = 1, . . . , n, if f (xk ) and f (xk−1 ) have dierent signs, compute a zero using findzero
3. The output vector p should contain all the computed zeros
4. Consider the functions
1
f1 (x) = sin x − e−x
0.5
sin(x2 ) 1
f2 (x) = 2
− e−x/10 0
10 + x 50
-0.5
Run your function findmanyzeros for these functions and f 1 (x)
y=0
their derivatives, on the interval [0, 10] with n = 50 points -1 Zeros
Extrema
and tol = 10−10 . Plot the functions with the computed -1.5
zeros and the local extrema as in the example to the right. 0 2 4 6 8 10

Give a brief comment about the results.


Reporting requirements: We will not run any submitted MATLAB codes. Prepare a report
showing your MATLAB codes, your comments, and other requested information, such as the plots
and the tables with the output from findzero in Problem 2 (no tables needed for Problem 4).

This content is protected and may not be shared, uploaded, or distributed. Page 1/ 1

You might also like