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
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started