Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

(Note: It is a ONE QUESTION with some SIX PARTS, so even if Chegg interprets this question as multiple(6) questions, and reduces my remaining posts,

(Note: It is a ONE QUESTION with some SIX PARTS, so even if Chegg interprets this question as multiple(6) questions, and reduces my remaining posts, that is okay to me.)

(Though I tried many times but could not solve this MATLAB problem on my own (maybe because programming language is entirely new to me). So I have no choice except asking this on Chegg. Any help will be highly appreciated. Thank you.)

HOW TO MODIFY THESE 6 MATLAB FUNCTIONS(calculate.m,doAdd.m, doDiv.m, doMult.m, doPow.m, doSub.m as shown in pictures below) SO AS TO MAKE THEM WORK AS A WHOLE AS DESCRIBED BELOW?image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

PROBLEM DESCRIPTION: This project involves the evaluation of mathematical expressions. Function [ answer ] = calculate ( expr ) Function [ pos sum ] = doAdd ( expr, pos ) Function [ pos quot ] = doDiv ( expr, pos ) Function [ pos prod ] = doMult ( expr, pos ) Function [ pos expo ] = doPow ( expr, pos ) Function [ pos diff ] = doSub ( expr, pos )

Any series of expressions begins with one of the operator keywords add, div, mult, pow, and sub and ends with the keyword stop . If there is more than one expression, the results are packaged up and returned in a vector. There may be nested expressions for which you may see multiple instances of an operator keyword / stop combination. See below for examples. For addition, all the values up to the associated stop are added. For subtraction, all the values are cumulatively subtracted from zero (0). For multiplication, all the values are multiplied. For division, all the values are cumulatively divided into one (1). For exponentiation, all the values are exponentiated from right to left, just as is customarily done in mathematics.

The emphasis of this project is on getting multiple functions going, with them invoking each other, sending over data to be processed and retrieving results.

We start with a mathematical expression with a variety of operations that may be nested either explicitly with parentheses, eg 2 * (3 + 4) * 5 or implicitly, eg 2 * 3 + 4 * 5. These expressions need to be converted into a form that can be processed by your program. You do this manually, using words such as 'mult' and 'add' at the beginning of each expression, as well as at the beginning of each nested expression. Then, rather than using a close parenthesis ")", we use the word 'stop' to indicate the end of an expression. So the former becomes 'mult' '2' 'add' '3' '4' 'stop' '5' 'stop' and the latter becomes 'add' 'mult' '2' '3' 'stop' 'mult' '4' '5' 'stop'.

The calculate function starts with the first word, such as 'add', and invokes the appropriate function to handle it, in this case doAdd. doAdd works through the list of words, adding up the numbers if finds and invoking other functions such as 'mult' to handle subexpressions, after which it continues adding up any additional numbers and subexpressions. The core of each of your functions is a switch statement to handle each discovery of a new subexpression, and a simple calculation to handle each value it finds. So the doAdd function will have this switch statement and it will have a simple sum = sum + nextNumber. This little package of a switch and a summing command is embedded inside a while loop that keeps repeating the process and add cycle until a 'stop' directive is reached. At that point the sum is returned to whatever function invoked the doAdd. Each of the other functions is very much like doAdd. The main difference is how the values are handled, such as subtracted, multiplied, etc.

That's pretty much all you have to do!

Editor - C\Users\DELL 13\Desktop\csce155nl project021calculate.m Find Files Compare Print Comment Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Run and FILE Function Filename: calculate .m % Purpose: Project 02 Calculator main function Takes the algebraic expressions and sends it to the appropriate operation handler. Packages up the results and returns it as a vector % Input: One column cell array of char arrays representing operations 'add','sub''mult', "div'and 'pow' the 'stop' directive, and numbers for the calculation 10 % Output: Vector of numeric results of possibly series of calculations 12 13 % Example: The expressions below represent 23 27 +3 242+4 (2+3) 5) (1) 1 21374 -4-2 23 (1/ 2)4+5+2+3))3 15 16 17 18 19 20 21 % >> t {'add ' , '23', '27', 'pow ', '3', '2','2', 'stop', '4', 'stop', - malt',add '2',3','stop',add',4','5',stop''stop'.. div',2','3',"4, stop' sub',4',2",'stop' mult','2",'3,'div', '2", "stop, add','4','5','2", .. 23 malt'1', 3', 'stop','stop','3', 'stop' % >> calculate (t) 25 26 27 2B 29end 30 an5-135.0000 45.0000 0.0417-6.0000 126.0000 CALCULATE (top level) function answer-calculatel cxpr Activate Windows Go to PC settings to activate Windows Ln 21 Col 35 11:33 PM 2/15/2019 Editor-C:\Users\DELLJ3\Desktop\csce1 55project02\doAdd.m EDITOR Find Files Compare Print Ld Comment ame Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Advance Run and FILE Function Filename: doAdd.m Purpo5e: Project 02 Calculator add function Takes the expression to be summed and sends items to the appropriate operation handler. Adds them al1 up Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 % Example: The expression below represent 23 27 +3 242+4 t { 'add', '23', '27', 'por','3', '2', '2','stop','4', 'stop ' } _ >> calculate (t) % ans-135.0000 23 ADDITION 25 26- function [pos sumdokdd cxpr, pos) sum = 0; Activate Windows Go to PC settings to activate Windows 2B doAdd Ln 27 Col4 11:35 PM 2/15/2019 Editor - CAUsers\,DELL_13\Desktopcsce155nkproject02\doDiv.m EDITOR Find Files GoTo Comment New Open Save Breakpoints Run Run and Advance Advance Run and Print Find FILE Function Filename: doDiv.m Purpo5e: Project 02 Calculator divide function Takes the expression to be divided and sends items to the appropriate operation handler. Divides them all up starting with 1 as the divisor % Input: One column cell array of char arrays representing operations 'add','sub''mult' div',and 'pow,the "stop' directive, and numbers for the calculation 10 12 13 Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression below represents (1) 121374 >> t { 'dir','2','3', '4','stop' } " >> calculate (t) % ans-0.0417 23 DIVIDE 25 26- 27end 2B function [pos quot]doDivexpr, pos) quot 1; Activate Windows Go to PC settings to activate Windows Click and drag to move doDiv.m or its tab... doDiv Ln 15 Col 2 11:35 PM 2/15/2019 Editor-CAUsers\DELL-BlDesktopicsce1 55project02\doMult.m Find Files Run Section Comment Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Advance Run and Print FILE EDIT calculatem XI doAdd.m IdoDiv.m Function Filename: doMult.m 8 Purpose: Project 02 Calculator multiplication function Takes the expression to be multiplied and sends items to the appropriate operation handler. Multiplies them all up Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression bclow represents (1) 1 21374 % >> t = {'mult ' , 'add', '2','3', 'stop', 'add', '4', '5', 'stop', 'stop' } % >> calculate (t) an5-45.0000 23 MULTIPLICATION function [pos prod] = doMult ( expr, pos ) prod1: 26- 27 end Activate Windows Go to PC settings to activate Windows doMult Ln 15 Col 11:35 PM 2/15/2019 Editor-C:\Users\DELLJ3\Desktop\csce1 55project02\doPow.m Find Files Compare Print G oCommentBreakpoints Run Run andAdvance Run and New Open Save Find Advance FILE NAVIGATE Function Filename: doPow.m 8 Purpose: Project 02 Calculator power function Takes the expression to be exponentiated and sends items to the appropriate operation handler. Exponentiates them all up in the normal right to left order % Input: One column cell array of char arrays representing operations 'add','sub''mult' div',and 'pow,the "stop' directive, and numbers for the calculation 10 12 13 Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 8 Example: The expression below represents 3 2 2 % >> calculate (t) an5-81.0000 23 25 EXPONENTIATE 26 function [pos expo] = doPow expr, pos ) expo1: 28 29 Activate Windows Go to PC settings to activate Windows end Click and drag to move doPow.m or its tab... doPow Ln 15 Col 2 11:35 PM 2/15/2019 Editor - C\Users\DELL 13\Desktop\csce155nl project021doSub.m EDITOR Find Files GoTo Comment New Open Save Breakpoints Run Run and Advance Advance Run and Print Find FILE NAVIGATE doSub.mx+ Function Filename: doSub.m 8 Purpose: Project 02 Calculator subtraction function Takes the expression to be subtracted and sends items to the appropriate operation handler. Subtracts them all from 0 Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression below represents -4-2 >> calculate (t) ans--6 .0000 % 23 SUBTRACTION 25 26- 27end 2B 29 function [pos diff doSub expr, pos) diff = 0; Activate Windows Go to PC settings to activate Windows doSub Ln 15 Col 2 11:35 PM 2/15/2019 Editor - C\Users\DELL 13\Desktop\csce155nl project021calculate.m Find Files Compare Print Comment Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Run and FILE Function Filename: calculate .m % Purpose: Project 02 Calculator main function Takes the algebraic expressions and sends it to the appropriate operation handler. Packages up the results and returns it as a vector % Input: One column cell array of char arrays representing operations 'add','sub''mult', "div'and 'pow' the 'stop' directive, and numbers for the calculation 10 % Output: Vector of numeric results of possibly series of calculations 12 13 % Example: The expressions below represent 23 27 +3 242+4 (2+3) 5) (1) 1 21374 -4-2 23 (1/ 2)4+5+2+3))3 15 16 17 18 19 20 21 % >> t {'add ' , '23', '27', 'pow ', '3', '2','2', 'stop', '4', 'stop', - malt',add '2',3','stop',add',4','5',stop''stop'.. div',2','3',"4, stop' sub',4',2",'stop' mult','2",'3,'div', '2", "stop, add','4','5','2", .. 23 malt'1', 3', 'stop','stop','3', 'stop' % >> calculate (t) 25 26 27 2B 29end 30 an5-135.0000 45.0000 0.0417-6.0000 126.0000 CALCULATE (top level) function answer-calculatel cxpr Activate Windows Go to PC settings to activate Windows Ln 21 Col 35 11:33 PM 2/15/2019 Editor-C:\Users\DELLJ3\Desktop\csce1 55project02\doAdd.m EDITOR Find Files Compare Print Ld Comment ame Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Advance Run and FILE Function Filename: doAdd.m Purpo5e: Project 02 Calculator add function Takes the expression to be summed and sends items to the appropriate operation handler. Adds them al1 up Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 % Example: The expression below represent 23 27 +3 242+4 t { 'add', '23', '27', 'por','3', '2', '2','stop','4', 'stop ' } _ >> calculate (t) % ans-135.0000 23 ADDITION 25 26- function [pos sumdokdd cxpr, pos) sum = 0; Activate Windows Go to PC settings to activate Windows 2B doAdd Ln 27 Col4 11:35 PM 2/15/2019 Editor - CAUsers\,DELL_13\Desktopcsce155nkproject02\doDiv.m EDITOR Find Files GoTo Comment New Open Save Breakpoints Run Run and Advance Advance Run and Print Find FILE Function Filename: doDiv.m Purpo5e: Project 02 Calculator divide function Takes the expression to be divided and sends items to the appropriate operation handler. Divides them all up starting with 1 as the divisor % Input: One column cell array of char arrays representing operations 'add','sub''mult' div',and 'pow,the "stop' directive, and numbers for the calculation 10 12 13 Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression below represents (1) 121374 >> t { 'dir','2','3', '4','stop' } " >> calculate (t) % ans-0.0417 23 DIVIDE 25 26- 27end 2B function [pos quot]doDivexpr, pos) quot 1; Activate Windows Go to PC settings to activate Windows Click and drag to move doDiv.m or its tab... doDiv Ln 15 Col 2 11:35 PM 2/15/2019 Editor-CAUsers\DELL-BlDesktopicsce1 55project02\doMult.m Find Files Run Section Comment Go To Find NAVIGATE New Open Save Breakpoints Run Run and Advance Advance Run and Print FILE EDIT calculatem XI doAdd.m IdoDiv.m Function Filename: doMult.m 8 Purpose: Project 02 Calculator multiplication function Takes the expression to be multiplied and sends items to the appropriate operation handler. Multiplies them all up Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression bclow represents (1) 1 21374 % >> t = {'mult ' , 'add', '2','3', 'stop', 'add', '4', '5', 'stop', 'stop' } % >> calculate (t) an5-45.0000 23 MULTIPLICATION function [pos prod] = doMult ( expr, pos ) prod1: 26- 27 end Activate Windows Go to PC settings to activate Windows doMult Ln 15 Col 11:35 PM 2/15/2019 Editor-C:\Users\DELLJ3\Desktop\csce1 55project02\doPow.m Find Files Compare Print G oCommentBreakpoints Run Run andAdvance Run and New Open Save Find Advance FILE NAVIGATE Function Filename: doPow.m 8 Purpose: Project 02 Calculator power function Takes the expression to be exponentiated and sends items to the appropriate operation handler. Exponentiates them all up in the normal right to left order % Input: One column cell array of char arrays representing operations 'add','sub''mult' div',and 'pow,the "stop' directive, and numbers for the calculation 10 12 13 Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 8 Example: The expression below represents 3 2 2 % >> calculate (t) an5-81.0000 23 25 EXPONENTIATE 26 function [pos expo] = doPow expr, pos ) expo1: 28 29 Activate Windows Go to PC settings to activate Windows end Click and drag to move doPow.m or its tab... doPow Ln 15 Col 2 11:35 PM 2/15/2019 Editor - C\Users\DELL 13\Desktop\csce155nl project021doSub.m EDITOR Find Files GoTo Comment New Open Save Breakpoints Run Run and Advance Advance Run and Print Find FILE NAVIGATE doSub.mx+ Function Filename: doSub.m 8 Purpose: Project 02 Calculator subtraction function Takes the expression to be subtracted and sends items to the appropriate operation handler. Subtracts them all from 0 Input: One column cell array of char arrays representing operations 'add' 'sub', 'mult' div'and 'pow', the 'stop' directive, and nunbers for the calculation 10 12 13 % Output: Vector of numeric results of possibly series of calculations 15 16 17 18 19 20 21 Example: The expression below represents -4-2 >> calculate (t) ans--6 .0000 % 23 SUBTRACTION 25 26- 27end 2B 29 function [pos diff doSub expr, pos) diff = 0; Activate Windows Go to PC settings to activate Windows doSub Ln 15 Col 2 11:35 PM 2/15/2019

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions