how to plot all points
21 views (last 30 days)
Show older comments
Vedang Mhaske
on 26 May 2021
Commented: Vedang Mhaske
on 27 May 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 Comments
Accepted Answer
Adam Danz
on 26 May 2021
Edited: Adam Danz
on 26 May 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 Comments
Adam Danz
on 26 May 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.
More Answers (1)
Christopher McCausland
on 26 May 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 Comments
Adam Danz
on 26 May 2021
Edited: Adam Danz
on 26 May 2021
@Vedang Mhaske, thanks for showing the appreciation. Many users of the Forum forget that part.
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!