Question

In Matlab: Given the script: x = 1; x = fun(x-1); y = x-1 and the...

In Matlab:

Given the script:

x = 1;

x = fun(x-1);

y = x-1

and the function:

function y = fun(x)

x = x-1;

y = x-1;

end

What is the output

a) -2

b) -4

c) -3

d) -1

Homework Answers

Answer #1

c) -3

Explanation:
-------------
function y = fun(x)
    x = x-1;
    y = x-1;
end

x = 1;
x = fun(x-1);   // calls fun with 1-1 = 0
    x = x-1; changes x to 0-1 = -1
    y = x-1; so, y = -1-1 = -2
    so, function returns -1
y = x-1, y = -2-1 = -3
so, value of y is -3.
since there is no semi-colon after y=x-1, value of y is printed.
so, -3 is printed


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
Using the Script: function EulerMethod1(n) X = 0 : 1/n : 1 ; Y = zeros(...
Using the Script: function EulerMethod1(n) X = 0 : 1/n : 1 ; Y = zeros( 1, n + 1 ) ; Y(1) = 1 ; for k = 1 : n m = Y(k) ; Y(k + 1) = Y(k) + m*( X(k + 1) - X(k) ) ; end clf plot( X, Y ) Create a new script, which defines the function EulerMethod2. The purpose of EulerMethod2 is to use Euler's Method to approximate the solution to the...
Write a matlab script file to solve the differential equation dy/dt = 1 − y^3 with...
Write a matlab script file to solve the differential equation dy/dt = 1 − y^3 with initial condition y(0)=0, from t=0 to t=10. matlab question
Using MATLAB Write a user-defined MATLAB function for the following math function: y(x)= (-0.2x^3 + 7x^2)e^-0.3x...
Using MATLAB Write a user-defined MATLAB function for the following math function: y(x)= (-0.2x^3 + 7x^2)e^-0.3x The input to the function is x and the output is y. Write the function such that x can be a vector (use element-by-element operations). (a) Use the function to calculate y(-1.5) and y(5). (b) Use the function to make a plot of the function y(x) for -2 ≤ x ≤ 6.
I need code for a problem in Matlab Create a single Matlab script that accomplishes the...
I need code for a problem in Matlab Create a single Matlab script that accomplishes the following: Defines a blank tic tac toe board, a 3 x 3 matrix, called board with blank spots represented by zeros. Asks player 1 for a row and column, then puts 1 in a given location. Asks player 2 for a row and column, then puts 2 in a given location
Using Matlab to solve the problem below Given X=[-2 -1 0 1 2] Y=[1.5 3.2 4.5...
Using Matlab to solve the problem below Given X=[-2 -1 0 1 2] Y=[1.5 3.2 4.5 3.4 2] a). Plot a scatter plot of the data b). Determine the coefficients of the polynomial ?0 + ?1? + ?2?2 which best fits the data c). Plot this function on the same plot as in part ‘a’. USE MATLAB CODE ONLY! USE MATLAB CODE ONLY! THANK YOU
Let f(x, y) = c/x, 0 < y < x < 1 be the joint density...
Let f(x, y) = c/x, 0 < y < x < 1 be the joint density function of X and Y . a) What is the value of c? a) 1   b) 2 c) 1/2 d) 2/3 e) 3/2 b)what is the marginal probability density function of X? a) x/2 b)1 c)1/x d)x e)2x c)what is the marginal probability density function of Y ? a) ln y   b)−ln y c)1 d)y e)y2 d)what is E[X]? a)1 b)2 c)4 d)1/2 e)1/4
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9,...
Write Matlab script to plot the spectrum (-? to ? radians) of DTMF signal for keys...
Write Matlab script to plot the spectrum (-? to ? radians) of DTMF signal for keys 1, A and B. Choose an appropriate sampling frequency. The DTMF frequencies are indicated in table 1.     Critically analyze the design specification.                                           Row/column 1209HZ 1336Hz 1477HZ 1633HZ 697 1 2 3 A 770 4 5 6 B 852 7 8 9 C 941 * 0 # D     Write Matlab script to plot the spectrum (-? to ? radians) of DTMF signal for...
12. Let f(x, y) = c/x, 0 < y < x < 1 be the joint...
12. Let f(x, y) = c/x, 0 < y < x < 1 be the joint density function of X and Y . What is the value of c? a) 1; b) 2; c) 1/2; d) 2/3; e) 3/2. 13. In Problem 12, what is the marginal probability density function of X? a) x/2; b)1; c)1/x; d)x; e)2x. 14. In Problem 12, what is the marginal probability density function of Y ? a) ln y; b)−ln y; c)1; d)y; e)y2....
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits by following the instructions noted below. The input signal and impulse response generation should be done in the function. The test bench script should be used only to call the function and for signal plotting purposes. No plotting should be done inside the function itself. Name your function L2_C Instructions: Input voltage ,x(t), can be AC or DC. Consider a variable ‘w1’ which can...