Finding the approximate length with for loop?

5 visualizaciones (últimos 30 días)
Bob
Bob el 5 de Mzo. de 2015
Comentada: Bob el 10 de Mzo. de 2015
(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
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
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 Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by