Hallo, i have my matlab exam tomorrow, can anyone help me find the errors in the give Parameter. Thankyou

1 回表示 (過去 30 日間)
function y = f4(a)
v = [a;pi];
if v(2)>round(v(1))
y= v(1);
else
y=pi+v(3)
end

回答 (1 件)

Elias Gule
Elias Gule 2017 年 7 月 13 日
The error is in the line
y=pi+v(3)
You are essentially trying to retrieve the 3rd element of a 2-element array/vector. This will throw "Index exceeds matrix dimensions." exception. Note that the line
v = [a;pi];
initializes a 2 x 1 vector, v.
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 7 月 13 日
This might be what is happening in practice, but in the general case, if the function were called with a column vector as input, then the entire column vector would be put into v, followed by pi. So if it were called with (for example) [8.3; 7.9] then v(1) would become 8.3, v(2) would become 7.9, and v(3) would become pi, and there would then be no processing error.
Therefore possibly the problem is in not passing a large enough vector to the function.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by