Question
I only need help on doing number 4
LON-CAPA COurse ConSSOE-First ear-SprirASSGT9 (1)pd softEdge elackboard Conter 4d Metro Boomin No mpstate/Downloads/ASSGT9%20(1) pdf Users/april/AppData/Local/Pac Your task: Design, code, and debug a MATLAB script that: (1) Asks the user (use a nenu) what kind of function (m-file or anonymous function) will be used. If an m-file, have the user enter the filename; if an anonymous function, have the user enter the function (hint: use MATLABs eval function in your script to appropriately assign these tasks to desired variables) C) Calls a user-defined function (ie, m-file) that requests the user to enter the domain of interest (i.e., the start and end point of the plot), and produces an annotated plot (ie, a plot with appropriate labels) of the mathematical function in the desired domain. Use MATLABsfd a command G) Displays a ne au asking the user what type of analysis they wish to perform; choices are finding the mathematical functions zeros, minima, maxima, or area under the curve Uses a s wit ch case structure (with the nenu response as its expression) to: (a) Call a user-defined function that finds as many zeros as the user desires and displays (b) Call a user-defined function that finds as many minima as the user desires and (4) the location of each zero to the screen. Use MATLABsfer o command displays the location of each minimum to the screen. Use MATLABs f Ii nbnd command (c) Call a user-defined function that finds as many maxima as the user desires and displays the location of each maximum to the screen. Use MATLABs f imnbnd command. Note that you need to use the negated function for this step, make sure that you ask for this function in step 1 d) Within the main script, find the area under the curve between two points entered by the user. Give the user the option of using either the trapz or quad methods (5) Allow the user to select as many functional analyses as they desire (i.e., return to step 3) (6) Allow the user to run the program as many times as they desire (i.e, return to step 1)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution to question 4 along with required details in comments are given as follows:

----------------------- MAIN SCRIPT FILE

clear all
clc
f=@(x) sin(x); % a sample function

want_to_continue=1;

while (want_to_continue)
% Menu used for switch satements
fprintf('\n\nPlease enter one among the following choices to select the operation to be performed:');
fprintf('\n1. Find muliple zeros');
fprintf('\n2. Find multiple maxima');
fprintf('\n3. Find multiple minima');
fprintf('\n4. Find Area under curve');
fprintf('\n5. Want to exit?');
  
num=input('\nEnter the choice:');
  
switch(num)
case 1 % find N zeros
N=input('\nFind the number of zeros to be found:');
roots=find_multiple_zeros(f,N); % call the function
g=sprintf('%3.5f ',roots); % display the statement
fprintf('The Roots of the function are obtained obtained at :%s\n',g);
case 2 % find N minimas
N=input('\nFind the number of minima(s) to be found:');
roots=find_multiple_minima(f,N); % call the function
g=sprintf('%3.5f ',roots); % display the results
fprintf('The minima of the function are obtained at :%s\n',g);
case 3 % find N maximas
N=input('\nFind the number of maxima(s) to be found:');
roots=find_multiple_maxima(f,N); % call the function
g=sprintf('%3.5f ',roots); % display the results
fprintf('The maxima of the function are obtained at :%s\n',g);
case 4
N1=input('\n Enter the first limit:'); % find the lower limit of the curve
N2=input('\n Enter the second limit:'); % find the upper limit of the curve
M=input('\n Enter 1 for trapz method and 2 for quad method:'); % select the method
area=find_aoc(f,N1,N2,M); % call the function
fprintf('The area of the function between %d and % d is %3.5f sq. units.\n',N1,N2,area);
case 5
want_to_continue=0;
otherwise
fprintf('Wrong Choice entered!')
end
end

-------------------------- FUNCTION 1: FIND MULTIPLE ZEROS

function roots=find_multiple_zeros(f,N)
% find N zeros of the function
starting_points=linspace(0,N*pi,100); % divide the interval betweeen 0 and N*pi into 100 pieces
for i=1:1:100
% Look for the zeros in the function's current interval.
x(i)=fzero(f, starting_points(i));
end
x_unique=x(diff(x)>1e-12); % identify unique zeros

