0% found this document useful (0 votes)
13 views2 pages

Numerical hw1

The document describes a homework assignment to write code to find all roots between 0 and 10 of any function using numerical methods with 0.01% accuracy. It provides skeleton code for a driver function and root finding function that can be modified and used bisection and Newton-Raphson methods are outlined as examples.

Uploaded by

980911seo
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)
13 views2 pages

Numerical hw1

The document describes a homework assignment to write code to find all roots between 0 and 10 of any function using numerical methods with 0.01% accuracy. It provides skeleton code for a driver function and root finding function that can be modified and used bisection and Newton-Raphson methods are outlined as examples.

Uploaded by

980911seo
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

ME301 Numerical Analysis

Homework #1
Due: March 28, 2024
Root finding
Write your own code called hw1 that finds all the roots between 0 and 10 of any function. Use
any method or a combination of methods to find all the roots with 0.01% accuracy or less.

- Put any other subfunctions, if any, in a single file entitled hw1.m, and submit your code only.
- Feel free to start with the following skeleton, but you need to modify it to find all.
- Extensions get extra credits. For example, efficiency, rich outputs, etc.

% skeleton code: the driver


% ==============
% 2024/03/21 Byeongchan Lee
% ==============
function x = hw1()
x = []; % roots initially set to blank

% initial guesses (change this to find all the roots)


for x0 = [0 10]
new1 = findRoot(x0); x=[x new1];
end

% Your code goes here to remove redundant roots within es above


end

% skeleton code: you can change the whole function


% ==============
% 2024/03/21 Byeongchan Lee
% ==============
function x = findRoot(x0)
es = 0.01; % desired accuracy (fixed as stated above)
maxIter = 10; % maximum number of iterations (feel free to change)

% main loop
for i = 1:maxIter

x = x0 - phantom(x0)/pprime(x0); % N-R stepping


ea = norm(1-x0/x)*100; % convergence check

% stopping criterion
if ea < es
return
end

x0 = x; % update info
end

x = NaN; % root not found. Try a different initial guess


end

- Function to be examined: phantom.m


- Gradient: pprime.m
- Second derivative: pdprime.m
- Your code will be evaluated with three trial functions other than the one provided.
The following is a bisection version for your information.

% skeleton code: the driver


% ==============
% 2024/03/21 Byeongchan Lee
% ==============
function x = hw1()
x = []; % roots initially set to blank

% initial guesses (change this to find all the roots)


x0 = [0 10];
new1 = findRoot(x0); x=[x new1];

% Your code goes here to remove redundant roots within es above


end

% skeleton code: you can change the whole function


% ==============
% 2024/03/21 Byeongchan Lee
% ==============
function x = findRoot(x0)
es = 0.01; % desired accuracy (fixed as stated above)
maxIter = 10; % maximum number of iterations (feel free to change)

xl = x0(1); xu = x0(2);
x = (xl + xu)/2;

% main loop
for i = 1:maxIter
if phantom(xl)*phantom(x) < 0
xu = x;
elseif phantom(xl)*phantom(x) > 0
xl = x;
else
return
end

x = (xl + xu)/2;
ea = norm((xu-xl)/(xu+xl))*100; % convergence check

% stopping criterion
if ea < es
return
end
end

x = NaN; % root not found. Try a different initial guess


end

You might also like