Help me with this problem
Show older comments
I get an error at line 18 and says : Index in position 2 exceeds array bounds (must not exceed 4).
that's the code :
axis equal
B0 = power(1 - t, 3);
B1 = 3 .* t .* power(1 - t, 2);
B2 = 3 .* power(t, 2) .* (1 - t);
B3 = power(t, 3);
B = [B0; B1; B2; B3];
f = b * B;
hold on
plot(b(1,:),b(2,:),'r-')
plot(f(1,:),f(2,:),'b')
t=1/3;
b0 = [3;0];
b1 = [-3;3];
b2 = [3;3];
b3 = [-3;0];
b=[b0,b1,b2,b3];
for i=1:4
b0(:,i)= b(:,i).*(1-t)+b(:,i+1).*t;
end
b0
for i=1:3
b1(:,i)=b0(:,i).*(1-t)+b0(:,i+1).*t;
end
b1
for i=1:2
b2(:,i)=b1(:,i).*(1-t)+b1(:,i+1).*t;
end
b2
b3(:,1)=b2(:,1).*(1-t)+b2(:,2).*t;
plot(b0(1,:),b0(2,:),'r')
plot(b0(3,:),b0(4,:),'r')
plot(b1(1,:),b1(2,:),'g')
plot(b1(1,:),b1(2,:),'g-')
plot(b2(1,:),b2(2,:),'k')
plot(b2(1,:),b2(2,:),'k-')
plot(b3(1,:),b3(2,:),'m')
grid
hold off
Answers (1)
John D'Errico
on 24 Apr 2020
Edited: John D'Errico
on 24 Apr 2020
Just look at your code. THINK about the line in question. Here are the relevant lines.
b=[b0,b1,b2,b3];
for i=1:4
b0(:,i)= b(:,i).*(1-t)+b(:,i+1).*t;
What is the size of the vector/array b? (something by 4. I'm not sure how many rows without more carefully reading your code.)
What values does i take on? (1:4)
What happens when you try to access element 5=4+1 of a vector with 4 elements? (Read the error message again.) In fact, MATLAB directed you to exactly that line.
What do you really want to do there? (God only knows. ;-) Well, I hope you do.)
1 Comment
PAUL-ROBERT CEOLCA
on 24 Apr 2020
Categories
Find more on MATLAB 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!