필터 지우기
필터 지우기

how to plot all points

조회 수: 16 (최근 30일)
Vedang Mhaske
Vedang Mhaske 2021년 5월 26일
댓글: Vedang Mhaske 2021년 5월 27일
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
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
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에서 Mining Geology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by