how to plot all points

24 次查看(过去 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')
Hello guys, Can ou please help me in plotting all points on graph

采纳的回答

Adam Danz
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 个评论
Vedang Mhaske
Vedang Mhaske 2021-5-26
Hello Adam,
Thank you for your reply. As I was trying for a linear increasing frquency, it works perfectly for me.
Thank you for your precious time and help.
Kind regards
Vedang
Adam Danz
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
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
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.
Vedang Mhaske
Vedang Mhaske 2021-5-27
Just wanted to share with you that I tried your code with the function zeros. But at first time it was not showing any rise in y direction(accl). So I just created one more Array function to plot all the varying accl points and now its working.
Thank you.
Kind regards
Vedang
clc;
pstep=20;
freq = 20:150;
maxfreq = 150;
tend = maxfreq - freq; % 130,129,128...0
itime = 0;
itimeArray = zeros(length(freq),1);
acclArray = zeros(length(accl)); % new array for accl
for i=1:length(freq)
itime = itime + 1./ (pstep.*freq(i)); % 0.0025, 0.0049, 0.0072...0.1022
itimeArray(i,:) = itimeArray(i,:) + itime;
ifreq = freq(i) + tend./(tend .* itime); % 420, 225.8780, 161...
accl = 19620.*(sind(2.*pi.*ifreq.*itime)); % 2254.16, 2366.3...
acclArray(i,:) = acclArray(i,:) + accl;
end
plot(itimeArray,acclArray,'-bo')
title('Linear Steigende Frequenz')
xlabel('Time Step in sec.')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by