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 comentarios

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.
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 el 8 de Mzo. de 2015
Editada: Geoff Hayes el 8 de Mzo. de 2015
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)
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 el 8 de Mzo. de 2015
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 el 9 de Mzo. de 2015
Still having trouble with this code.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 9 de Mzo. de 2015
Editada: James Tursa el 9 de Mzo. de 2015
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 comentarios

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 el 9 de Mzo. de 2015
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 el 9 de Mzo. de 2015
Editada: Bob el 9 de Mzo. de 2015
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 el 10 de Mzo. de 2015
I figured it out!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Bob
el 5 de Mzo. de 2015

Comentada:

Bob
el 10 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by