Vectorising equations in loop
이전 댓글 표시
I have made the following code (C1) using arrays. I undertsand this is bad practise so i tried to write it again this time vectorising the equations but i keep getting "Array indices must be positive integers or logical values."
%C1: Loop with array
n=0.1;
v=[];
for i=1:n:4.9
v=[v 0.1553567*((i)^6)-2.0416*((i)^5)+9.1837*((i)^4)-14.829*((i)^3)-1.3703*((i)^2)+32.821*(i)-1.3155];
end
for i=4.9:n:15.3
v=[v 0.003980879*(i^5)-0.2247*(i^4)+4.8682*(i^3)-50.442*(i^2)+254.67*(i)-430.66];
end
for i=15.4:n:25
v=[v -0.073*(i^2)+6.1802*(i)+40.423];
end
disp(v)
t=0.9:n:25;
plot(t,v)
%C2: Loop with vectors
n=0.1;
v=[];
for i=1:n:4.9
v(i)= 0.1553567*((i)^6)-2.0416*((i)^5)+9.1837*((i)^4)-14.829*((i)^3)-1.3703*((i)^2)+32.821*(i)-1.3155;
end
for i=4.9:n:15.3
v(i)=0.003980879*(i^5)-0.2247*(i^4)+4.8682*(i^3)-50.442*(i^2)+254.67*(i)-430.66;
end
for i=15.4:n:25
v(i)= -0.073*(i^2)+6.1802*(i)+40.423;
end
disp(v)
t=0.9:n:25;
plot(t,v)
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!