E80 Matlab Assignment 6 Loops, Conditionals, and an ODE For this assignment, you will practice your coding with conditionals and loops, while also testing a numeric solver. Fig. 1 provides a simple robot path that starts at position (1,1) and ends at position (1,10). You must create a function directs the robot to follow this path, and stops to take temperature measurements at each location along the path. Note your simulation will not be real-time or realistic for that matter, but it will force you to think about conditionals. Figure 1: Desired robot path. Red circles indicate desired positions at which temperature measurements are to be taken. First Order Temperature Measurements 1. Create a new function called firstOrderTemp whose inputs are t, T, tau, x, y. In this case t is time, T is temperature, tau is a time constant, and (x,y) is a position. Outputs of the function is Tdot. The first line of your function should look like: function Tdot = firstOrderTemp(t, T, tau, x, y) 2. Add code to your firstOrderTemp function to calculate Tdot based on the following first order equation. 1 1 𝑇̇ (𝑡) + 𝑇(𝑡) = 𝑇𝑓 𝜏 𝜏 In this case, the final temperature Tf is modeled as a Gaussian type function of position 𝑇𝑓 = 𝑑2 − 2 𝑒 𝜎 The equation above is a function of standard deviation sigma = 10 and distance d, i.e. the distance from position (1,1). 3. Solve your function with the ode23 solver. Since your function will require a few inputs, (e.g. tau, x, y), we need to call it in a special way: [t,T] = ode23(@(t,T) firstOrderTemp(t,T,tau, x, y),[t0 tf], T0); Before you call it, you must specify the values of tau, x, y, t0, tf, T0. Set the position x,y to be (1,1). Set the initial and final times (i.e. t0, tf) that you are solving the equation over to be 0 and 20 seconds respectively. Set the initial temperature T0 to be 10 degrees, and tau = 2.0s. 4. Plot the temperature T as a function of time t. Label the plot axis, save the image, and add to your submission sheet. Path Control 5. Create a new function called getNextPosition whose inputs are the current position x, y and the values xMax and yMax which represent the boundaries of a lawnmower path that the robot will follow, (see Fig. 1 above which has xMax = yMax = 10). The output of the function will be a new position x,y. 6. Add conditionals to the getNextPosition function, that update the x,y positon to follow the path shown in Fig. 1. For example if the input to the function is position (x,y) = (1,1), the output of the function should be (x,y) = (2,1). If the input to the function is (10,1), the output should be (10,2). Conditional functions that may be useful include if and elseif. It may also be helpful to check if the value of x or y is even or odd. E.g. the following code updates y if y is even: if mod(y,2) == 0 % even y = y + 1; end 7. Test your getNextPosition function using the following code at the command line or in a script file: X=1; Y=1; for i=1:100 [X(i+1), Y(i+1)]=getNextPosition(X(i),Y(i),10,10); end; plot(X,Y,’r-o’); axis([0 11 0 11]); 8. Save the plot, (which should be identical to Fig. 1), and add it to your submission sheet. Path Control 9. Create a new script file called E79MatlabAssignment6.m. Add the following clear function call and instantiations to the top of your file: clear; T0 = 10; t0 = 0; tf = 20; x=1; y=1; tau=2; xMax = 10; yMax = 10; These functions set the initial temperature for each data point in a 10×10 grid to be 10 degrees. The variables t0 and tf are used to set the initial and final time points over which an ode solver will calculate a temperature measurement transient response. The time constant of the response is tau. 10. Add these additional instantiations to your script file: TempMap = 0*ones(xMax,yMax); Path = []; The variable TempMap is a 2D array that will be used to store temperature values as a function of x,y position. The variable Path will contain the positions of the robot as it moves through the environment (following the path in Fig. 1). It should take on the format [x1 y1; x2 y2; x3 y3;…;xn yn] where n is the number of points. 11. Create a for loop that will iterate over time steps, where the maximum number of time steps is equal to the number of locations to visit along the path, (e.g. the number of red circles along the path in Fig. 1). Each iteration of the loop will correspond with one temperature measurement, followed by one change in robot’s position (i.e. it will step 1 unit along the path). 12. Within the for loop, your code must accomplish the tasks listed below. Each of which should be one line of code: a. Add the current position x,y to the Path vector. b. Query the ode23 function to obtain temperature at the current x,y position. c. Update the TempMap for the current x,y position to be equal to the steady state temperature outputted from the ode23 function call in step a. d. Update the robot position by calling the getNextPosition function you wrote. 13. Also within the for loop, you must create a 3D plot of the temperature as a function of position, and the robot path. Add a line of code in the for loop (after the code above) for each step below. a. Use the surf function to create a 3D surface plot of the TRANSPOSE of the temperature map. b. Set hold to on. c. Use plot3 to plot the path as a red line at a height of 20. Remember the first column of Path contains the x values, and the second column of Path contains the y values. d. Add a drawnow call to update the figure. Since the surf and plot functions will be called within the loop, the figure will be updated at each iteration of the loop and you should observe the temperature map being updated as the robot path grows. 14. Save your figure as a .png file and add it to your submission sheet. E80 Matlab Submission 6 Loops, Conditionals, and an ODE 1. A plot of temperature as a function of time. 2. A 2D plot of the path. 3. A 3D plot the path and temperature map surface.
The post Engineering Worksheet first appeared on Assignment writing service.
All our Essay orders are Original, and are written from scratch. Try us today at 30% off
Engineering Worksheet
Let your paper be done by an expert
Custom Essay Writing Service
Our custom essay writing service has already gained a positive reputation in this business field. Understandably so, all custom papers produced by our academic writers are individually crafted from scratch and written according to all your instructions and requirements. We offer Havard, APA, MLA, or Chicago style papers in more than 70 disciplines. With our writing service, you can get quality custom essays, as well as a dissertation, a research paper, or term papers for an affordable price. Any paper will be written on time for a cheap price.
Professional Essay writing service
When professional help in completing any kind of homework is all you need, scholarfront.com is the right place to get it. We guarantee you help in all kinds of academia, including essay, coursework, research, or term paper help etc., it is no problem for us. With our cheap essay writing service, you can be sure to get credible academic aid for a reasonable price, as the name of our website suggests. For years, we have been providing online custom writing assistance to students from countries all over the world, including the United States, Canada, the United Kingdom, Australia, Italy, New Zealand, China, and Japan.