8 件のコメント

Image Analyst
Image Analyst 2023 年 1 月 1 日
I can't read that language but the equation goes in MATLAB exactly as it is written there. That is proper syntax.
daniel
daniel 2023 年 1 月 1 日
Write a function velocity(x,y,z,t) that calculates the instantaneous speed when parameters are given and the following formula is known:
v= sqrt(diff(x).^2+diff(y).^2+diff(z).^2)./diff(t);
Note: The calculation of the function can only be performed when the size of the vectors t,z,y,x have the same size (length).
Torsten
Torsten 2023 年 1 月 1 日
function v = velocity(x,y,z,t)
v = sqrt(diff(x).^2+diff(y).^2+diff(z).^2)./diff(t);
end
Done.
daniel
daniel 2023 年 1 月 1 日
The calculation of the function can only be performed when the size of the vectors t,z,y,x have the same size (length).
Torsten
Torsten 2023 年 1 月 1 日
編集済み: Torsten 2023 年 1 月 1 日
Yes, then supply t,x,y and z as vectors of the same length.
Or check their lengths in the function.
Matt J
Matt J 2023 年 1 月 1 日
daniel
daniel 2023 年 1 月 1 日
Walter Roberson
Walter Roberson 2023 年 1 月 1 日
In the case of that error message, the problem is that you had accidentally created a variable named velocity that you are then trying to index. If there is a variable in scope and a function of the same name, MATLAB gives priority to the variable in scope.

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

 採用された回答

Image Analyst
Image Analyst 2023 年 1 月 1 日

0 投票

Try this:
v = velocity([0, 5.6, 7], [0, 1.1, 2.4], [0, 4, 20], [0, 1, 2])
v = 1×2
6.9692 16.1137
function v = velocity(x,y,z,t)
v = [];
if ~isequal(size(x), size(y)) || ~isequal(size(x), size(z)) || ~isequal(size(x), size(t))
uiwait(errordlg('Sizes do not match'));
return;
end
v = sqrt(diff(x).^2+diff(y).^2+diff(z).^2)./diff(t);
end

5 件のコメント

daniel
daniel 2023 年 1 月 1 日
Image Analyst
Image Analyst 2023 年 1 月 1 日
You must be running different code. I ran mine right here in answers. Paste your code in here, just like I did, then click the green run triangle.
What does this show if you type it in the command window?
which -all velocity
All you have is the m-file function, right? You don't have a variable called velocity, right?
daniel
daniel 2023 年 1 月 1 日
daniel
daniel 2023 年 1 月 1 日
its working tnks
Voss
Voss 2023 年 1 月 3 日
@daniel: isequal accepts more than two input arguments and returns true if and only if all the inputs are equivalent.
That is, you can replace this:
if ~isequal(size(x), size(y)) || ~isequal(size(x), size(z)) || ~isequal(size(x), size(t))
with this:
if ~isequal(size(x), size(y), size(z), size(t))

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

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2023 年 1 月 1 日

コメント済み:

2023 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by