how to plot all points
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vedang Mhaske
el 26 de Mayo de 2021
Comentada: Vedang Mhaske
el 27 de Mayo de 2021
clc;
pstep=20;
freq = 20:150;
maxfreq = 150;
tend = maxfreq - freq; % 130,129,128...0
itime = 0;
for i=1:length(freq)
itime = itime + 1./ (pstep.*freq(i)); % 0.0025, 0.0049, 0.0072...0.1022
ifreq = freq(i) + tend./(tend .* itime); % 420, 225.8780, 161...
accl = 19620.*(sind(2.*pi.*ifreq.*itime)); % 2254.16, 2366.3...
end
plot(itime,accl,'-bo')
title('Linear Steigende Frequenz')
xlabel('Time Step in sec.')
ylabel('Acceleration mm/s^2')
Hello guys, Can ou please help me in plotting all points on graph
0 comentarios
Respuesta aceptada
Adam Danz
el 26 de Mayo de 2021
Editada: Adam Danz
el 26 de Mayo de 2021
Vectorized version...
pstep=20;
freq = 20:150;
maxfreq = 150;
tend = maxfreq - freq;
itime = cumsum(1./ (pstep.*freq));
ifreq = freq + tend./(tend .* itime);
accl = 19620.*(sind(2.*pi.*ifreq.*itime));
plot(itime,accl,'-bo')
title('Linear Steigende Frequenz')
xlabel('Time Step in sec.')
ylabel('Acceleration mm/s^2')
edit: no content change, just a clean-up to add output suppression.
2 comentarios
Adam Danz
el 26 de Mayo de 2021
The reason your version wasn't working is that you were plotting each point point-by-point which cleared the previous point that was plotted.
Using your version of the code, the problem could have been avoided by executing hold on prior to the loop and plotting each point within the loop.
But with vectorization or by storing the values within the loop as demonstrated by Christopher McCausland, you have access to results from all iterations and can plot the data after collecting the data.
Más respuestas (1)
Christopher McCausland
el 26 de Mayo de 2021
Hi Vedang,
Before considering my answer below you should know that there are more efficent methids to do this. However as you question asks why this isn't working, my solotion focuses on the 'why' rather than computational efficeny. Learning is more important in my opinion.
So alterations can be seen below, esentially your code overwites the value of variable 'itime' in each loop, you never gave told matlab where or how to store multiple values.
Line 8 does this, creating a variable (itimeArray) the same length as frequency, the 'zeros' function dictates that each value itimeArray will start as a '0'. Line 14 then takes the variable 'itime' in each loop (i) and writes it to itimeArray of i. Thus gives a 131x1 double which can be plotted rather than the 1x1 overwritten double 'itime'.
To stress again there are computationally faster ways to calculate this but from a learning point of view I feel this is more intuative.
Let me know if you need any more help,
Christopher
clc;
pstep=20;
freq = 20:150;
maxfreq = 150;
tend = maxfreq - freq; % 130,129,128...0
itime = 0;
itimeArray = zeros(length(freq),1); % Initilise the array, how big does it need to be?
for i=1:length(freq)
itime = itime + 1./ (pstep.*freq(i)); % 0.0025, 0.0049, 0.0072...0.1022
itimeArray(i,:) = itimeArray(i,:) + itime; % Write loop value to array
ifreq = freq(i) + tend./(tend .* itime); % 420, 225.8780, 161...
accl = 19620.*(sind(2.*pi.*ifreq.*itime)); % 2254.16, 2366.3...
end
plot(itimeArray,accl,'-bo')
title('Linear Steigende Frequenz')
xlabel('Time Step in sec.')
ylabel('Acceleration mm/s^2')
4 comentarios
Adam Danz
el 26 de Mayo de 2021
Editada: Adam Danz
el 26 de Mayo de 2021
@Vedang Mhaske, thanks for showing the appreciation. Many users of the Forum forget that part.
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!