whats wrong with my code?

1 回表示 (過去 30 日間)
daniel
daniel 2023 年 1 月 1 日
コメント済み: Image Analyst 2023 年 1 月 1 日
function v=velocity(x,y,z,t)
if and (length(x)==length(y), length(y)==length(z))
if length(z)==length(t)
v=sqrt(diff(x).^2+diff(y).^2+diff(z).^2)./diff(t);
disp(velocity,sqrt)
else
disp('Vectors must have the same length for calculation');
end
end
  1 件のコメント
Voss
Voss 2023 年 1 月 1 日
What is your intent with this line?
disp(velocity,sqrt)
That calls the function velocity() again, with no arguments (and the function sqrt() with no arguments), which causes an error.
Maybe you mean this instead?
disp(v)

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

回答 (1 件)

Image Analyst
Image Analyst 2023 年 1 月 1 日
編集済み: Image Analyst 2023 年 1 月 1 日
Well #1 is the lack of comments. All programmers should comment their code, and you should too.
Secondly, this
if and (length(x)==length(y), length(y)==length(z))
should be
if (length(x)==length(y)) && (length(y)==length(z))
And the very first line inside the function should be
v = [];
so that you at least return something if the length test fails.
  3 件のコメント
Torsten
Torsten 2023 年 1 月 1 日
disp(velocity,sqrt)
This statement does not make sense since "velocity" is the name of the function, but not of a variable that could be displayed.
Further, if such a variable existed, it had to be
disp(sqrt(velocity))
instead of
disp(velocity,sqrt)
Image Analyst
Image Analyst 2023 年 1 月 1 日
Like @Voss said, you need
disp(v)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by