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
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
madhan ravi 2018 年 12 月 4 日

0 投票

change drag(t) to drag

4 件のコメント

Image Analyst
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
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
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
madhan ravi 2018 年 12 月 4 日
yes , thank you for making it clear , really an expert advice! :)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2018 年 12 月 4 日

コメント済み:

2018 年 12 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by