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]);

1 comentario

Stephen23
Stephen23 el 3 de Nov. de 2017
See Jan Simon's answer for the simplest solution.

Iniciar sesión para comentar.

 Respuesta aceptada

John BG
John BG el 5 de Oct. de 2017
Editada: John BG el 5 de Oct. de 2017

1 voto

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 comentarios

David Oshidero
David Oshidero el 5 de Oct. de 2017
Thank you, I'll try work on changing this then
Jan
Jan el 5 de Oct. de 2017
Editada: Jan el 5 de Oct. de 2017
If you pass a scalar and a vector plot basically does nothing
No. Try this:
plot(1:10, 5, 'o')
You see, it does draw something. But if you do not specify the marker, the single points are not visible. With plot(60:70, y) a polygon is drawn through the points, which is visible also, even if there are no markers.
Walter Roberson
Walter Roberson el 5 de Oct. de 2017
Note that because y(m) is being assigned to and m starts at 60, y(1) through y(59) are going to exist and be 0, which is going to throw off the plotting. plot([60:70], y) would fail because y would not be the same length as [60:70]
Also note that [60:70] is a more expensive way of writing 60:70 or (60:70). My timing tests suggest that using [60:70] is on the order of 40% slower than using 60:70 (though for an array that small, there is a lot of variation in the timings.)
John BG
John BG el 5 de Oct. de 2017
the 'does nothing' is a way to speak.
The point is that neither passing a single value against a function represented with the many values of a vector, nor the multicolour bubble plot all-to-one mapping are what was being asked.
The y(m) of the question is an m-to-m mapping common to the majority functions.
Walter Roberson
Walter Roberson el 5 de Oct. de 2017
"The y(m) of the question is an m-to-m mapping common to the majority functions."
In the user's code, it is a (max(m)-min(m)+1) to max(m) mapping -- that is, 70-60+1 = 11 different m values, and 70 vector locations created, the first min(m)-1 = 59 of which would be 0.
Jan
Jan el 6 de Oct. de 2017
@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 el 7 de Oct. de 2017
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?

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 5 de Oct. de 2017

3 votos

After the for loop m will be the scalar 70, not the range.

6 comentarios

David Oshidero
David Oshidero el 5 de Oct. de 2017
how would I go about changing it so m is the range?
Walter Roberson
Walter Roberson el 5 de Oct. de 2017
The following general pattern is useful:
%%Define Variables
g = 9.8;
c = 14;
v=35;
%%Calculations
figure; hold on
m_vals = 60:70;
num_m = length(m_vals);
y = zeros(1, num_m);
for m_idx = 1 : num_m
m = m_vals(m_idx);
y(m_idx) = (14*35)/(m*9.8);
end
plot(m_vals, y)
Walter Roberson
Walter Roberson el 5 de Oct. de 2017
In the specialized case, the above can be abbreviated as
%%Define Variables
g = 9.8;
c = 14;
v=35;
%%Calculations
figure; hold on
y = zeros(1, 11);
for m = 60:70
y(m-60+1) = (14*35)/(m*9.8);
end
plot(60:70, y)
David Oshidero
David Oshidero el 6 de Oct. de 2017
Okay, thanks I'll retry taking all this into account
David Oshidero
David Oshidero el 6 de Oct. de 2017
I wanted to ask what does the y = zeros do in the eq?
Jan
Jan el 6 de Oct. de 2017
Editada: Jan el 5 de Nov. de 2017
@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.

Iniciar sesión para comentar.

Jan
Jan el 6 de Oct. de 2017
Editada: Jan el 9 de Oct. de 2017

2 votos

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 comentarios

Walter Roberson
Walter Roberson el 7 de Oct. de 2017
The lines
t = 60:70;
y = (14*35) / (m * 9.8);
should be
m = 60:70;
y = (14*35) ./ (m * 9.8);
Jan
Jan el 9 de Oct. de 2017
Thank you, Walter.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Oct. de 2017

Editada:

Jan
el 5 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by