How do i create a variable as index for vector m for my code below.

4 ビュー (過去 30 日間)
cyril adesina
cyril adesina 2020 年 3 月 16 日
編集済み: Guillaume 2020 年 3 月 16 日
clear
K = 1;
M = zeros(1,1000);
for omega = 1:1000
s = omega*i;
G = K/(s*(s+1.71)*(s+100));
M(omega) = G;
end
omega = 1:1000;
phase = rad2deg(angle(M));
dB = 20*log10(abs(M));
semilogx(omega,dB)
title('Semilogarithmic Decibel Plot for K = 1')
xlabel('Frequency Range (rad/s)')
ylabel('Decibels (dB)')
grid on
I am trying to create two plots using loop programming (for…end), one plot is the magnitude
20 log M (this is called decibel dB) for from 0.1 to 1000 , and the other plot is the phase (in degree) for from 0. 1 to 1000.
I have attached the code for 1-1000 but when i chnage the 1 value i get an error saying only integers can be used. Can anyone help me fix to this shows a graph for the boundary 0.1-1000.
  1 件のコメント
Guillaume
Guillaume 2020 年 3 月 16 日
編集済み: Guillaume 2020 年 3 月 16 日
In response to the flag "Can I have my question deleted. I do not want my code to be copied":
Sorry, we do not delete questions. According to the Terms of Use, "You agree that all Content that you contribute to the MATLAB Answers portion of MATLAB Central will be licensed under the Creative Commons Attribution Share Alike 3.0 license".
It's completely contrary to the spirit of Answers to get your question deleted once you've received help. We give you help on the understanding that it may benefit more than you. If you want personal support, Answers is not for you.
In any case, the code you've posted is trivial, there's nothing here that could be confidential.
And in case, the OP vandalise the question, this is the original
clear
K = 1;
M = zeros(1,1000);
for omega = 1:1000
s = omega*i;
G = K/(s*(s+1.71)*(s+100));
M(omega) = G;
end
omega = 1:1000;
phase = rad2deg(angle(M));
dB = 20*log10(abs(M));
semilogx(omega,dB)
title('Semilogarithmic Decibel Plot for K = 1')
xlabel('Frequency Range (rad/s)')
ylabel('Decibels (dB)')
grid on
I am trying to create two plots using loop programming (for…end), one plot is the magnitude
20 log M (this is called decibel dB) for from 0.1 to 1000 , and the other plot is the phase (in degree) for from 0. 1 to 1000.
I have attached the code for 1-1000 but when i chnage the 1 value i get an error saying only integers can be used. Can anyone help me fix to this shows a graph for the boundary 0.1-1000.

サインインしてコメントする。

採用された回答

Jakob B. Nielsen
Jakob B. Nielsen 2020 年 3 月 16 日
Your issue is that if you make a for loop for 0.1 to 1000, and then use that same counter as an index, when you then type M(omega) = G; you try to place into the 0.1'th index of M, the result G. Clearly, this is impossible.
Separate your loop counter from your X, like this. This also means you can make any change you want just to your omega input, and the remainder of the script will still fly.
clear
K = 1;
omega = 0.1:0.1:1000; %omega goes from 0.1, in steps of 0.1, to 1000.
M = zeros(1,size(omega,2)); %adjust your preallocation size to be equal to your omega length
for count= 1:size(omega,2) %and the loop counter as well
s = omega(count)*i; %finally, take the loop counter index of omega
G = K/(s*(s+1.71)*(s+100));
M(count) = G;
end
phase = rad2deg(angle(M));
dB = 20*log10(abs(M));
semilogx(omega,dB)
title('Semilogarithmic Decibel Plot for K = 1')
xlabel('Frequency Range (rad/s)')
ylabel('Decibels (dB)')
grid on
  1 件のコメント
cyril adesina
cyril adesina 2020 年 3 月 16 日
for i = 0.1:0.1:1000
if phase(i) < 0
phase(i) = phase(i) + 360;
end
end
semilogx(omega,phase)
title('Semilogarithmic Phase Plot for K = 1')
xlabel('Frequency Range (rad/s)')
ylabel('Phase Angle (deg)')
grid on
When i input the i value in relation to the above code in my first question,my code does not work can somebody help please. when i change 'for i = 1:1000, the graph works. i need it for the range 0.1 to 1000.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime and Frequency Domain Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by