Why is my plot plotting blank?
184 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
>> 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 Commento
John
il 4 Nov 2015
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)
Risposte (2)
Walter Roberson
il 2 Nov 2015
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
il 4 Nov 2015
Modificato: Nitin Khola
il 4 Nov 2015
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 Commenti
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!