I don't get a reasonable output for my code?

In summary, the conversation discusses the use of Matlab software to plot the probability of error for a given equation. The command "trapz" is used for numerical integration, but the resulting plot has incorrect values on the y-axis. The formula for the plot is provided using Maketex, and the Matlab code used to plot it is also shared. The discussion also includes troubleshooting tips and suggestions for debugging the code.
  • #1
deema_master
13
0
I'm trying to plot the probability of error for the following equation using Matlab software, i want to use the command "trapz" for the numerical integration, the problem is that i get a fine shape for the plot, but the values in the y-axis are wrong, the whole curve should be between 0 and 1.2 but it is between 0.492 and 0.5! Can anyone just tell me what is wrong in my code, or just give me a hint? I really need help. Here is my formula that i need to plot written using Maketex:$$P(e)=0.5-\frac{.5}{\sqrt{\pi}}\sum_{\alpha=1-k_1/2}^{\infty}\sum_{\beta=1-k_2/2}^{\infty}\sum_{\eta=0}^{\infty}\sum_{N=0}^{\infty}\sum_{M=0}^{\infty}\sum_{Q=0}^{\infty}\sum_{i=0}^{\eta}\sum_{j=0}^{N}\sum_{A=0}^{N-j}v^{\eta+N+1/2} C
\int_0^{\infty}\exp(-z*v(1+1.5/v))z^{\eta+N-.5} \frac{1}{(z+1)^{A+Q+Nrkr/2} (z+.5)^{M+i+j+Nsks/2}} dz$$

where:
$$C=0.25*\exp \left(-\frac{\lambda_1}{2}\right)*\left(\frac{\lambda_1^2}{4}\right)^{\alpha/2}*\left(\frac{\lambda_2^2}{4}\right)^{\beta/2}*\exp \left(-\frac{\lambda_2}{2}\right)*\left(\frac{\lambda_1}{4 *em *v}\right)^{\eta}*\left(\frac{\lambda_2}{4 *em* v}\right)^N*\exp (-\frac{\lambda_r*Nr}{2})*0.25^{Ns*ks/4-0.5}*\exp (-\frac{\lambda_s*Ns}{2})*0.25^{Nr*kr/4-0.5}*(Ns*\lambda_s/4)^M*(Nr*\lambda_r/4)^Q*\frac{1}{\eta! M! N! Q! \Gamma (M+Ks*Ns/2) \Gamma (Q+Nr*kr/2) \Gamma (\beta+N+1) \Gamma (\eta+\alpha+1)}*{em}^A {\eta\choose{i}}{N\choose{j}}{{N-j}\choose{A}}(em+1)^{N-j-A} \Gamma (A+Q+Nr*kr/2) \Gamma (i+j+M+Ns*ks/2)*\left(2^{A+Q+Nr*kr/2)}\right.$$And here is my Matlab code:
Code:
close all; clear;clc;
Nr=2;Ns=2;
lmda1=.3; lmda2=.3;
lmdas=.1; lmdar=.1;

z= 0.0001:1:40;
k1=2;k2=2;
kr=2.*Nr;ks=2.*Ns;
ax=0;
avg=0.0001:1:40;
em=1;
ch=2;

for alp=1-k1.*.5:ch
    for beta=1-k2.*.5:ch
        for eta=0:ch
            for N=0:ch
                for M=0:ch
                    for Q=0:ch
                        for id=0:eta
                            for jd=0:N
                                for A=0:N-jd
                                    %
                                  
                                  
                                  
                                  
                                    up=.25.*exp(-lmda1./2).*(lmda1./2).^(alp).*(lmda2.^2./(4)).^(beta./2).*exp(-lmda2./2).*(lmda1./(4.*em.*avg)).^eta.*(lmda2./(4.*em.*avg)).^N.*exp(-lmdas.*Ns.*.5).*.25.^(ks.*.25-.5).*exp(-lmdar.*Nr.*.5).*.25.^(kr.*.25-.5).*(Ns.*lmdas.*.25).^M.*(Nr.*lmdar.*.25).^Q;
                                  
                                    cy=up.*(1./(factorial(eta).*factorial(N).*factorial(M).*factorial(Q).*gamma(eta+alp+1).*gamma(N+beta+1).*gamma(M+ks.*.5).*gamma(Q+kr.*.5)));
                                    cj=cy.*(factorial(eta)./(factorial(id).*factorial(eta-id))).*(factorial(N)./(factorial(jd).*factorial(N-jd))).*gamma(M+id+jd+ks.*.5);
                                    f1=(cj.*(factorial(N-jd)./(factorial(A).*factorial(N-jd-A))).*em.^A.*(((em+1).^(N-jd-A))).*gamma(kr.*.5+Q+A));
                                  
                                    f2=f1.*(2.^(kr.*.5+Q+A)).*avg.^(eta+N);
                                    ax=ax+f2;
                                  
                                  
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end
q2=2;n2=2;N2=1;eta2=1;

fun2 = exp(-z.*avg.*(1+1.5./avg)).*z.^(eta2+N2-1./2).*(1./((1+z).^(q2).*(1./2+z).^(n2)));

out= trapz(z,fun2);

b=.5.*(1-ax.*(1./sqrt(pi)).*out.*avg.^(1./2));
plot(avg,b);grid;
 
Physics news on Phys.org
  • #2
I fixed your latex as you needed $$ to bracket the expressions
 
  • #3
jedishrfu said:
I fixed your latex as you needed $$ to bracket the expressions
great! Thank you.
 
  • #4
This is probably something we can't help you with directly. My suggestion is to pick apart your program and validate that intermediate results are coming out correctly.

Programmers will often break up code into smaller pieces to make it easier to debug and would add print statements to show intermediate results.
 
  • #5
What is going on with z (and avg)?

You have:
z=0.0001:1:40

Is this what you intend?
z = 0.0001 1.0001 2.0001 3.0001 ...etc... 39.0001 (length of z is 40)
 
  • #6
Ye
lewando said:
What is going on with z (and avg)?

You have:
z=0.0001:1:40

Is this what you intend?
z = 0.0001 1.0001 2.0001 3.0001 ...etc... 39.0001 (length of z is 40)
Yes this is what i want...i know z should go to a larger value...but i get a dimension error so i made their size the same.
 

1. Why is my code not producing the expected output?

There could be multiple reasons for this. It could be due to a logical error in your code, incorrect use of functions or methods, missing or incorrect input data, or even a typo. It is important to carefully review your code and check for any potential errors.

2. How can I debug my code to find the issue?

One way to debug your code is by using print statements or a debugger tool to track the flow of your code and check the values of variables at different points. You can also try running your code with different inputs to see if the issue persists.

3. Could the issue be with my computer or programming environment?

It is possible that the issue could be with your computer or programming environment, but it is more likely that there is an error in your code. However, it is always a good idea to check for any software updates or conflicts that could be causing the issue.

4. I have tried everything, but my code still won't give the expected output. What should I do?

If you have exhausted all possible solutions and your code is still not producing the expected output, it might be helpful to seek advice from a colleague or a programming community. They may be able to spot an error that you may have missed or suggest a different approach to solving the problem.

5. How can I prevent this issue from happening in the future?

To prevent similar issues from occurring in the future, it is important to write clean and organized code with proper comments and documentation. Regularly testing your code with different inputs and debugging it thoroughly can also help catch any errors early on.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Advanced Physics Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Nuclear Engineering
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
Back
Top