Question: Write a MATLAB function, called mysum that inputs the integer n and outputs the sum of the first n positive integers. If a negative n

Write a MATLAB function, called mysum that inputs the integer n and outputs the sum of the first n positive integers. If a negative n is input, make sure to output the value -1 and print the following statement to the user: cannot input a negative number. Use a for loop to compute the sum - dont use MATLABs built in summation function. Your function should have a header that looks like:

function y=mysum(n)

where n is the number you stop the sum at and y is the returned sum. Hint: You can check your code in MATLAB (NOT MATLAB GRADER) by typing the following into the command line: myresult = mysum(3) and check that myresult is equivalent to 1+2+3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!