In an assignment A(I) = B, the number of elements in B and I must be the same.

1 回表示 (過去 30 日間)
Ben Booth
Ben Booth 2015 年 2 月 18 日
コメント済み: Ben Booth 2015 年 2 月 18 日
Hi all,
I am using Matlab for a university tutorial, and have been assigned the task of:
"Plot the response of G1(s) to the first cycle of a sine wave
G1(s) = (3s+2) / (2s^3+4s^2+5s+1)
u(t) = | 0 for t<0 | 2*sin(10*pi*t) for 0<t>8 | 0 for t>=8"
In order to solve the problem, I have written the following code:
num=[3 2];
den=[2 4 5 1];
time=[0:0.02:10]';
u=zeros(length(time),1);
x=2*sin(10*pi*time);
for i=min(find(time>=0 & time<8):length(time))
u(i)=x;
end
sys=tf(num,den);
y=lsim(sys,u,time);
plot(time,y,'r',time,u,'m')
However, I am receiving the error "In an assignment A(I) = B, the number of elements in B and I must be the same."
I understand that this issue is arising because I am trying to input a vector (x) into a scalar (i), however, I am unsure how I can overcome this issue, as my value for x is constantly changing. Could anyone advise me on a function which can be used, or any modification I could use to resolve the issue.
Thank you in advance, Ben

採用された回答

Erik S.
Erik S. 2015 年 2 月 18 日
I attach a file, is it the result you need?
  1 件のコメント
Ben Booth
Ben Booth 2015 年 2 月 18 日
Hi Erik,
This looks very similar to the result which I require.
Thank you very much for all your help.

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

その他の回答 (2 件)

Erik S.
Erik S. 2015 年 2 月 18 日
Hi Your x in the loop is a vector write
y(i)=x(i)
  1 件のコメント
Ben Booth
Ben Booth 2015 年 2 月 18 日
Hi Erik,
Thank you for your quick reply.
After making the modification you suggested the error is removed, however, I am not getting the desired plot. As shown below, the output remains at a constant 0, and does not alter with time. Is there anything else which you can suggest?
Thanks in advance, Ben

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


Erik S.
Erik S. 2015 年 2 月 18 日
Hi
Your loop condition is not correct I think. It will only run 1 time. At which iteration should it start and finish?

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by