roots=x_unique; % return unique zeros

end

------------------------ FUNCTION 2: FIND MULTIPLE MINIMAS

function roots=find_multiple_minima(f,N)
starting_points=linspace(0,2*(N+1)*pi,(N+1)); %divide the interval betweeen 0 and 2*(N+1)*pi into (N+1) pieces
for i=1:1:N
% Look for the minima in the function's suninterval.
x(i)=fminbnd(f,starting_points(i),starting_points(i+1));
end
x_unique=x;

roots=x_unique; % return the minimas

end

-------------------------- FUNCTION 3: FIND MULTIPLE MAXIMA

function roots=find_multiple_maxima(f,N)
starting_points=linspace(0,10*(N+1)*pi,10*(N+1));
h=@(x)-f(x); % negate the function to convert to maxima problem
for i=1:1:N
% Look for the zeros in the function's current subintervals
x(i)=fminbnd(h,starting_points(i),starting_points(i+1));
end
x_unique=x;

roots=x_unique(1:N); % return the maximas

end

----------------------------------- FUNCTION 4: FIND AREA UNDER CURVE

function area=find_aoc(f,N1,N2,M)
x=linspace(N1,N2,1000)';
h=@(x) abs(f(x));
% remove the sign from the function so that all y values are positive. This is to
% ensure that area obatined is always positive

if M==1 % if method selected is one, use trapezoidal method
area = trapz(x,h(x));
  
elseif M==2 % if method selected is two, use quad method
area= quad(h,N1,N2);
else
area=0; % othewise, return 0
end
end

--------------------------- SCREENSHOTS OF CODES

main script.m X find multiple zeros.m X find multiple mínima.m find-multiple maxima.m X find-aoc.m X + lear al1 clc f-C(x) si30 31 32- N=input (\nFind the number of maxima (s) to be found :); roots-find multiple_maxima (f,N) g sprintf ( , %3 . 5f ,main-script.m find-multiple zeros.m × find-multiple minima.m X find multiple maxima.m X find-a oc.m + function roots-tind mulmain script.m X find_multiple_zeros.m Xfind_multiple_minima.m find multiple_ maxima.m find_aoc.m+ function roots=find multiplmain script.m × find-multiple-zeros.m X find multiple minima.m 1 甲function roots-find multiple maxima (f,N) 2starting point 3main-script.m ×| find-multiple zeros.m XI find-multiple minima.m find-multiple-maxima.m 지 find-aoc.m X function area-find aoc

-------------------------------------- SAMPLE OUTPUT

Command Window Please enter one among the following choices to select the operation to be performed: 1. Find muliple zeros 2.

Command Window 2. Find multiple maxima 3. Find multiple minima 4. Find Area under curve 5. Want to exit? Enter the choice:3 F2. Find multiple maxima 3. Find multiple minima 4. Find Area under curve 5. Want to exit? Enter the choice:4 Enter the first

Add a comment
Know the answer?
Add Answer to:
I only need help on doing number 4 LON-CAPA COurse ConSSOE-First ear-SprirASSGT9 (1)pd softEdge elackboard Conter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • Matlab help *****CONSTANT***** NUM_MONTHS = 12; FIRST_YEAR = 2013; LAST_YEAR = 2016; %*****...

    matlab help *****CONSTANT***** NUM_MONTHS = 12; FIRST_YEAR = 2013; LAST_YEAR = 2016; %*****INPUT***** months = 1:NUM_MONTHS; years = FIRST_YEAR:LAST_YEAR; % read data file lake_powell = load( 'lake_powell.txt' ); % OR load lake_powell.txt % print with title and year column headings. fprintf(' Lake Powell Water Levels (in feet)\n') fprintf('%8d ', years) fprintf('\n') % print contents of lake_powell for month = 1:NUM_MONTHS fprintf('%9.2f', lake_powell(month,:)) fprintf('\n') end % PART B ========================================================== fprintf('\nPART B: Determine average elevation of water level for each year and...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT