Unit4Lab Report Mayol
Unit4Lab Report Mayol
Gov. Guading Adaza St., Sta. Cruz, Dapitan City Province of Zamboanga del Norte
LABORATORY REPORT 4
ECE 316 NUMERICAL METHODS
SUBMITTED BY:
BSECE-III
SUBMITTED TO:
PROFESSOR
Lab 4
Exercise 1: The f1.m file from the previous lab can be modified in the
following way to return both the value (y) and its derivative (yprime).
function [y,yprime]=f1(x)
% [y,yprime]=f1(x) computes y=x^2-9 and its derivative,
% yprime=2*x
% your name and the date
y=x.^2-9;
yprime=2*x;
f1.m modified
1. Copy this code to a file f1.m or retrieve your f1.m from the previous
lab and modify it to return yprime. Use the following syntax to find the
function value and its derivative at x=3. [value,derivative]=f1(3)
2. Conveniently, this modified version could still be used for the
bisection method as well because Matlab returns only the first value (y)
unless the second is explicitly used. Use the following syntax to find only
the function value. value=f1(3)
Exercise 2:
1. Modify each of the four function m-files f1.m, f2.m, f3.m and f4.m
from Lab 3 to return both the function value and that of its derivative. the
functions are:
f1: y = x^2-9 yprime=???
f2: y = x^5-x-1 yprime=???
f3: y = x*exp(-x) yprime=???
f4: y = 2*cos(3*x)-exp(x) yprime=???
4. What are the values of the function and derivative at x=2 for each of
the five functions?
Simulations:
[value,derivative]=f0(2) value = 1, derivative = 1
[value,derivative]=f1(2) value = -5, derivative = 4
Exercise 3:
1. Open a new, empty m-file named newton.m. You can use either the
menus or the command edit newton.m.
2. Start off the function with its signature line and follow that with
comments. The comments should repeat the signature as a comment,
contain a line or two explaining what the function does, explain what
each of the parameters mean, and include your name and the date.
‘newton’ is a function that takes a function handle f, an initial guess x,
and an optional maximum number of iterations maxIts.
It uses Newton's method to find the root of the function f(x) = 0 by
iteratively updating x.
The loop runs until either the function value f(x) is close enough to zero
(determined by EPSILON) or the maximum number of iterations maxIts
is reached.
The function returns the final x value and the number of iterations numIts
taken to converge or reach the maximum iterations.
Remember to define your specific function f with the signature
[y, yprime]= f(x) and pass it to the newton function to find the root using
Newton's method.
3. From a programming standpoint, the iteration should be limited to a
fixed (large) number of steps. The reason for this is that a loop that never
terminates appears to you as if Matlab remains ``busy'' forever. In the
following code, the special Matlab variable nargin gives a count of the
number of input arguments that were used in the function call. This
allows the argument maxIts to be omitted when its default value is
satisfactory.
if nargin < 3
maxIts=100; % default value if maxIts is omitted
end
4. Define the convergence criterion
EPSILON = 1.0e-8;
It sets EPSILON to 1.0×10 −8, which means that if the absolute value
of the function being solved reaches a value smaller than 1.0×10 −8, the
iteration is considered converged and the method stops. Adjusting the
value of EPSILON can impact the precision and accuracy of the solution
obtained by the iterative method.
5. Some people like to use while loops so that the stopping criterion
appears at
the beginning of the loop. I feel that if you are going to count the
iterations you
may as well use a for statement. Start off the loop with
for numIts=1:maxIts
7.Why are there no quotes around the f in this expression? (Why is it not
feval('f',x) or feval(@f,x)?)
increment = -y / yprime;
This line of code calculates the increment by taking the negative ratio
of the function value y to its derivative yprime. This increment value,
when subtracted from the current x, helps update the approximation to
approach the root of the function.
x = x + increment;
This line of code adds the calculated increment to the current value of
x, effectively updating x to a new approximation that ideally gets closer
to the root of the function.
10. Finish the loop and the m-file with the following statements
errorEstimate = abs(increment);
disp(strcat(num2str(numIts), ' x=', num2str(x), '
error estimate=', ...
num2str(errorEstimate)));
if errorEstimate<EPSILON
return;
end
end
% if get here, the Newton iteration has failed!
error('newton: maximum number of iterations exceeded.')
The return statement causes the function to return to its caller before
all maxIts iterations are complete. If the error estimate is not satisfied
within maxIts iterations, then the Matlab error function will cause the
calculation to terminate with a red error message. It is a good idea to
include the name of the function as part of the error message so you can
find where the error occurred.
The disp statement prints out some useful numbers during the
iteration. For the first few exercises in this lab, leave this printing in the
file, but when the function is being used as part of a calculation, it should
not print extraneous information.
Ans. After updating x with the Newton-Raphson formula, the code
calculates the errorEstimate as the absolute value of the increment.
The disp function prints out the iteration count, the current value of x, and
the error estimate.
If the error estimate is below the specified tolerance EPSILON, the
function returns early.
If the maximum number of iterations maxIts is reached without
convergence, an error message is displayed indicating that the maximum
number of iterations has been exceeded. This helps catch cases where the
method doesn't converge within the specified iterations.
11. Complete the comments at the beginning of the function by replacing
the ``???'' symbols, and use the help newton command to confirm your
comments are correct.
12. Recall the Newton iteration should take only a single iteration when
the function is linear. Test your newton function on the linear function f0
that you wrote in the previous exercise. Start from an initial guess of
x=10. The default value of maxIts (100) is satsifactory, so you can omit it
and use the command
[approxRoot numIts]=newton('f0',10);
The correct solution, of course, is x=1 and it should take a single
iteration. It actually takes a second iteration in order to recognize that it
has converged, so numIts should be 2. If either the solution or the number
of iterations is wrong, you have a bug: fix your code.
13. Test your newton function on the quadratic function f1 that you
wrote in the previous exercise. Start from an initial guess of x=0.1. The
default value of maxIts (100) is satsifactory, so you can omit it. The
correct solution, of course, is x=3. How many iterations are needed?