Question

Please correct the MATLAB code under the problem, which is attempting to solve the following problem:...

Please correct the MATLAB code under the problem, which is attempting to solve the following problem:

Sally is trying to figure out how far she walked to Taco Bell to pick up food for her favorite professor, who is coughing so hard that they may have a cold. To get to her local Taco Bell, she has to walk the following distances at reasonable paces for her, especially in these hills of East LA.
1) Sally walked at 3.0 miles per hour for 20 minutes.
2) Sally then slowed down and climbed the hill, which slowed her down to 2.3 miles per hour for 5 minutes.
3) Encountering a small downslope, Sally sped up to 3.5 miles per hour for 5 minutes.
4) Some flat terrain comes, so Sally walked at 2.8 miles for 10 more minutes.
5) Around the corner from Taco Bell, Sally stopped to tie her shoes for 2 minutes.
6) Finally, Sally skipped to Taco Bell at 4.0 miles per hour for 1 minute.
How far did Sally walk to Taco Bell? Skipping is included in the term "walking".

Broken MATLAB code:

hours_per_minute = 1/60;  % 1 hr / 60 mins for conversion
walk_normal = 3.0*hours_per_minute;  % miles per minute
walk_climb = 2.3*hours_per_minute;  % miles per minute
walk_downhill = 3.5*hours_per_minute;  % miles per minute
walk_tired = 2.8*hours_per_minute;  % miles per minute
walk_skipping = 4.0*hours_per_minute;  % miles per minute
walk_stopped = 0;  % miles per minute

walking_speeds = [walk_normal, walk_climb, walk_downhill, walk_tired, 0, walk_skipping];  % vector of miles per min
walking_times = [20, 5, 5, 10, 2, 1];  % vector of minutes
total_time = zeros(1, length(walking_times)+1);  % minutes, getting the total time Sally's walked so far
distance = zeros(1, length(walking_times)+1);  % miles, pre-initializing distance traveled so far

for i=1:length(walking_times)
    distance(i) = distance(i-1) + walking_speeds(i-1)*walking_times(i-1);
    total_time(i) = total_time(i-1) + walking_times(i-1);
end

plot(total_time, distance);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The problem was with 'i' values in for loop it was going from 1 to length(walking_times) but it should be from 2 to length(walking_times)+1. Since Matlab takes only positive integers as array indices, i-1=1-1 makes array index value zero which raises an error. Length of total_time and distance is length(walking_times)+1 that is why the last value of 'i' should be length(walking_times)+1.

Otherwise, the code is correct.

Corrected Code:

hours_per_minute = 1/60; % 1 hr / 60 mins for conversion
walk_normal = 3.0*hours_per_minute; % miles per minute
walk_climb = 2.3*hours_per_minute; % miles per minute
walk_downhill = 3.5*hours_per_minute; % miles per minute
walk_tired = 2.8*hours_per_minute; % miles per minute
walk_skipping = 4.0*hours_per_minute; % miles per minute
walk_stopped = 0; % miles per minute

walking_speeds = [walk_normal, walk_climb, walk_downhill, walk_tired, 0, walk_skipping]; % vector of miles per min
walking_times = [20, 5, 5, 10, 2, 1]; % vector of minutes
total_time = zeros(1, length(walking_times)+1); % minutes, getting the total time Sally's walked so far
distance = zeros(1, length(walking_times)+1); % miles, pre-initializing distance traveled so far

for i=2:length(walking_times)+1
    distance(i) = distance(i-1) + walking_speeds(i-1)*walking_times(i-1);
    total_time(i) = total_time(i-1) + walking_times(i-1);
end

plot(total_time, distance);
xlabel('Time(minutes)')
ylabel('Distance(miles)')

