how to plot all points
11 次查看(过去 30 天)
显示 更早的评论
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/630900/image.png)
Hello guys, Can ou please help me in plotting all points on graph
0 个评论
采纳的回答
Adam Danz
2021-5-26
编辑:Adam Danz
2021-5-26
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 个评论
Adam Danz
2021-5-26
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.
更多回答(1 个)
Christopher McCausland
2021-5-26
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 个评论
Adam Danz
2021-5-26
编辑:Adam Danz
2021-5-26
@Vedang Mhaske, thanks for showing the appreciation. Many users of the Forum forget that part.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mining Geology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!