Error Not same Vector length why not functioning

Hi guys, I'm very new to matlab and am wondering why my code does not function properly. I searched the same error but couldnt understand what would solve my problem exactly. My professor wants us to write t2 = 0:0.2:20 instead, but both have the same error. Help and any explanation is appreciated!

 Accepted Answer

Just as a guess, did you expect this to create a 20 element vector whose first element is 0 and whose last is 2001?
t1 = linspace(0, 20, 2001);
size(t1)
ans = 1×2
1 2001
If so you've just listed the inputs in the wrong order. The documentation page for the linspace function shows that in the three input syntax, the first two inputs are the first and last element. The third is the length of the output vector. This is consistent with the two input syntax, which behaves as though you'd specified 100 as the third input.
"y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1)."
t2 = linspace(0, 2001, 20);
size(t2)
ans = 1×2
1 20
By the way, linspace helps you out a little bit avoiding an issue with your third vector as you've written it. The expression you specified, 10/0.07, is not an integer value. But linspace does not error; it rounds the value down to the nearest integer and uses that as the number of points.
n = 10/0.07
n = 142.8571
n == round(n) % not an integer value
ans = logical
0
t3 = linspace(0, 10, 10/0.07);
size(t3)
ans = 1×2
1 142
length(t3) == floor(n)
ans = logical
1

1 Comment

Thank you very much, I did not expect anything since this was the first time I made something in Matlab. My professor just mixed up some variables, that's why it didnt function properly and now I also see why! Thanks again!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 7 Nov 2023

Commented:

on 7 Nov 2023

Community Treasure Hunt

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

Start Hunting!