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.

Solutions

Expert Solution

Program:

clc;
function digits_function(n) % function definition

num = n; % store the value n in num

number_digits = 0; % Assign count to 0
sum_digits = 0; % Assign sum_digits to 0
mul_digits = 1; % Assign mul_digits to 1

while (n > 0) % Loop to check n>0
sum_digits += mod(n,10); % Calculate sum of digits
mul_digits *= mod(n,10); % Calculate mutiplication of digits
n = fix(n / 10); % perform integer division
number_digits++; % increment countnumbe_digits by 1
end


fprintf("Number of digits in %d is %d\n",num,number_digits);
% print the number and number of digits

fprintf("Sum of digits in %d is %d\n",num,sum_digits)
% print the sum of digits

fprintf("Multiplcation of digits in %d is %d\n",num,mul_digits)
% print the multiplication of digits

fprintf("\n"); % print new line
end

Output:


Related Solutions

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...
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,...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT