MATLAB starts repeating operations for no reason
9 views (last 30 days)
Show older comments
EDIT 2: Figured it out, it was just a wrong parenthesis (how the hell can that cause such a terrible outcome?).
I can't really tell what's going on. I have a script with a loop that integrates a matrix for each value of a variable m. In a separate formula I set up the matrix (K), but when I execute the script, I see that the matrix starts oscillating between certain values, never getting to the integral. Is this a usual problem? Why does it happen? This is the code, in case you want to see what I mean:
%SCRIPT
Ms=4.92549e-6;
Int=10*Ms;
Mmax=200*Ms;
Error=zeros(1,numel(Ms:Int:Mmax));
for m=Ms:Int:Mmax
L=2*integral(@(f) ParameterErrorsFormula2(f,m),20,5000,'ArrayValued',true)
N=inv(L)
Error(round((m-Ms)/Int)+1)=100*sqrt(N(4,4));
end
loglog((Ms:Int:Mmax)/Ms,Error)
%FUNCTION
function K=ParameterErrorsFormula2(f,m)
m1=0.5*m;
sm=m1*(m-m1)/m^2;
M=m*sm.^(3/5);
u=(pi*M*f).^(1/3);
Function= @(f) f.^(-7/3)/3.24e-46./((6.35*(f/500)).^-5 + 2./(f/500) + 1 + (f/500).^2);
SNR=10;
I=integral(Function,20,5000);
r2=4*I*M.^(5/3)./(SNR.^2*30*pi^(4/3))
h2=SNR.^2/(4*I*f.^(7/3));
Sn=3.24e-46*((6.35*(f/f0)).^-5 + 2./(f/f0) + 1 + (f/f0).^2);
i=sqrt(-1);
a=0.98;
fq=(1-0.63*(1-a)^(3/10))/(2*pi*m);
Q=2*(1-a)^(-9/20);
e=1.5/100;
Ard=sqrt(128*sm.^2*e*m./(10*pi*r2*fq*Q));
k=Ard.*exp(i*2*pi*f)/(2*pi)
h_rd=k.*(1./(fq/Q-2*i*(f-fq))+1./(fq/Q-2*i*(f+fq)))
d_fq=k.*fq.*((2*i*Q+1)*Q./(fq*(i-2*Q)+2*f*Q).^2)+(1-2*i*Q)*Q./(fq*(i+2*Q)+2*f*Q).^2
A=[h_rd 0 0 (h_rd/6-d_fq) (2*h_rd/5+3*d_fq/5)]
B=conj(A)
C=B'*A
D=A'*B
K=(C+D)./Sn
Sorry about posting such a long code, I just don't know what is happening, it keeps repeating the same operations over and over. Thanks to anyone who tries to help!
EDIT: I just found out it just takes a damn long time to finish. However, the graph I get is just random lines with no clear pattern. There is something fundamentally wrong with my code, does anybody have any idea of what could it be?
2 Comments
Star Strider
on 25 Jul 2015
It would help if you describe what you’re doing. What problem are you solving?
One observation: The (') operator takes the complex-conjugate transpose of the variable that precedes it. So here, to get ‘C’, you’re actually multiplying the simple transpose of ‘A’ by ‘A’, not its complex-conjugate transpose:
B=conj(A)
C=B'*A
The simple transpose is (.'). This is also the exception to the rule that the dot-operator indicates element-wise operations. Everywhere else it does; here it converts the complex-conjugate transpose to the simple transpose.
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!