Finding the approximate length with for loop?

5 次查看(过去 30 天)
(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.
  6 个评论
Bob
Bob 2015-3-8
In my third line of coding I am trying to calculate the distance formula. Below is exactly what the function has to do:
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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2015-3-9
编辑:James Tursa 2015-3-9
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.
  4 个评论
Bob
Bob 2015-3-9
编辑:Bob 2015-3-9
step 1: Do not know how else you would do it.
Step 2:
point=solve(f(t),g(t))
Step 3:
sqrt((f(t))^2+(g(t))^2)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by