Add a comment
Know the answer?
Add Answer to:
Please correct the MATLAB code under the problem, which is attempting to solve the following problem:...
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
  • Distance Walked Write a program to calculate how many mile, feet, and inches a person walks...

    Distance Walked Write a program to calculate how many mile, feet, and inches a person walks a day, given the stride length in inches (the stride length is measured from heel to heel and determines how far walk with each step], the number of steps per minute, and the minutes that person walks a day. Note that 1 mile is 5280 feet and 1 feet is 12 inches. In your program, there should be the follo constants: final Int FEET_PER_MILE...

  • Please Solve this question by using Matlab. errors accumulate ran 1.1.6. It's reasonable to expect that...

    Please Solve this question by using Matlab. errors accumulate ran 1.1.6. It's reasonable to expect that floating point during a long computation, creating what is known as a random walk. O average we expect as many errors to be negative as positive, so they tend to partially cancel out. Suppose we define a random sequence by x 0 and 4-1 ± 1 for n 1, with the signs chosen by tossing a fair coin for each n. Let a, and...

  • Class Date Name UNTQuadratic Functions, Equations, and Relations Performance Task represented by ...

    help with all please!! Class Date Name UNTQuadratic Functions, Equations, and Relations Performance Task represented by the function f(s)--0.03s+2.4s-30, where s is the speed in miles per hour. miles per gallon for Kim's new car can be The gas mileage in 1. Write an equation for the model in vertex form. 2. What is the maximum gas mileage of Kim's car? At what speed will Kim maximize his gas mileage? A new attraction at a carnival launches a participant from...

  • Use the attached Matlab code as a basis to solve the following ordinary differential equation usi...

    Question 1 QUESTION 2 Use the attached Matlab code as a basis to solve the following ordinary differential equation using Euler's method, with timestep of 0.1, from t-0to t-100. d)0) -0 - sin (5vt cos(у Plot y versus t from t=0 to t=100. How many local maxima are on this interval(do not include end points). Be careful to count them all! Answer should be an integer 1 w% Matlab code for the solution of Module 2 3 dt-9.1; %dt is...

  • Please solve this using matlab code to solve part 1 this is the timevector to use...

    Please solve this using matlab code to solve part 1 this is the timevector to use timevector=.25:.25:24; Thǐ block of code ini tidl İze3 r1a t a you need to solve the problerns in clear all, cIC Do not remove solardayl- [0 0 000 000 0 0000 00000 00000000000 0.04 0.08 0.092 0.12 0.124 0.14 0.152 0.168 0.452 1.244 2.196 2.676 2.824 2.964 3.108 3.208 3.296 3.4 3.456 3.54 2.828 3.72 3,716 3,728 3.684 3.628 3.392 3.444 1.916 2.628 2.944...

  • Consider a simple queuing system in which customers arrive randomly such that the time between successive...

    Consider a simple queuing system in which customers arrive randomly such that the time between successive arrivals is exponentially distributed with a rate parameter l = 2.8 per minute. The service time, that is the time it takes to serve each customer is also Exponentially distributed with a rate parameter m = 3 per minute. Create a Matlab simulation to model the above queuing system by randomly sampling time between arrivals and service times from the Exponential Distribution. If a...

  • solve using Matlab please Problem 2. Measurement Error You work for a precision spring company and...

    solve using Matlab please Problem 2. Measurement Error You work for a precision spring company and you have been asked to solve a problem in the quality assurance area. You are given a bunch of spring data. The displacement of the spring is measured as a function of a force applied to the spring. The factory does not care so much about the actual spring constant as they do that they spring constant does not vary too much from spring...

  • MATLAB question: I have written a code to solve for a metals resitance. Here is the...

    MATLAB question: I have written a code to solve for a metals resitance. Here is the problem statement: Part 1 (Algorithms) – due Friday: Think about how you might solve the problem of calculating the resistance of a given group of metals depending on the type of metal, the length of the wire and the area of the wire (if given the diameter only – so you must calculate the area). Write instructions (an algorithm) for a script that behaves...

  • Please help me correct my MATLAB script code for this problem, thank you!! A projectile PA is launched from point A towards the east with an initial launch velocity voa and an initial...

    Please help me correct my MATLAB script code for this problem, thank you!! A projectile PA is launched from point A towards the east with an initial launch velocity voa and an initial lauw angle of 0x. The impact point of the projectile Pa is a point B in a valley with an ordinate, yon, located below the clevation of point A. The launch from point A is instantaneously detected at point B, and a counter projectile P launched simultaneously...

  • MATLAB Code Introduction: “Petrochemical Processing” Diethanolamine (DEA) is extensively used in the gas processing industry for removing acid gases such as carbon dioxide and hydrogen sulfide from li...

    MATLAB Code Introduction: “Petrochemical Processing” Diethanolamine (DEA) is extensively used in the gas processing industry for removing acid gases such as carbon dioxide and hydrogen sulfide from light hydrocarbons. DEA's popularity is based on several factors: energy savings compared with certain other solvents; high affinity for acid gases; fair resistance to degradation. Degradation is defined as the irreversible transformation of DEA into undesirable compounds. The principal degradation compounds were found to be 3-(hydroxyethyl)-2-oxazolidone (HEOD), N,N,N-tris(hydroxyethyl)ethylenediamine (THEED), and N,N-bis(hydroxyethyl)-piperazine (BHEP).[1] In...

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