How do I fix my code when the index exceeds the number if array elements?
Mostrar comentarios más antiguos
drag(t)=findDrag(velocity(t),dragCoefficient,density(t),surfaceA);
function [DRAG] = findDrag (velocity,density,dragCoefficient,surfaceA)
%findDrag Calculate the force of Drag on the rocket
%Detailed explanation goes here
DRAG = (1/2)*((velocity)^2)*(density)*dragCoefficient*surfaceA;
end
Respuestas (2)
Image Analyst
el 4 de Dic. de 2018
1 voto
What is the value of t when you call it?
What are the lengths of the velocity and density vectors?
t must be less than the length of the velocity and density vectors.
1 comentario
madhan ravi
el 4 de Dic. de 2018
+1 made it clear
madhan ravi
el 4 de Dic. de 2018
0 votos
change drag(t) to drag
4 comentarios
Image Analyst
el 4 de Dic. de 2018
If t is valid, then drag(t) should be okay. What we need to know is the value of t. If it's undefined, or zero, or a negative number, or a fractional number, then that's a problem. It sounds like t is some positive number, like 10 or whatever, but that the velocity and density arrays do not have t elements in them, like velocity and density have only 4 elements in them instead of 10.
madhan ravi
el 4 de Dic. de 2018
Exactly sir Image Analyst but as I can see the operator * inside the function DRAG returned is just a scalar so there‘s no point in indexing drag(t) although maybe it should be .* only the OP can clarify it
Image Analyst
el 4 de Dic. de 2018
Editada: Image Analyst
el 4 de Dic. de 2018
madhan, this code works just fine:
vec = zeros(1, 10)
vec(8) = MyFunction(999)
function output = MyFunction(inputArg)
output = inputArg;
end
It will assign 999 to the 8th element of vec. You don't even need to initialize vec if you don't want to. This also works just fine:
vec(8) = MyFunction(999)
function output = MyFunction(inputArg)
output = inputArg;
end
It will create a vector of 8 elements and assign 999 to the 8th element.
However, this doesn't
v = zeros(1, 5);
t = 55;
vec(8) = MyFunction(999, v(t))
function output = MyFunction(inputArg, d)
output = inputArg;
end
and that is the problem he is having.
madhan ravi
el 4 de Dic. de 2018
yes , thank you for making it clear , really an expert advice! :)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!