Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must be interactive Write a MATLAB program that implements all the nonlinear equ

ID: 3802876 • Letter: M

Question


Must be interactive Write a MATLAB program that implements all the nonlinear equations root finding algorithms we discussed in class. The program should display a menu with a list of methods names in addition to an exit option. Once an option is selected, a function of the specified algorithm should be invoked. Each algorithm should ask for its necessary inputs. Remember that Muller, Bairstow and roots only works with polynomials. Bisection False Position Fixed-Point iteration Newton Secant Modified Secant MATLAB fzero Muller Bairstow MATLAB roots Exit

Explanation / Answer

This seems to be assignment problem so instead of giving away the solution, I am going to direct/help you on how to approach and do the problem. I assume you have implementations to all the algorithms discussed in your class so that we can concentrate on building the interactive menu. The algorithms implementations can simply be added and invoked from the menu program.

The program should display a menu with above options when it is started or executed.
How to do that? You can use series of disp methods to display each menu option at a time like:

disp('1. Bisection');
disp('2. False Position');
....
....

disp('11. Exit');

Then ask the user to choose an option using input method.
%choice selection
choice=input('Please select your choice: ');

That seems a bit of code & work to get a simple menu to be displayed. So MATLAB offered a cool built-in method menu that does the same for you. You can get the same menu along with prompting the user to choose an option as follows:

choice = menu('Select an option', 'Bisection', 'False Position', ..., 'Exit');

You can also place the options in an array and use the array in the menu functions as the second argument. Also, menu ensures that a valid number was chosen, so you'd no longer need that check in your code.

Now, based on the user selection you have to invoke the respective algorithm. You can do this using switch case.

switch choice
case 1
bisection()
case 2
false_position()
....
....
otherwise
exit
end

Then write the respective functions as follows:

function bisection()
% ask the user for required inputs using input method

% then your implementation of the algorithm here

Use input method to take required inputs from the user, as initial_value = input('Enter initial value');

Putting them all together:

% All the menu options
menu_items = {'Bisection', 'False Position', 'Fixed-Point iteration', 'Newton', 'Secant', 'Modified Secant', 'MATLAB fzero', 'Muller', 'Bairstow', 'MATLAB roots', 'Exit'};
% display the menu
choice = menu('Choose an option', menu_items);

% invoke the respective algorithm based on the choice
switch choice
case 1
bisection()
case 2
false_position()
case 3
fixed_point_iteration()
case 4
newton()
case 5
secant()
case 6
modified_secant()
case 7
MATLAB_fzero()
case 8
muller()
case 9
bairstow()
case 10
MATLAB_roots()
otherwise
exit
end

%% bisection: implementation of Bisection algorithm
function bisection()
% ask the user for required inputs using input method

% then your implementation of the algorithm here

%% false_position: implementation of False Position algorithm
function false_position(arg)
% ask the user for required inputs using input method

% then your implementation of the algorithm here

% similarly write other finctions.