Question

Write a function called mysum that sums the first n positive numbers. Give the result x1...

Write a function called mysum that sums the first n positive numbers. Give the result x1 for n=20,and the result x2 for n=100.

Matlab code

Homework Answers

Answer #1

Always save function file using function name

Here given code is save by mysum.m

code of function

function sum = mysum(n)
   
    if(n>0)
        sum=0;
        for i=1:n
            sum=sum+i;
        end
       
    else
        disp('not defined')
    end
       
           
end

Result

Know the answer?
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
Similar Questions
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Question 4 MATLAB a) Write a function file called q4.m containing the following nonlinear equations. ?(?1?2...
Question 4 MATLAB a) Write a function file called q4.m containing the following nonlinear equations. ?(?1?2 ) = ?1 2 + ?2 2 − 26 ?(?1?2 ) = 3?1 2 + 25?2 2 − 100 (1 Mark) b) Use MATLAB’s FSOLVE operator to solve these equations with x1 = 2 and x2 = 2 as your starting point. How many iterations did MATLAB use to solve them? (1 Mark)
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
Consider the sequence(an)n≥1that starts1,3,5,7,9,...(i.e, the odd numbers in order). (a) Give a recursive definition and closed...
Consider the sequence(an)n≥1that starts1,3,5,7,9,...(i.e, the odd numbers in order). (a) Give a recursive definition and closed formula for the sequence. (b) Write out the sequence(bn)n≥2 of partial sums of (an). Write down the recursive definition for (bn) and guess at the closed formula. (b) How did you get the partial sums?
5.34 Write a function statement() that takes as input a list of floating-point numbers, with positive...
5.34 Write a function statement() that takes as input a list of floating-point numbers, with positive numbers representing deposits to and negative numbers representing withdrawals from a bank account. Your function should return a list of two floating-point numbers; the first will be the sum of the deposits, and the second (a negative number) will be the sum of the withdrawals. >>> statement([30.95, -15.67, 45.56, -55.00, 43.78]) [120.29, -70.67]
USING MATLAB Write a program called QuadProg that determines the roots of a quadratic equation. Use...
USING MATLAB Write a program called QuadProg that determines the roots of a quadratic equation. Use functions to implement each action. Zip you directory of *.m files into a single file and turn this in on BBLearn under Class Activities. QuadProg write a function called getInputs to get a single input value. write a function called findDiscriminant write a function called findDenominator write a function called findRoot which will be used for both roots write a function called displayResults displayResults...
Write a C# source code. Write a function called CircArea() that finds the area of a...
Write a C# source code. Write a function called CircArea() that finds the area of a circle. It should take an argument of type double and return an argument of the same type. Write a Main() method that gets a radius value from the user, calls CircArea(), and displays the result.
In Matlab, write a function that takes a relatively small (less than 100 billion) positive integer...
In Matlab, write a function that takes a relatively small (less than 100 billion) positive integer input and outputs the prime factorization of that integer using as few built-in functions as possible. Explain the processes with comments. NOTE: Do not use the functions factor(n), primes(n), or isprime(n). You should be able to write out what these functions do yourself if you want to be able to do something similar.