MATLAB plotting problems...

32 次查看(过去 30 天)
David Oshidero
David Oshidero 2017-10-5
编辑: Jan 2017-11-5
I have been trying to plot this graph on matlab or figure out how to go about doing it but everything I seem to do so far does not work. I was hoping I could get some help.
%%Define Variables
g = 9.8;
c = 14;
v=35;
%%Calculations
figure; hold on
for m = 60:70
y(m) = (14*35)/(m*9.8);
end
figure(1); plot(m,y(m));
xlim([60 70]);

采纳的回答

John BG
John BG 2017-10-5
编辑:John BG 2017-10-5
Hi David
your code doesn't plot because m is scalar while y is a vector.
command plot requires either a vector, in this case would be y, or 2 vectors.
If you pass a scalar and a vector plot basically does nothing.
try either
plot([60:70],y)
or simply
plot(y)
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  7 个评论
Jan
Jan 2017-10-6
@John BG: Your "does nothing" is accompanied by "your code doesn't plot because m is scalar while y is a vector" and "command plot requires either a vector [...] or 2 vectors". Both statements are wrong and therefore confusing - independent from the fact, if this is or is not the point of the original question.
John BG
John BG 2017-10-7
the reason why this question was posted is that the plot was invoked with a scalar in the reference vector while passing the vector y in the function field of the plot.
My answer is not confusing, it addresses the attempt to use a scalar as reference to multiple values in y.
The code posted in the question didn't plot, but with the correct reference vector input in plot, now it plots ok, what's so confusing?

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2017-10-5
After the for loop m will be the scalar 70, not the range.
  6 个评论
David Oshidero
David Oshidero 2017-10-6
I wanted to ask what does the y = zeros do in the eq?
Jan
Jan 2017-10-6
编辑:Jan 2017-11-5
@David: It is a pre-allocation. The output array is created at once, because this is much cheaper than letting the array grow iteratively. Example:
x = [];
for k = 1:1e7
x(k) = k + rand;
end
Now in each iteration Matlab has to create a new array in the memory, which is 1 element larger than the old one, copy the former contents and insert the new value. Finally Matlab did not reserve memory for 1e6 elements, but for sum(1:1e7). For a double this needs 8 byte per element, such that 400 TB must be allocated and copied. This wastes a lot of time. To avoid this, the array is allocated before the loop:
x = zeros(1, 1e7);
for k = 1:1e7
x(k) = k + rand;
end
While the first version takes 1.75 sec, the second needs 0.45 sec only.
Well, this is the theory. Fortunately Matlab tries to reduce the drawbacks as good as possible. Even a fast processor could allocate and copy 400 TB in 1.75 seconds.
In your case the runtime does not matter. But the strategy in the forum is to teach good programming styles in general.

请先登录,再进行评论。


Jan
Jan 2017-10-6
编辑:Jan 2017-10-9
A simplified version of your code:
g = 9.8;
c = 14;
v = 35;
t = 60:70;
y = (14*35) ./ (m * 9.8); % [EDITED, fixed typo, thanks Walter!]
figure;
plot(m, y);
xlim([60 70]);
This is called "vectorizing": Matlab can perform the calculations with the vector m directly. This can be faster and nicer than the loop version, and it is less prone to bugs of indexing.
  2 个评论
Walter Roberson
Walter Roberson 2017-10-7
The lines
t = 60:70;
y = (14*35) / (m * 9.8);
should be
m = 60:70;
y = (14*35) ./ (m * 9.8);
Jan
Jan 2017-10-9
Thank you, Walter.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by