Where is my error?
1 回表示 (過去 30 日間)
古いコメントを表示
I'm very new to MATLAB and I'm having alot of trouble debugging this. I need to write an M file that will accept a vector T, and a scalar C (zeta) and return a vector y whose value is defined as,
y=e^(-CT)*(2Ccos(Wd*T)+(We/Wd)*sin(Wd*T))
where Wd=sqrt(1-C^2) and We=1-2C^2 for C=[0,0.2,0.3,0.4,0.5] over the interval T=[0,4pi].
I keep getting the three same messages depending on the changes I make. Either, Matrix dimensions must agree, or error using ".*" or error using "*". Here is my code,
C=[0 0.2 0.3 0.4 0.5];
T=0:0.01:4*pi;
y=exp((-C).*T)*((2*C*cos((sqrt(1-(C^2)).*T)))+(((1-2*(C^2))/(sqrt(1-(C^2))))*sin((sqrt(1-(C^2))).*T)));
y=response(T,C(1));
figure;plot(T,y,'b');hold on
y=response(T,C(2));
plot(T,y,'r');hold on
y=response(T,C(3));
plot(T,y,'k');hold on
y=response(T,C(4));
plot(T,y,'m');hold on
y=response(T,C(5));
plot(T,y,'--b');hold on
I've had help with it but I'm still not able to get it to work. Like I said, I'm very new to this. I've spent hours trying to debug this myself but I just don't know what I'm doing well enough to find the problem.
I can get the desired results from this code but I'm pretty sure that this is not the way they want it to be done,
T = 0:0.01:4*pi;
y=exp(T*(-0)).*((2*0*cos(T*(sqrt(1-(0^2)))))+(((1-2*(0^2))/(sqrt(1-(0^2)))).*sin(T*(sqrt(1-(0^2))))));
plot (y);hold on
y=exp(T*(-0.2)).*((2*0.2*cos(T*(sqrt(1-(0.2^2)))))+(((1-2*(0.2^2))/(sqrt(1-(0.2^2)))).*sin(T*(sqrt(1-(0.2^2))))));
plot (y);hold on
y=exp(T*(-0.3)).*((2*0.3*cos(T*(sqrt(1-(0.3^2)))))+(((1-2*(0.3^2))/(sqrt(1-(0.3^2)))).*sin(T*(sqrt(1-(0.3^2))))));
plot (y);hold on
y=exp(T*(-0.4)).*((2*0.4*cos(T*(sqrt(1-(0.4^2)))))+(((1-2*(0.4^2))/(sqrt(1-(0.4^2)))).*sin(T*(sqrt(1-(0.4^2))))));
plot (y);hold on
y=exp(T*(-0.5)).*((2*0.5*cos(T*(sqrt(1-(0.5^2)))))+(((1-2*(0.5^2))/(sqrt(1-(0.5^2)))).*sin(T*(sqrt(1-(0.5^2))))));
plot (y);hold on grid on legend('C=0','C=0.2','C=0.3','C=0.4','C=0.5'); ylabel ('y axis') xlabel ('T axis') title ('Graph of y=e^-^C^T(2Ccos(w_dT)+(w_e/w_d)sin(w_dT))')
0 件のコメント
採用された回答
Image Analyst
2016 年 10 月 23 日
C is 5 elements long, and T is 1257 elements long, so you can't do (-C).*T because the vector lengths don't match. Perhaps you need loops over both C and T to handle all permutations. Or else use meshgrid() to do it vectorized but that's a little more tricky.
4 件のコメント
その他の回答 (1 件)
Image Analyst
2016 年 10 月 23 日
Try putting a dot in front of every mathematical operation, so .* instead of *, ./ instead of /, .^ instead of ^. No need for the dot for plus and minus though. I didn't run your code, but this is an easy thing to try so just try it and let me know what happens. Post new code if it still fails and you're certain you have a dot in front of every *, /, and ^.
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!