Finding the approximate length with for loop?

(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 件のコメント

Stephen23
Stephen23 2015 年 3 月 6 日
I don't get any error:
>> mylength(1,2,3,4,0)
ans =
5.8284
Can you explain what you are doing that generates the error, and give us the exact and complete error message.
Jan
Jan 2015 年 3 月 6 日
Moved from Answer section:
Anyone help me?
[Please do nut bump a question by adding a non-answer in the answer section. Thanks]
Bob
Bob 2015 年 3 月 8 日
編集済み: Geoff Hayes 2015 年 3 月 8 日
This is how I test the code by running another script file and calling that function:
answer = mylength(@(t) 3*sin(t),@(t) 3*cos(t), 0, 2*pi, 10)
answer =18.541019662496844
error:
Undefined function 'minus' for input arguments of type 'function_handle'.
Error in mylength (line 3)
interval = sqrt((f-a)^2+(g-b)^2);
Error in Test (line 4)
results = mylength(@(t) 3*sin(t),@(t) 3*cos(t), 0, 2*pi, 10)
Geoff Hayes
Geoff Hayes 2015 年 3 月 8 日
Bradley - the first input to your function mylength is a function handle which corresponds to the f input of your function. In the third line, your code is doing
interval = sqrt((f-a)^2+(g-b)^2);
where it tries to subtract a from the function f. what do you really mean to happen at this line? Please describe in detail why f and g are function handles and whether your mylength function requires this to be true.
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.
Bob
Bob 2015 年 3 月 9 日
Still having trouble with this code.

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2015 年 3 月 9 日
編集済み: James Tursa 2015 年 3 月 9 日

0 投票

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 日
Step1:
t=(b-a) / n;
Step2: not sure how to do? What formula?
Step3:
sqrt((a)^2+(b)^2);
Is this correct? Would this all be in one line of code?
James Tursa
James Tursa 2015 年 3 月 9 日
Step 1 is incorrect. What you have shown, (b-a)/n, is the delta-t value (the difference between successive t values), not the t values themselves.
Step 2 is to plug the t values directly into the (f(t),g(t)) formula. E.g., if the first t value is 7, then the first point is (f(7),g(7)).
Step 3, yes that is the general formula, but you shouldn't be using a and b here ... you should be using the results of Step 2.
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)
Bob
Bob 2015 年 3 月 10 日
I figured it out!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

Bob
2015 年 3 月 5 日

コメント済み:

Bob
2015 年 3 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by