Why is my plot plotting blank?
63 次查看(过去 30 天)
显示 更早的评论
>> t = 0:20;
>> Vx = 1000;
>> for ii = 1:2:length(t)
Vx = Vx - (0.0004*Vx^2)*t(ii)
end
Vx =
1000
Vx =
200
Vx =
136
Vx =
91.6096
Vx =
64.7542
Vx =
47.9818
Vx =
36.9310
Vx =
29.2931
Vx =
23.8014
Vx =
19.7225
Vx =
16.6107
>> plot (t,Vx)
1 个评论
John
2015-11-4
You are plotting a single point. You are overwriting Vx every loop iteration. You need to index Vx. Try this:
t = 0:20;
Vx = zeros(size(t));
Vx(1) = 1000;
for ii = 2:length(t)
Vx(ii) = Vx(ii - 1) - (0.0004*Vx(ii - 1)^2)*t(ii);
end
figure, plot(t, Vx)
回答(2 个)
Walter Roberson
2015-11-2
Your plot is not empty. You can see that with a minor change:
plot (t,Vx, '*')
You should ask yourself how many items are in t and how many items are in Vx
Nitin Khola
2015-11-4
编辑:Nitin Khola
2015-11-4
As Walter has mentioned, Vx in this case is a scalar. Refer to the last bullet in the description for plot(X,Y) by following the link below: http://www.mathworks.com/help/matlab/ref/plot.html#description
Your code indicates that you have a variable "Vx" that is dependent on the variable "t". Also, most for loops can be avoided as discussed in following documentation. http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
If there is no possibility for eliminating the "for" loop, you can do data linking so that the plot updates its data. Refer to the following documentation for details on data linking: http://www.mathworks.com/help/matlab/data_analysis/making-graphs-responsive-with-data-linking.html
I do not recommend it as it will seriously affect the performance of the code, however, just for illustration purposes, you can modify your existing code to include a "hold on" and a "scatter" in the loop. Note, that I do not recommend it. It will just help you see how these commands work.
for ii = 1:2:length(t)
Vx = Vx - (0.0004*Vx^2)*t(ii)
scatter(t(ii),Vx);
hold on
end
I would suggest reading in detail about vectorization and data linking is the best approach.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!