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

Unit4Lab Report Mayol

The document describes exercises completed for a numerical methods laboratory report. It includes modifying existing functions to return both the function value and derivative, testing the modified functions, and writing a newton.m function to implement Newton's method for finding roots of functions. The newton.m function takes a function handle, initial guess, and maximum iterations as inputs and returns the approximated root and number of iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit4Lab Report Mayol

The document describes exercises completed for a numerical methods laboratory report. It includes modifying existing functions to return both the function value and derivative, testing the modified functions, and writing a newton.m function to implement Newton's method for finding roots of functions. The newton.m function takes a function handle, initial guess, and maximum iterations as inputs and returns the approximated root and number of iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JOSE RIZAL MEMORIAL STATE UNIVERSITY

The Premier University in the Province of Zamboanga del Norte

Gov. Guading Adaza St., Sta. Cruz, Dapitan City Province of Zamboanga del Norte

LABORATORY REPORT 4
ECE 316 NUMERICAL METHODS

SUBMITTED BY:

ALLEN JAY M. MAYOL

BSECE-III

SUBMITTED TO:

ENGR. GILLERT M. BONGCAC

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)

3. When it is necessary to call the function f1 from inside a function such


as newton, Matlab's feval function still works. The notation for this is
similar: [value,derivative]=feval('f1',3). Use this syntax at the command line.
You should get the same values as before.
4. Although the syntax [value,derivative] appears similar to a vector with
two components, it is not that at all! You will see later in this course cases
where value is a vector and derivative is a matrix. In any case, you can
see from the following commands
v=[10,11] %v is a 2-component vector
v=f1(3) %v has become a number, since f1 returns a number
%unless the derivative is explicitly requested.

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=???

Modified f1: y = x^2-9 yprime = 2*x


Modified f2: y = x^5-x-1 yprime = 5*x^4 - 1

Modified f3: y = x*exp(-x) yprime = exp(-x) * (1 - x)

Modified f4: y = 2*cos(3*x)-exp(x) yprime = -6 * sin(3 * x) - exp(x)


2. Use the help f1 command to confirm that your comments include at
least the formula for f1, and similarly for f2, f3 and f4.

3. Add a function 3. f0(x)=x-1. Don't forget the derivative.

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

[value,derivative]=f2(2) value = 29, derivative = 79

[value,derivative]=f3(2) value = 0.2707, derivative = -0.1353


[value,derivative]=f3(2) value = -5.4687, derivative = -5.7126

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

This for loop iterates a maximum of maxIts times, performing the


Newton-Raphson method steps within the loop. If the convergence
criterion is met (i.e.,∣f(x)∣<EPSILON), the loop exits early and returns
the result. This approach offers a clear iteration count and control over
the number of steps taken.
6. Evaluate the function and its derivative at the current point x using
feval, much as was done in bisect.m. Remember that it is customary to
indent the statements within a loop.You may choose the variable names
as you please. Recall that the syntax for using two values from a single
function call is given above. (If you are using a newer version of Matlab
and you plan on using the @, you can still use feval or not, as you wish.)

7.Why are there no quotes around the f in this expression? (Why is it not
feval('f',x) or feval(@f,x)?)

A function handle in MATLAB is essentially a MATLAB data type


that represents a function. When you pass f as an argument to feval, it
assumes that f is a function handle variable that already points to a
function, not a string literal or the name of the function as a string.
8. Define a Matlab variable increment as the negative ratio of the
function value divided by the value of its derivative. This is the right side
of Equation (1).

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.

9. Complete Equation (1) with the statement

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?

You might also like