Where is my error?
Show older comments
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))')
Accepted Answer
More Answers (1)
Image Analyst
on 23 Oct 2016
0 votes
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 Comment
Brian Hoblin
on 23 Oct 2016
Edited: Walter Roberson
on 23 Oct 2016
Categories
Find more on Labels and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!