Question

In: Computer Science

Using Matlab Write a function, called digits_function that is able to calculate the number of digits...

Using Matlab

  1. Write a function, called digits_function that is able to calculate the number of digits and the multiplication of the digits. The input of this function is N (entered number) and the outputs are number_digits and sum_digits.

  1. Write a function, called print_Min_function that is able to print the minimum of 3 numbers. The inputs of this function are a, b and c.

  1. Write a function, called prime_function that receives one parameter n, and checks whether the number is prime or not. The input of this function is n and the output is result_prime.

  1. Now after you created the three functions, you have to write a program that can call the above functions according to the user’s choice. Write a menu driven that allows the user to select from the following options

  1. To use the digits function you have to enter 1.
  2. To use the minimum function you have to enter 2.
  3. To use the prime function you have to enter 3.
  4. To Exit the program you have to enter 4.

Once the user select one of the above choices you have to read the value entered by the user and then to call the respective function. If the user entered a number that is not equal to 1 or 2 or 3 or 4, the program should ask the user to re-enter again.

Then, you have to use “input” to enter the values of the variables (inputs of the functions).

For the digits function, you have to enter only positive number. For the minimum function, you have to enter greater than or equal to zero values and for the prime function you have to enter greater than 1 values.

For all of the functions, the program should ask the user to re-enter again if the user entered wrong numbers.

Solutions

Expert Solution

Function Code

=======================================

function [d,p] = digits_function(n)
dd = num2str(n) - '0';
d = length(dd);
p = sum(dd);
end

=======================================

function y = print_Min_function(f,s,th)
a = [f;s;th];
y = min(a);
end

===================================

function y = prime_function(n)
if(isprime(n))
disp('Number is Prime')
fprintf('\n')
y = n;
else
disp('Number is not Prime')
fprintf('\n')
y = n;
end
end

===================================

Script Code

while(1)
disp('To use the digits function you have to enter 1.')
disp('To use the minimum function you have to enter 2.')
disp('To use the prime function you have to enter 3.')
num = input('Enter Your Choice: ');
if(num>4)
continue;
elseif(num==4)
break;
end
fprintf('\n')
if(num==1)
x = input('Enter number: ');
if(x<0)
continue;
end
[number_digits ,sum_digits] = digits_function(x);
fprintf('The number of digits are %.0f\n\n',number_digits)
fprintf('Sum of digits is %.2f\n\n',sum_digits)
elseif(num==2)
a = input('Enter First number: ');
b = input('Enter Second number: ');
c = input('Enter Third number: ');
if(a<0 || b<0 || c<0)
continue;
end
min_number = print_Min_function(a,b,c);
fprintf('Minimum number is %.2f\n\n',min_number)
elseif(num==3)
d = input('Enter number: ');
if(d<2)
continue;
end
result_prime = prime_function(d);
else
end
end

============================================

Results

No output with invalid inputs

Exits when 4 is entered

====================================================


Related Solutions

Using Matlab Write a function, called digits_function that is able to calculate the number of digits...
Using Matlab Write a function, called digits_function that is able to calculate the number of digits and the multiplication of the digits. The input of this function is N (entered number) and the outputs are number_digits and sum_digits.
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one...
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one vector v (either a column or a row) and outputs the same vector v of the same dimensions, but with the values in reverse order (use MATLAB function flip()). In other words, v will be overwritten by its flipped version. In your function, you may only use length() and floor(). You only need one loop. %this code tests the function, myflip, which you will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single...
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single file here in Canvas. To make grading easier, please do the following: • Close your function with "end". • Don't have your function print anything to the console; just return the output. To accomplish this: o Put a semicolon ";" at the end of each statement. o Put a percent "%" before each disp or fprintf statement. The function numdiff should take the following...
Matlab You will write a function to calculate the determinant of a matrix. It should work...
Matlab You will write a function to calculate the determinant of a matrix. It should work for any size matrix. Remember that the determinant can be calculated by multiplying the diagonal elements of an upper right triangular matrix. Your function will take a matrix passed to it and put it in upper right triangular form. You will work down the diagonal beginning at row 1 column 1, then row 2 column 2, etc. Note that the row and column numbers...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data arrays (call them x and f), and the array you would like to interpolate to (call it xstar). The function should have one output: the interpolated array ( call it fstar). The function should be able to interpolate x and f onto xstar using linear interpolation and give the result as fstar. The function may not use any intrinsic functions except length.
Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward...
Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward and backward substitution procedures. Do NOT use the built-in "lu" or "inv" commands in your code. You will need to employ Nested Loops. Thank you! function_____ = myMatrixInversion(_____)
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an...
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an output, A1. The output A1 should be the area under a curve, f(x), for x starting at x_start and ending at x_end. The input r should be a vector (array) having x_start and x_end as its two elements. The input N should be an integer equal to the number of equallength sub-intervals in which the interval from x_start to x_end should be divided. Here,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT