How do I fix my code when the index exceeds the number if array elements?
显示 更早的评论
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
回答(2 个)
Image Analyst
2018-12-4
1 个投票
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.
madhan ravi
2018-12-4
0 个投票
change drag(t) to drag
4 个评论
Image Analyst
2018-12-4
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
2018-12-4
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
2018-12-4
编辑:Image Analyst
2018-12-4
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
2018-12-4
yes , thank you for making it clear , really an expert advice! :)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!