Hello,
I've been trying to debug my matlab program for a long time now and think I've finally discovered the main source of error. I have in my code this:
mod = m.*(m0 + i.*(w./wr).*sqrt(m0*m))./(m + i.*(w./wr).*sqrt(m*m0));
It's pretty gnarly, but I think all you need to know that besides the variables i and wr, the rest of the variables are merely constants. wr is a 1x100 matrix, whereas i = sqrt(-1). My problem is that mod should also be a 1x100 matrix but it winds up being a 1x1 complex matrix instead. I thought that as long as I used ./ and .* I would be able keep mod as a 1x100 matrix since wr is in the equation. Any thoughts as to how I should resolve this issue? I've tried every combo of /,*, ./, and .* I could think of to no avail. Thanks in advance!

 Accepted Answer

Guillaume
Guillaume on 9 Oct 2015
Edited: Guillaume on 9 Oct 2015
What are the values of m, m0, w and the range for wr? Your equation works as it should for me when using random values
>>wr = rand(1, 100)
>>m = rand; m0 = rand; w = rand;
>>mod = m.*(m0 + i.*(w./wr).*sqrt(m0*m))./(m + i.*(w./wr).*sqrt(m*m0));
>>size(mod)
ans =
1 100
Also, make sure that i is indeed the imaginary unit. Lots of people tend to use i as iterator in for loops.

4 Comments

Hello,
Wow, such a speedy answer! Firstly I made a mistake. wr is a constant and w is a vector.
This is w:
w = wr*logspace(-2,2,100);
As for the rest, I merely chose random numbers for m0, m, and wr, though m should be greater than m0 (m>m0). I used 1,2,4 respectively for each input.
By the way this may be superfluous info but I wanted mod to be a matrix so that V and Q could be matrices as well:
Q = imag(mod)./real(mod); V = sqrt(real(mod)./p); <-- p is another random constant
Vicky
Vicky on 9 Oct 2015
Something odd has happened. I really don't know what I did but my program suddenly works properly now! It's such an inexplicable thing to occur, especially when I've been working on this bug for hours now!!
Vicky
Vicky on 9 Oct 2015
At any rate, thank you so very much for your advice. I'm still not sure what I did to fix my error, but double checking with you seems to have been the key.
Well, I'm glad it's sorted.
I completely forgot to mention: do not call your variable mod as you wont' be able to use the built-in mod.

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 9 Oct 2015

Commented:

on 9 Oct 2015

Community Treasure Hunt

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

Start Hunting!