Vectorising equations in loop
Show older comments
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)
Accepted Answer
More Answers (0)
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!