MATLAB: Finding the approximate length with for loop

for loophomework

(f,g,a,b,n) which takes five inputs: f: A function handle. g: A function handle. a: A real number. b: A real number. n: A positive integer. Note: You may assume that that a < b. Question: The functions f(t) and g(t) determine the location of an object at any time t by (f(t), g(t)). Approximate the distance traveled by the object between t = a and t = b by dividing [a, b] into n equal subintervals, determining the location of the object at each of those t-values and then by finding the straight-line distance between those locations and adding them.
Code:
function results = mylength(f,g,a,b,n);
totalLength = 0;
interval = sqrt((f-a)^2+(g-b)^2);
for x = [a:interval:b]
if x<b
totalLength = totalLength + x + interval;
end
end
results = totalLength;
end
Error: I am getting an error at the start of the for loop. I know I am getting that error because in interval I am using 4 variables and not just 2 variables. Also an error with how I am calculating interval. Can someone help/tell me how I would adjust the code to solve for this problem.

Best Answer

Taking the instructions verbatim:
dividing [a, b] into n equal subintervals
determining the location of the object at each of those t-values
finding the straight-line distance between those locations
adding them.
So then write m-code for each of those lines.
Take the first one, "dividing [a, b] into n equal subintervals". How do you go about doing that? Suppose I said "divide the interval [0, 5] into 10 equal subintervals". The answer is intervals of length 0.5, right? You would have 0.0, 0.5, 1.0, 1.5, ..., 5.0 as your set of t values. How do you generalize that for an interval [a, b] and number of intervals n? These will be your t values.
Take the second one, "determining the location of the object at each of those t-values". The instructions tell you explicitly how to generate a point from an arbitrary value t, namely (f(t),g(t)). So just plug your t values from the previous paragraph into this formula to get all of the points involved. You should end up with n+1 points being the endpoints of n segments.
Then the third one, "Finding the straight-line distance between these locations". How do you find the straight line distance between two arbitrary points? You seem to already know it involves sqrt etc. So just apply that to successive points to get n distances.
Then the fourth one, "Adding them" ... well, nothing needs to be said here.