Matlab basic problem

Discussion in 'Math' started by mikewashere05, Nov 26, 2009.

1. mikewashere05 Thread Starter New Member

Oct 26, 2009
15
0
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:
Code ( (Unknown Language)):
1. function [y] = lab10(T,sclr)
2. wd = sqrt(1.-(sclr.^2));
3. we = 1-(2.*sclr.^2);
4. y = exp((-1.*sclr).*T).*(2.*sclr.*cos(wd.*T)+(we./wd).*sin(wd.*T));
5.
Here are the results:
Code ( (Unknown Language)):
1.
2. >> T=[0:pi/10:4*pi]
3.
4. T =
5.
6.   Columns 1 through 8
7.
8.          0    0.3142    0.6283    0.9425    1.2566    1.5708    1.8850    2.1991
9.
10.   Columns 9 through 16
11.
12.     2.5133    2.8274    3.1416    3.4558    3.7699    4.0841    4.3982    4.7124
13.
14.   Columns 17 through 24
15.
16.     5.0265    5.3407    5.6549    5.9690    6.2832    6.5973    6.9115    7.2257
17.
18.   Columns 25 through 32
19.
20.     7.5398    7.8540    8.1681    8.4823    8.7965    9.1106    9.4248    9.7389
21.
22.   Columns 33 through 40
23.
24.    10.0531   10.3673   10.6814   10.9956   11.3097   11.6239   11.9381   12.2522
25.
26.   Column 41
27.
28.    12.5664
29.
30. >> sclr=[0,.2,.3,.4,.5]
31.
32. sclr =
33.
34.          0    0.2000    0.3000    0.4000    0.5000
35.
36. >> lab10(T,sclr)
37. ??? Error using ==> times
38. Matrix dimensions must agree.
39.
40. Error in ==> lab10 at 4
41. y = exp((-1.*sclr).*T).*(2.*sclr.*cos(wd.*T)+(we./wd).*sin(wd.*T));
42.
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?

2. t_n_k AAC Fanatic!

Mar 6, 2009
5,448
783
Maybe you forgot 'endfunction'

3. mikewashere05 Thread Starter New Member

Oct 26, 2009
15
0
If that was actually the problem I'd slap myself pretty hard.

I just figured it out, pretty simple. When I did
Code ( (Unknown Language)):
1. 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.

Code ( (Unknown Language)):
1. T = [0:(4 * pi) / 4:4 * pi];
Works fine. I just hope I'm understanding the wording of the problem correctly.

4. ashwini1 New Member

Aug 1, 2009
9
0
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.