Recursive method from for loop
Afficher commentaires plus anciens
Hi!
I have a normal working loop
clc;
time = [0, 1, 2, 3, 4, 10, 12, 18];
distance = [4.71, 9, 15, 19, 20, 45, 55, 78];
totaltime = 0;
totaldistance = 0;
figure;
hold on
for i=1:1:8
dis = 0.9^(-i) * distance(i);
totaldistance = dis+totaldistance;
hold on
ti = 0.9^(-i) * time(i);
totaltime = ti+totaltime;
plot(ti,dis,'p')
hold on
plot(totaltime,totaldistance,'o')
hold on
end
v=totaldistance/totaltime;
estimatedspeed = v
plot(v,'*')
And I need to get the same result of estimated speed using recursive method. I'm a begginer in Matlab and I need a little bit of help. I tried to write a function as it would be recursive, but nothing goes right. Where I'm making a mistake?
clc;
time = [0, 1, 2, 3, 4, 10, 12, 18];
distance = [4.71, 9, 15, 19, 20, 45, 55, 78];
totaltime = 0;
totaldistance = 0;
function averagespeed = speed(time, distance)
for i=1:1:8
dis = 0.9^(-i) * distance(i);
ti = 0.9^(-i) * time(i);
totaldistance = dis+totaldistance;
totaltime = ti+totaltime;
averagespeed = speed + totaldistance/totaltime;
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!