Question

Write a MATLAB userdefined function that calculates the product and the ratio of two variables x...

Write a MATLAB userdefined function that calculates the product and the ratio of two variables x and y.

1. if any element in the second input argument is 0, then a warning message must be generated (‘Possible division by 0’)

2. if the user calls the function with one input argument, then the second input argument must be equal to the first one.

3. if the two input argumenta are of different sizes, then a proper error message must be generated with the exception when one or both are scalars.

4. the second output argument (ratio) must not be calculated unless the function is called with two output arguments.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

clc
clear all
[p,r]=fun([3 2 2],[90 0 0])


function [p,r]=fun(x,y)
if(nargin<2)
y=x;
end
if(sum(y==0)>0)
warning('Possible division by 0');
end
[m1,n1]=size(x);
[m2,n2]=size(y);
if(m1~=m2||n1~=n2)
if(~(isscalar(x)||isscalar(y)))
error('Arguments of different sizes');
end
end
p=x.*y;
if(nargout==2)
r=x./y;
end
end

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB userdefined function that calculates the product and the ratio of two variables x...
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
  • Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates...

    Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates the determinant of a 3x3 matrix by using the formula: A22 A23 11 A32 A33 A23 A12 A31 A33 21 A For the function name and arguments use d3-det3by3 (A), where the input argument A is the matrix and the output argument d3 is the value of the determinant. Write the code of det3by3 such that it has a subfunction that calculates the 2x2...

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

  • Write a user-defined MATLAB function that will calculate the area/volume of a shape whose dimensions are...

    Write a user-defined MATLAB function that will calculate the area/volume of a shape whose dimensions are given by the user. This function will be able to calculate the area for 2D objects like a triangle or a rectangle and 3D objects such as a box. The name of the function will be findArea and it will have four input arguments. The first input argument will be the length. The second input argument will be the width. The third input argument...

  • 1. Palindrome Write a program that prompts the user for a positive integer number and output whet...

    Language is C 1. Palindrome Write a program that prompts the user for a positive integer number and output whether the number is a palindrome or not. A palindrome number is a number that is the same when reversed. For example, 12321 is a palindrome; 1234 is not a palindromoe. You will write a function named as palindrome. The function will have two arguments, number and isPalindrome. Both arguments are pass-by-reference. The argument number is a pointer to a variable...

  • Problemi Consider the plecewise function sit) defined as follows: -(+2 when -2sts2 0.5 elsewhere s(t)- Write...

    Problemi Consider the plecewise function sit) defined as follows: -(+2 when -2sts2 0.5 elsewhere s(t)- Write a MATLAB user-defined function called aystfun.n that has one input argument to, and two output arguments: s0 and vsO. Your function must satisfy all the following requirements: 1. Your function must calculate and return the vector so - s(t0) for any supplied numerical vector to. We assume that the function is always called with a vector t0 whose elements are all in [-4,4]. 2....

  • use matlab to solve it 1. Write a function called MyTimeConversion with the following function declaration...

    use matlab to solve it 1. Write a function called MyTimeConversion with the following function declaration line. begin code function [Hours, Minutes, Message] = MyTimeConversion (TotalMinutes) end code The input argument TotalMinutes should be a nonnegative scalar integer representing the total number of minutes in a specified time interval. The output arguments Hours and Minutes should be nonnegative scalars such that Hours*60 + Minutes = TotalMinutes and the value of Minutes must be less than 60. The output argument Message...

  • Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...

    Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same. Write this as a function with input arguments the matrices to be multiplied and output argument the resultant matrix, use "assert" statement(s) to check that the input arguments are valid and print an error message if they are not. Test with A=[1,2,3;4,5,6] and B=[7,8;9,10;11,12] showing results for: (a) A*B, (b) B*A, and (c) A*A.

  • MATLAN Quiz 2 Create a MATLAB function that calculates the average of values in a vector that als...

    MATLAN Quiz 2 Create a MATLAB function that calculates the average of values in a vector that also drops the slowest values. Call this function average_drop(). The function must have a single input (the vector) and a single output (the average without the lowest score). 1. 2. Create a MATLAB program that asks the user to input grades for a student and calculates the final grade and letter grade. The user must enter 5 quiz grades (worth 50% of the...

  • Write a user-defined MATLAB function that gives a random integer number within a range between two...

    Write a user-defined MATLAB function that gives a random integer number within a range between two numbers. For the function name and arguments, use n = randint(a,b) where the two input arguments a and b are the two numbers and the output argument n is the random number. Use the function in the command window for the following: (a) Generate a random number between 1 and 49. (b) Generate a random number between -35 and -2

  • Needs to be in MATLAB. Create a function that accepts two numbers as input arguments and...

    Needs to be in MATLAB. Create a function that accepts two numbers as input arguments and returns one with the larger absolute value. The function must return an error message if the number of arguments is not 2.

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
ADVERTISEMENT