find the error in the following code. x = -1:1:10; y = -1:2:10; plot(x,y) in matlab

The error in the given code is a dimension mismatch between x and y.

The reason for this error is that ":" creates an array based on the step value, but it does not take into consideration the final value, which can result in arrays with different lengths.

To fix it, we need to change either x or y such that the lengths match. We can do this by changing the step value of y to 1 instead of 2. The corrected code should look like this:

main.m
x = -1:1:10;
y = -1:1:10;
plot(x,y)
36 chars
4 lines

Alternatively, we can also change the range of x so that the lengths of x and y match. This can be achieved by changing the upper bound of x from 10 to 9. The corrected code using this method should look like this:

main.m
x = -1:1:9;
y = -1:2:10;
plot(x,y)
35 chars
4 lines

related categories

gistlibby LogSnag