Use the Programming Language —- > 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 O for currentLocationX and currentLocationY. Your Function Save Reset MATLAB Documentation 1 function [endLocationx, endLocationY] = UpdateLocation(del taxcoord, del tavcoord) 2 % deltaxcoord: Move object along x axis 3 % deltaYCoord: Move object along y axis 4 % Declare a persistent variable currentlocationx % Declare a persistent variable current Location 5 6 8 9 18 % 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 currentLocationV currentLocationY = deltaYCoord; 12 ES 14 15 16 1/ 18 19 end endLocationx = currentlocationx; endLocationY = currentLocationY; Code to call your function C Reset endLocationY] endLocationV] UpdateLocation (0, = UpdateLocation(2, 5) 3) 1 [endLocationx, = 2 [endLocationx, Run Function
Expert Answer
Create a file named UpdateLocation.m and paste given code into it. (Filename must be UpdateLocation.m)
UpdateLocation.m
function[endLocationX,endLocationY] = UpdateLocation(deltaXCoord, deltaYCoord)
global x;
global y;
if(isnan(x))
x = 0;
end
if(isnan(y))
y = 0;
end
x = x + deltaXCoord;
y = y + deltaYCoord;
endLocationX = x;
endLocationY = y;
end
driver.m Create a file driver.m and paste given code into it! You just need to run driver.m only to get the output.
clear all;
global x = NaN;
global y = NaN;
[endLocationX,endLocationY] = UpdateLocation(0,5)
[endLocationX,endLocationY] = UpdateLocation(2,3)
[endLocationX,endLocationY] = UpdateLocation(-7,1)
Sample Output:
endLocationX = 0 endLocationY = 5 endLocationX = 2 endLocationY = 8 endLocationX = -5 endLocationY = 9