Question

1. Write a function in matlab platform for each part as described. a. Obtains the multiplication of elements of n-sized...

1. Write a function in matlab platform for each part as described.
a. Obtains the multiplication of elements of n-sized vector, where n is a positive integer. Name of this function must be “all2”.
b. Obtains the sum of elements of nxn-sized matrix, where n is a positive integer. Name of this function must be “will3”.
c. Obtains the sum of prime factors of an integer. Name of this function must be “be0”.
Example (1): Prime factors of 84 are [2 3 7].
Hint (1): primes(n) returns the vector of all prime numbers until n.
Hint (2): mod(t,k) returns the remaining number where t is divided by k.
d. Obtains the sum of multipliers of an integer. Name of this function must be “well6”.
Example (2): Multipliers of 84 are [2 2 3 7].
Hint (3): You may benefit from the solution in part c.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. a

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y = all2(x)
y=1;
for i=1:length(x)
    y = y*x(i);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Command Window >> аїї2( [1 2 3 4]) ans 24 >> аїї2(4) ans 4

1. b

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y = will3(x)
y=0;

[row_x col_x] = size(x);
if row_x ~= col_x
    error('input to the function will3 must be a nxn matrix where n is a positive integer.');
else
    for i=1:row_x
        for j=1:col_x
            y = y + x(i,j);
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Command Window will3([1 2 3;4 5 6]) Error using wills (line 6) will3([1 2 3;4 5 6;7 8 9]) ans 45 willa(t1 2;3 4]) ans 10

1. c

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y = be0(x)

array_primes = primes(x);

y = 0;

for i=1:length(array_primes)
    if mod(x,array_primes(i))== 0
        y = y + array_primes(i);
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Command Window beo (84) ans ї2 >beo(10) ans - 7 be0 (11) ans її

1. d

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y = well6(x)

array_primes = primes(x);

y = 0;
quotient = x;

for i=1:length(array_primes)
    if mod(x,array_primes(i))== 0
        y = y + array_primes(i);
        quotient = quotient/array_primes(i);
    end
end

% If the final quotient obtained from previous for loop is >1 it is also
% a multiplier and should be added to the sum.

if quotient > 1
    y = y + quotient;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Command Window well6(84) ans 14 well6(10) ans 7 well6(11) ans =

Add a comment
Know the answer?
Add Answer to:
1. Write a function in matlab platform for each part as described. a. Obtains the multiplication of elements of n-sized...
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
  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • Part 1: Write a C++ function that inputs a positive integer and determines whether it is...

    Part 1: Write a C++ function that inputs a positive integer and determines whether it is prime using trial division. Part 2: Use the function from part 1 to make a C++ program that finds as many primes of the form n2 + 1, where n is a positive integer, as you can.

  • Prime numbers are the building blocks of all integers greater than 1. Any integer n >...

    Prime numbers are the building blocks of all integers greater than 1. Any integer n > 1 can be written as a unique product of primes i.e. n = p1 × p2 × ... × pm. Here, pi are primes such that p1 ≤ p2 ≤ ... ≤ pm. Take, for example, the number 18. It can be written as 18 = 2 × 3 × 3. 1. Write a Python function prime divisors(n) which accept an integer n and...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • How to solve it using Python? 5. Write a function called no_squares which takes an input...

    How to solve it using Python? 5. Write a function called no_squares which takes an input parameter N, a positive integer, and returns: 1 if N is not divisible by a square and has an even number of prime factors -1 if N is not divisible by a square and has an odd number of prime factors 0 if N is divisible by a square For example, no-squares (10) returns 1 (since 10 = 2x5), no-squares (30) returns-1 (since 30...

  • This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an...

    This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an integer number n, and returns a matrix that is nxn, where the value of each number is the distance from the upper-left to bottom-right diagonal. (Use while loops if you can!) Numbers below the diagonal should be negative, and numbers above the diagonal should be positive. For example, if your input was 5, your output would be: 0 1 2 3 4 -1 0...

  • *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...

    *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby- n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note...

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