Matlab basic problem

Thread Starter

mikewashere05

Joined Oct 26, 2009
15
Hello all,

I have a Matlab homework problem that I'm struggling a little bit with. I would post this under the "Homework" board but that is categorized under "Electronics" and isn't very fitting.

Here is the question:


Here is my .m file:
Rich (BB code):
function [y] = lab10(T,sclr)
wd = sqrt(1.-(sclr.^2));
we = 1-(2.*sclr.^2);
y = exp((-1.*sclr).*T).*(2.*sclr.*cos(wd.*T)+(we./wd).*sin(wd.*T));
Here are the results:
Rich (BB code):
>> T=[0:pi/10:4*pi]

T =

  Columns 1 through 8

         0    0.3142    0.6283    0.9425    1.2566    1.5708    1.8850    2.1991

  Columns 9 through 16

    2.5133    2.8274    3.1416    3.4558    3.7699    4.0841    4.3982    4.7124

  Columns 17 through 24

    5.0265    5.3407    5.6549    5.9690    6.2832    6.5973    6.9115    7.2257

  Columns 25 through 32

    7.5398    7.8540    8.1681    8.4823    8.7965    9.1106    9.4248    9.7389

  Columns 33 through 40

   10.0531   10.3673   10.6814   10.9956   11.3097   11.6239   11.9381   12.2522

  Column 41

   12.5664

>> sclr=[0,.2,.3,.4,.5]

sclr =

         0    0.2000    0.3000    0.4000    0.5000

>> lab10(T,sclr)
??? Error using ==> times
Matrix dimensions must agree.

Error in ==> lab10 at 4
y = exp((-1.*sclr).*T).*(2.*sclr.*cos(wd.*T)+(we./wd).*sin(wd.*T));
Whats wrong?! I thought all my math was being done element wise (with the .* and ./ instead of * and /), so why is it complaining about matrix dimensions?
 

Thread Starter

mikewashere05

Joined Oct 26, 2009
15
Maybe you forgot 'endfunction'
If that was actually the problem I'd slap myself pretty hard.

I just figured it out, pretty simple. When I did
Rich (BB code):
T = [0:(4 * pi) / 5:4 * pi];
It didn't give me a 5 element array, it gave me a 5 "division" array I suppose you could say, which is actually a 6 element array.

Rich (BB code):
T = [0:(4 * pi) / 4:4 * pi];
Works fine. I just hope I'm understanding the wording of the problem correctly.
 

ashwini1

Joined Aug 1, 2009
9
Hope by this time you might have got answer for your question,
Any way my observation is that the problem is due to the mismatch in the size of arrays you are multiplying, T and wd arrays are not of the same length.
size(T)
= 1 41
size(wd)
= 1 5
And for matrix multiplication matlab expects that both the arrays should be of same size.

Your program works fine with:
T=[0 : pi : 4*pi];
 
Top