Where is my error?

2 views (last 30 days)
Brian Hoblin
Brian Hoblin on 23 Oct 2016
Commented: Brian Hoblin on 23 Oct 2016
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

Image Analyst
Image Analyst on 23 Oct 2016
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 Comments
Brian Hoblin
Brian Hoblin on 23 Oct 2016
"I'll play around with that. Thanks
Brian Hoblin
Brian Hoblin on 23 Oct 2016
Yes! this did it! I had to adjust the numerator and denominator line a little and that made it work. Well, the lines you posted worked but what should be in the numerator and denominator were a little mixed up. With all the parentheses that is to be expected (You should see me with a calculator). Thank you so much for the help. I really appreciate it.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 23 Oct 2016
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
Brian Hoblin on 23 Oct 2016
Edited: Walter Roberson on 23 Oct 2016
I tried that but I'm still getting the "Matrix dimensions must argee" error. This is the code I'm most recent tried,
T=0:0.01:4*pi;
C=[0, 0.2, 0.3, 0.4, 0.5];
y=exp.(T.*(-C)).*((2.*C.*cos(T.*(sqrt.(1-(C.^2)))))+(((1-2.*(C.^2))./(sqrt.(1-(C.^2)))).*sin(T.*(sqrt.(1-(C.^2))))));
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
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))')
I don't think the exp. and sqrt. is any different from exp and sqrt because I still get the same message with and without the "." but I was missing the "./"

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!