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

1 view (last 30 days)
function y = f4(a)
v = [a;pi];
if v(2)>round(v(1))
y= v(1);
else
y=pi+v(3)
end

Answers (1)

Elias Gule
Elias Gule on 13 Jul 2017
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 Comment
Walter Roberson
Walter Roberson on 13 Jul 2017
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!