In MatLab, write a function with persistent variables currentLocationX and…
- User
In MatLab, write a function with persistent variables currentLocationX and currentLocationY that store an object's location. Each call to the function adds deltaXCoord to currentLocationX and adds deltaYCoord to currentLocationY. Assume a starting location of 0 for currentLocationX and currentLocationY.
Use this code:
function [endLocationX, endLocationY] = UpdateLocation(deltaXCoord, deltaYCoord) % deltaXCoord: Move object along x axis % deltaYCoord: Move object along y axis
% Declare a persistent variable currentLocationX % Declare a persistent variable currentLocationY % If currentLocationX is empty, assign currentLocationX with deltaXCoord % Otherwise, add deltaXCoord to currentLocationX currentLocationX = deltaXCoord; % If currentLocationY is empty, assign currentLocationY with deltaYCoord % Otherwise, add deltaYCoord to currentLocationY currentLocationY = deltaYCoord; endLocationX = currentLocationX; endLocationY = currentLocationY;end
- Assistant
Here is the updated code with the correct usage of persistent variables:
function [endLocationX, endLocationY] = UpdateLocation(deltaXCoord, deltaYCoord) % deltaXCoord: Move object along x axis % deltaYCoord: Move object along y axis % Declare a persistent variable currentLocationX persistent currentLocationX % Declare a persistent variable currentLocationY persistent currentLocationY % If currentLocationX is empty, assign currentLocationX with deltaXCoord % Otherwise, add deltaXCoord to currentLocationX if isempty(currentLocationX) currentLocationX = deltaXCoord; else currentLocationX = currentLocationX + deltaXCoord; end % If currentLocationY is empty, assign currentLocationY with deltaYCoord % Otherwise, add deltaYCoord to currentLocationY if isempty(currentLocationY) currentLocationY = deltaYCoord; else currentLocationY = currentLocationY + deltaYCoord; end endLocationX = currentLocationX; endLocationY = currentLocationY; end - User
In MatLab, write a function called Divide which divides two numbers, and keeps count of how many times the function was called asking to divide by zero. The function has two outputs: z, the division of the two numbers, and times, the number of times the function was called with a divide-by-zero request. In the case of a divide-by-zero request, the resultant should be set to not a number (NaN).
Restrictions: The function must make use of a persistent variable.Use this code:
function [z,times] = Divide(x,y)
% Your code goes here %end
- Assistant
Here is the updated code with the implementation of the Divide function using a persistent variable to keep count of divide-by-zero requests:
function [z,times] = Divide(x,y) % Declare a persistent variable count persistent count % Initialize count to 0 if it is empty if isempty(count) count = 0; end % Increment count if y is 0 if y == 0 count = count + 1; end % Check if y is 0 if y == 0 z = NaN; % Set the result to NaN else z = x / y; % Divide x by y end times = count; % Output the value of count as times endIn this code, I have declared a persistent variable
countto keep track of the number of divide-by-zero requests. Thecountvariable is initialized to 0 if it is empty using theisempty()function.Then, in the function body, I increment
countby 1 ifyis equal to 0, indicating a divide-by-zero request.I then check if
yis zero. If it is, I setzto NaN, indicating an invalid division result. Otherwise, I calculate the division and store the result in . - User
In MatLab, complete the recursive function RaiseToPower(). In this problem the variable userBase is raised to the power assigned to the variable raisedValue.
Ex: If userBase is 2 and userExponent is 4, then raisedValue is assigned with 16 (i.e. 2^4).
Note: This example is for practicing recursion; a non-recursive function, or using the built-in function pow(), would be more common.
Use this code:
function resultVal = RaiseToPower(baseVal, exponentVal) % baseVal: Base value % exponentVal: Power userBase is raised to
resultVal = 0; if(exponentVal == 0) resultVal = 1; else % Complete the assignment statement below resultVal = endend
- Assistant
To complete the recursive function
RaiseToPower(), you can use the following code:function resultVal = RaiseToPower(baseVal, exponentVal) % baseVal: Base value % exponentVal: Power userBase is raised to if(exponentVal == 0) resultVal = 1; else % Recursive call to RaiseToPower() with exponentVal-1 resultVal = baseVal * RaiseToPower(baseVal, exponentVal-1); end endIn this code, if the
exponentValis 0, we return 1 because any value raised to the power of 0 equals 1.Otherwise, we recursively call the
RaiseToPower()function withexponentVal-1as the new exponent. In each recursive call, we multiplybaseValwith the result of the previous recursive call.This process continues until
exponentValreaches 0, at which point the recursion stops and the final result is returned.Note that this implementation assumes a valid positive exponent value. Negative or non-integer exponents are not considered in this code.
- User
In MatLab, complete the recursive function to determine if a number is prime. Skeletal code is provided in the PrimeChecker function.
Use this code:
% userNum: Value checked for prime function isPrime = CheckValue(userNum) % Do not modify, calls the recursive PrimeChecker function isPrime = PrimeChecker(userNum, userNum-1); end
function primeResult = PrimeChecker (testVal, divVal) % Complete the recursive function PrimeChecker % Function returns 0 if testVal is not prime and 1 if testVal is prime
% Test case 1: If testVal is 0 or 1, assign primeResult with 0 (not prime) primeResult = 0; % Test case 2: If testVal is only divisible by 1 and itself, % assign primeResult with 1 (is prime) % Hint: use divVal % Test case 3: If testVal can be evenly divided by divVal, % assign primeResult with 0 (not prime) % Hint: use the mod function % Otherwise, assign primeResult with the result of the recursive call % to PrimeChecker with testVal and (divVal - 1)end
- Assistant
To complete the recursive function
PrimeChecker, you can use the following code:function primeResult = PrimeChecker(testVal, divVal) % Complete the recursive function PrimeChecker % Function returns 0 if testVal is not prime and 1 if testVal is prime % Test case 1: If testVal is 0 or 1, assign primeResult with 0 (not prime) if testVal == 0 || testVal == 1 primeResult = 0; % Test case 2: If testVal is only divisible by 1 and itself, % assign primeResult with 1 (is prime) elseif divVal == 1 primeResult = 1; % Test case 3: If testVal can be evenly divided by divVal, % assign primeResult with 0 (not prime) elseif mod(testVal, divVal) == 0 primeResult = 0; % Otherwise, assign primeResult with the result of the recursive call % to PrimeChecker with testVal and (divVal - 1) else primeResult = PrimeChecker(testVal, divVal-1); end end % userNum: Value checked for prime function isPrime = CheckValue(userNum) % Do not modify, calls the recursive PrimeChecker function isPrime = PrimeChecker(userNum, userNum-1); end