MATLAB Answers

Error in plotting (blank graph): Vector Size

조회 수: 1(최근 30일)
Justin Doan
Justin Doan 23 Oct 2020
편집: madhan ravi 23 Oct 2020
v--- = linspace(0,35,36);
v = v * 0.44704;
N = length(v);
for i= 0:N
if (i < 30)
eta(i+1) = -0.001778 * ((v(i+1)).^2) + 0.05333 * (v(i+1));
else
eta(i+1) = 0;
end
end
hold on
plot (v,eta,'r-')
xlabel('Speed (m/s)')
ylabel('Net Efficiency (Percent)')
hold off
----------------------
I have pasted my code above. When I go to plot the data (v on x axis, eta on y axis), it says there is an error and that the vectors must be the same length. Any suggestions for a fix?

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(2개)

Ameer Hamza
Ameer Hamza 23 Oct 2020
편집: Ameer Hamza 23 Oct 2020
Check the following code. You don't need to use eta(i+1).
v = linspace(0,35,36);
v = v * 0.44704;
N = length(v);
eta = zeros(1, N); % pre-allocation for efficiency
for i= 1:N
if (i < 30)
eta(i) = -0.001778 * ((v(i+1)).^2) + 0.05333 * (v(i+1));
else
eta(i) = 0;
end
end
hold on
plot (v,eta,'r-')
xlabel('Speed (m/s)')
ylabel('Net Efficiency (Percent)')
hold off

  댓글 수: 0

댓글을 달려면 로그인하십시오.


madhan ravi
madhan ravi 23 Oct 2020
편집: madhan ravi 23 Oct 2020
No loops needed:
eta = -0.001778 * v.^2 + 0.05333 * v;
eta(30 : end) = 0;

  댓글 수: 0

댓글을 달려면 로그인하십시오.

범주

태그

Community Treasure Hunt

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

Start Hunting!

Translated by