a script problem in matlab!!

Discussion in 'Programmer's Corner' started by theo23, Nov 29, 2008.

  1. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    hi my friends.
    I have a problem and i want your help.I have the next code.I want to find the integral(0,inf) of final for v=-12:0.05:2.for one value of v,for example
    -11.95,the code gives me a result.When i try to change the value of v,-10.95 for example the value of subtotal is again the same and i dont know why.Why this happens?is there a problem in my code?Do you know what can i change to have for each value of v the equivalent subtotal and total?Because i want to make a plot(vdot,total) and with this code i think that the result will be a straight line which is wrong.
    A=12.56e-4;
    L=0.44;
    vdot=v/L
    G=2;
    T=300;
    q=1.6e-19;
    k=8.62e-5;
    %Edot between 0.01-0.7
    Edot=0.01;
    %Ndot between 10^8-10^15
    Ndot=10^8;
    %DE between 0.01-0.5
    DE=0.05;
    syms E;

    denom=1+exp((E-q*vdot)/(k*T));
    function1=1./denom;
    static=(G*Ndot)/(sqrt(pi/2)*DE);
    ek8eths=((E+Edot+q*vdot)/DE).^2;
    function2=exp(-2*ek8eths);
    function3=function1.*static;
    final=function3.*function2;

    %or if i try final=function1.*function2.*static is correct?

    subtotal=double(int(final,E,0,inf))
    total=q*A*L*subtotal

    Maybe if i try to solve it with gaussian integral?But i dont know how to use this method...
    Thank you.
     
  2. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Your code is terribly hard to decipher.

    First tell us what EXACTLY this code is supposed to do... because your description doesn't explain anything besides that you're tying to take an integral and finding different values don't change your outcome... expand on your explanation since your problem is probably in the code leading up to the integral, not in matlab's integral function.

    Secondly, where are you defining v? Is this a function? Why don't you include the whole thing?

    Also make your code more readable somehow, like perhaps putting code tags (put the word code in a bracket) around your code...

    -blazed
     
  3. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    yes you are right.Sorry.
    I have this big equation:

    Cdot=qAL∂/∂v∫(1/(1+(exp(E-qVdot)/kT))*(gNdot/sqrt(piM/2))*exp(-2((E+Edot+qVdot)/M)^2)dE.

    Now i want to insert this equation in Matlab(sorry,my English is not very good):)

    I have this values
    A=12.56*10^-4;,L=0.44;,G=2;,T=300;,q=1.6*10^-19;,k=8.62*10^-5;,
    Edot=0.01;,Ndot=10^8;,M=0.001;
    Edot must be between 0.01-1
    Ndot between 10^8-10^15
    M between 0.001-0.5
    The integral i want to be (0,inf)
    V must be:V=-12:0.05:2;
    Also,Vdot=V/L;
    First of all,I want this big function Cdot to give me a result for each value of V every time.After i must create a plot(V,Cdot).
    The code that i am thinking is:

    First i create a function in a M-file with name integral3:

    function total=integral3(vdot)
    A=12.56*10^-4;
    L=0.44;
    G=2;
    T=300;
    q=1.6*10^-19;
    k=8.62*10^-5;
    Edot=0.01;
    Ndot=10^8;
    M=0.001;
    de=0.01;
    E=0:0.01:1000;
    expe=((E+Edot+(q*vdot)/L)/M).^2;
    den=1+exp((E-(q*vdot)/L)/(k*T));
    static=(G*Ndot)/(sqrt(pi/2)*M);
    sinar2=exp(-2*expe);
    sinar1=1./den;
    sinar3=sinar1.*static;
    final=sinar3.*sinar2;
    subtotal=sum(final)*de;
    total=q*A*L*subtotal;


    And after in other M-file:
    cdot1(1)=integral3(-12);
    cdot2(281)=integral3(2.05);
    for i=2:281
    vdot(i)=-12+(i-1)*0.05;
    cdot1(i)=integral3(vdot(i));
    cdot2(i-1)=cdot1(i);
    cdot(i)=1e11*(cdot2(i)-cdot1(i))/0.05;
    end
    plot(vdot,cdot,'.:')

    My problem is that with this loop Cdot has always the same value...What's going wrong?
    What can i change to have for different value of Vdot,different Cdot?
    I think that maybe now understand my question..:)
     
  4. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    So far, the only thing I've noticed is this:

    "static=(G*Ndot)/(sqrt(pi/2)*M);" should have the M inside the square root according to your equation:

    "Cdot=qAL∂/∂v∫(1/(1+(exp(E-qVdot)/kT))*(gNdot/sqrt(piM/2))*exp(-2((E+Edot+qVdot)/M)^2)dE"

    Can you show perhaps what you're getting for the plot of Cdot?

    Is it actually showing a straight line, because you use the integral3 function a lot of times?

    Wait a second... why are you never integrating? Why are you never differentiating with respect to v?

    Hmm... my math must be rusty. Is sum of all the values multiplied by the dE equivalent to the integral? If so, then that's correct, but I still don't see where you're differentiating with respect to v...

    Could that be your problem? I'll look back at this later tonight.

    Good luck,
    -blazed
     
    Last edited: Nov 30, 2008
  5. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    ooops!!Yes you are right!!the correct is sqrt(piM/2)
    yes i use the integral3 function a lot of times because i want for each value of v different value of Cdot.
    Do you think that with this code i'm never integrating?Do you have any idea or do you know another way to find the integral?
    Maybe you are right in this part that i'm never differentiating with respect to v.
    If i change the code like this:
    function total=integral3(v)
    A=12.56*10^-4;
    L=0.44;
    G=2;
    T=300;
    q=1.6*10^-19;
    k=8.62*10^-5;
    Edot=0.01;
    Ndot=10^8;
    M=0.001;
    de=0.01;
    vdot=v/L;
    E=0:0.01:1000;
    expe=((E+Edot+(q*vdot))/M).^2;
    den=1+exp((E-(q*vdot))/(k*T));
    static=(G*Ndot)/(sqrt(pi*M/2));
    sinar2=exp(-2*expe);
    sinar1=1./den;
    sinar3=sinar1.*static;
    final=sinar3.*sinar2;
    subtotal=sum(final)*de;
    total=q*A*L*subtotal;

    and

    cdot1(1)=integral3(-12);
    cdot2(281)=integral3(2.05);
    for i=2:281
    v(i)=-12+(i-1)*0.05;
    cdot1(i)=integral3(v(i));
    cdot2(i-1)=cdot1(i);
    cdot(i)=1e11*(cdot2(i)-cdot1(i))/0.05;
    end
    plot(v,cdot,'.:')

    will be better?Do you think that now i'm differentiating with respect to v?But again i have a straight line as a result.Oh God!!
    You know,maybe the problem is that in my 'integral3' function returns the same value all the time because the value which i pass in, 'Vdot', is multiplied by 'q', which is a tiny number. So it has a very small effect on the result.
     
  6. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    First of all: in your original equation, based on the parenthesis, the integral is of 1 / everything else. So your denominator should include sinar 2 and static.

    And I have a question: why are you multiplying by 1*10^11 here: cdot(i)=1e11*(cdot2(i)-cdot1(i))/0.05;

    ?

    -blazed
     
  7. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    one of my friends suggest me the 1e11 to use it but he didn't expain me the reason..Tomorrow i'll see my code again with your notices and i'll tell you what's happen.again i want to thank you for your help..
     
  8. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Sure, no problem. But it's still weird how integral(v) gives the same number no matter what value of v you input. Vdot is in the equation... so why does it seem to have no affect? It's a bit baffling...

    -blazed
     
  9. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    oh sorry again.i forgot 2 ().Denominator not includes sinar 2 and static.
    Well,as an example:
    a=(1/(1+(exp(E-qVdot)/kT))
    b=(gNdot/sqrt(piM/2))
    c=exp(-2((E+Edot+qVdot)/M)^2)
    So the final equation is
    Cdot=qAL∂/∂v∫(a*b*c)de
    I think that now you understand completely my equation.:)
    I 'run' again the program without value 1e11.Now i have a better result something like -1.9134e-015 but this result is again the same for all the values of v and again my plot is straight line.something goes wrong with my problem.But what is the question...:)

    Also,one of my friends suggest me the Gauss-Kronrod method.What do you think about this method?
    http://www.mathworks.com/access/hel...Count=10&query=quadgk&submitButtonName=Search
     
    Last edited: Dec 2, 2008
  10. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Alright, so I copied over the function and tested it myself.

    I've discovered the problem: there is none.

    q is simply 1.6*10^-19, so at very small values of v, like 3 or even 200, even powers of 10 below 8 or so, Edot + E + some small number ≈ Edot + E

    Does that make sense? Same thing with the denominator, E - some small number is approximately E.

    Simply try a number like 3*10^15. You'll notice the results change.

    -blazed
     
  11. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    yes there is the proble.Edot + E + some small number ≈ Edot + E
    this value 3e15 you suggest me for q?because i dont understand completely.
     
  12. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    No, no. Q = 1.6*10^-19 coulombs. I meant try that value for v. Once you use values that large, Edot+E+small number will be Edot+E+large number, which means that it WILL affect the outcome of the integral.

    Just plug in those values and you'll notice that the integral3(v) function works fine.

    -Asaf
     
  13. theo23

    Thread Starter Member

    Nov 29, 2008
    16
    0
    Finally i found the problem.It was a so stupid error...Thank you very much for your help Blaze...
     
  14. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    No problem. Glad to see you got it fixed,

    -blazed
     
Loading...