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!