Question: The instructions and a sample problem in ROOT FINDING are contained in the attached Matlab code (that has a main code as well as a
The instructions and a sample problem in ROOT FINDING are contained in the attached Matlab code (that has a main code as well as a function code; it's named instructions, but it's a code with some work already done for you). Your job is to flesh out the main code therein and submit a working code, with the usual preliminaries (diary, etc.). All you need for completing this assignment is, besides the present instructions, is a mastery of the Matlab basics (especially anonymous functions and script functions Code is below
%% Regula Falsi (also known as "False Position") is a root-finding method that requires two initial guesses that bracket the root
% REVISED 06/16/2022 READ CAREFULLY!
% here, a Matlab function that renders this method is provided for you below.
% the input arguments calling the function are:
%
% # f the name of a function to evaluate f(x) on the fly, given the current value of x;
% # a the lower-bound of your initial guess for the interval in which the root lies
% # b the upper-bound of your initial guess for the interval in which the root lies
% # Note: these bounds, a & b, are usually apparent from the problem context
% # delta the convergence tolerance for the root; i.e. keep iterating until the root changes by less than this value
% # epsilon the convergence tolerance for the function value i.e. keep iterating until the function changes by less than this value
% # max1 maximum number of iterations (the answer, if convergence is true, shouldn't change if this number is increased
% to learn more about the regula falsi method, consult the link below:
%%
%
%
% so, the function that finds the root is provided for you.
% what you need to do is to first write the code for another Matlab function named, for example, fcalc,
% for some function of your choice: e.g. f(x) = exp(x) -3*x; it could be an anonymous one-liner function
% or a Matlab function script/code (like the regula-falsi one below). Given x, it should calculate or return a value for f(x)
% then you have to guess a value for a and one for be such that you expect the root of f(x), i.e.
% the value of x that makes f(x) = 0. In the example function above, a = 0 and b = 1 should work.
% set both delta and epsilon to 10^-6 and max1 = 20.
% that is, you have to modify this part of the code, which is the main code, to define f and assign values to a, b, etc.
% then the following line will invoke the root-finding function and get the job done
%[c,err,yc] = regula_falsi(@fcalc, a, b, delta, epsilon, max1); % notice how the function name fcalc is conveyed to the root-finder
%that line has a serious typo! No @ needed. Correct version below
[c,err,yc] = regula_falsi(func0, a, b, delta, epsilon, max1);
%---------------------------------
% A REMINDER ABOUT VARAIABLE NAMES
%---------------------------------
%{
Variables in the arguments list when calling a function
(saved in the same directory or appended at the bottom of the main code)
NEED NOT have the same names as in the function!
Again, names can be different (but do not have to be different either).
Some names can be the same in main & function, or all names can be different in the two domains.
Just the placement (i.e. order in the list of arguments) matters.
This flexibility is very useful in keeping meanings clear.
For example, that last command could ALTERNATIVELY be
[root_regula_falsi, error_regula_falsi, function_converged_value] = regula_falsi(func0, root_lower_bound, root_upper_bound, root_tolerance, function_tolerance, max_number_iterations);
See how understandable the latter call is?
With this enlightnment, let us revise the instructions from above:
INPUT ARGUMENTS: (input from the point of view of the root finder function, here regula_falsi.m)
root_lower_bound = 0;
root_upper_bound = 1;
root_tolerance = 10^-6; % this is a very crude tolerance; in a real problem, you have to make it finer; say 10^-10
function_tolerance = 10^-6; %ditto
max_number_iterations = 20; % again, in a real problem, 100 instead of 20
regula_falsi will interpret the above specs as
a = 0;
b = 1;
delta = 10^-6;
epsilon = 10^-6;
max1= 20;
Don't edit the names in that function! Let it use its own variable names!
OUTPUT ARGUMENTS: (output, from the point of view of the root finder function, here regula_falsi.m)
c = 1.3 (I just made this up! It will be the actual root found.)
err = 10^-7
yc = 10^-7
your main code will then interpret these results as
root_regula_falsi = 1.3
error_regula_falsi = 10^-7
function_converged_value = 10^-7
What happens in regula falsi stays in regula falsi (with apologies to Vegas!).
These results are all the main code cares about.
So far, the only thing we haven't addressed is that pesky func0 or fcalc aka f.
incidentally, func0 (you may use other names for it) is what is known as
the "objective function."
in root finding, the objective is to find roots.
in optimization problems, to find maxima or minima
the task at hand is, given a general purpose Matlab utility like fsolve
or a user-written Matlab function like regula falsi,
(general purpose? it can be used to process any objective function)
how to convey (to that general purpose root finder or optimizer)
the particular objective function (that will vary from problem to problem)?
%}
%----------------------------------------------------------------------------------------
% USING EXERCISE 1 to SEGUE INTO EXERCISE 2 (and beyond) to convey the objective function
%----------------------------------------------------------------------------------------
%lift a few lines from Exercise 1 to arrive at this func0 below.
% Exercise 1 shows the way. Here goes:
syms x % this declaration is a must, for the independent or input variables
% get the mathematical function (aka equation or expression) from the user
sc = inputdlg(' Type, using the Matlab convention for arithmetic, the mathematical function or expression (of variable x):' );
% you would enter, in this exercise, exp(x)-3*x
s = sc{:}; % this "colon" puts the string into a column vector; note the curly brackets
% it's important to convert the user's string input into a symbol
func = str2sym(s)
% setting up the Matlab function for the input Mathematical function or expression
% converting the symbolic expression for the function func0 into a Matlab function script OR a function handle (anonymous function)
%
fprintf(' Matlab Function for the input function')
func0 = matlabFunction(func)
% "func0" without the quotes can be used in an arguments-list (when calling root-finding function)
% As far as Exercise 2 is concerned, we may stop here and run with func0.
% Alternatively the "function handle" generated by Matlab can copied and pasted as an anonymous in-line function to evaluate f(x)
% e.g. User-input mathematical function: exp(x) - 3*x Matlab-generated anonymous function or function handle: @(x)x.*-3.0+exp(x)
% Study Snyder Notes Part 2 to learn more about these two types of Matlab function
%-------------------
% BACK TO EXERCISE 2
%-------------------
% finally, you have to write a few print or display commands to print the all the input values and the outputs returned by the root-finder.
% for something extra, you may add a few print ststements to the function below to display how the iteration progresses;
% i.e. k, a, b, c, and yc inside the for loop.
function [c,err,yc] = regula_falsi(f, a, b, delta, epsilon, max1)
% Input - f is the function input as a string 'f'
% - a and b are the left and right endpoints that bracket the root
% - delta is the tolerance for the root
% - epsilon is the tolerance for the value of f at the root
% - max1 is the maximum number of iterations
% Output - c is the root
% - yc=f(c)
% - err is the error estimate for c
ya = feval(f, a);
yb = feval(f, b);
% for this "regula falsi" root-finding routine to work, the endpoints must bracket the root.
% That is, the function must be positive at one endpoint and
% negative at the other endpoint.
if ya * yb > 0
disp('Note: f(a)*f(b) > 0'),
return,
end
for k = 1 : max1
dx = yb * (b - a) /(yb - ya);
c = b - dx;
ac = c - a;
yc = feval(f, c);
if yc == 0, return;
elseif yb * yc > 0
b = c;
yb = yc;
else
a = c;
ya = yc;
end
dx = min(abs(dx), ac);
if abs(dx) < delta, return, end
if abs(yc) < epsilon, return, end
end
c;
err = abs(b - a)/2;
yc = feval(f, c);
return
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
